コード例 #1
0
ファイル: JsGridReportBase.java プロジェクト: GIGADEVICE/imt
  /**
   * 向浏览器输出JSON数据
   *
   * @param
   * @return void
   */
  public void outDataToBrowser(TableData tableData) {
    StringBuffer outData = new StringBuffer();

    // 向前台输出数据
    outData.append("{pageInfo: {totalRowNum: " + tableData.getTotalRows() + "},");
    outData.append("data: [");
    boolean isFirst = true;

    TableHeaderMetaData headerMetaData = tableData.getTableHeader();
    List<TableDataRow> dataRows = tableData.getRows();
    try {
      for (TableDataRow dataRow : dataRows) {
        List<TableDataCell> dataCells = dataRow.getCells();
        int size = dataCells.size();
        if (!isFirst) {
          outData.append(",{");
          for (int i = 0; i < size; i++) {
            outData.append(
                headerMetaData.getColumnAt(i).getId() + ": '" + dataCells.get(i).getValue() + "',");
          }
          int index = outData.lastIndexOf(",");
          outData.deleteCharAt(index);
          outData.append("}");
        } else {
          outData.append("{");
          for (int i = 0; i < size; i++) {
            outData.append(
                headerMetaData.getColumnAt(i).getId() + ": '" + dataCells.get(i).getValue() + "',");
          }
          int index = outData.lastIndexOf(",");
          outData.deleteCharAt(index);
          outData.append("}");
          isFirst = false;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    outData.append("]");
    outData.append("}");

    try {
      out.print(outData.toString());
      out.flush();
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }