示例#1
0
  public void setupCurrentRowVariables() {
    if (_evaluatingConverterInsideSetupCurrentRowVariables) {
      // Prevent endless recursion in the column.getGroupingValueConverter() call (see below).
      // The grouping-related variables are not required during calculation of
      // groupingValueConverter,
      // so we can just skip this method if _evaluatingConverterInsideSetupCurrentRowVariables ==
      // true
      return;
    }
    DataTable dataTable = getDataTable();
    Object rowData = dataTable.isRowAvailable() ? dataTable.getRowData() : null;
    if (!(rowData instanceof GroupHeaderOrFooter)) {
      Components.setRequestVariable(getColumnHeaderVar(), null);
      Components.setRequestVariable(getGroupingValueVar(), null);
      Components.setRequestVariable(getGroupingValueStringVar(), null);
      return;
    }

    GroupHeaderOrFooter row = (GroupHeaderOrFooter) rowData;
    RowGroup rowGroup = row.getRowGroup();

    String columnId = rowGroup.getColumnId();
    Column column = (Column) dataTable.getColumnById(columnId);

    String columnHeader = column.getColumnHeader();
    Components.setRequestVariable(getColumnHeaderVar(), columnHeader);

    Object groupingValue = rowGroup.getGroupingValue();
    Components.setRequestVariable(getGroupingValueVar(), groupingValue);

    Converter groupingValueConverter;
    _evaluatingConverterInsideSetupCurrentRowVariables = true;
    // The upcoming getGroupingValueConverter call can invoke table's setRowIndex, which will result
    // re-entering this setupCurrentRowVariables method, so we should prevent endless recursion
    // here.
    try {
      groupingValueConverter = column.getGroupingValueConverter();
    } finally {
      _evaluatingConverterInsideSetupCurrentRowVariables = false;
    }
    FacesContext context = FacesContext.getCurrentInstance();
    if (groupingValueConverter == null) {
      groupingValueConverter =
          groupingValue != null
              ? Rendering.getConverterForType(context, groupingValue.getClass())
              : null;
    }
    String groupingValueStr =
        groupingValueConverter != null
            ? groupingValueConverter.getAsString(context, column, groupingValue)
            : groupingValue != null ? groupingValue.toString() : "";
    Components.setRequestVariable(getGroupingValueStringVar(), groupingValueStr);
  }
  boolean isInSameGroup(FacesContext context, DataTable table, int currentRowIndex) {

    table.setRowIndex(currentRowIndex);
    Object currentGroupByData = table.getSortBy();

    table.setRowIndex(currentRowIndex + 1);
    if (!table.isRowAvailable()) return false;

    Object nextGroupByData = table.getSortBy();
    if (currentGroupByData != null && nextGroupByData.equals(currentGroupByData)) {
      return true;
    }

    return false;
  }
  protected void encodeSubTable(
      FacesContext context, DataTable table, SubTable subTable, int rowIndex, String rowIndexVar)
      throws IOException {
    table.setRowIndex(rowIndex);
    if (!table.isRowAvailable()) {
      return;
    }

    // Row index var
    if (rowIndexVar != null) {
      context.getExternalContext().getRequestMap().put(rowIndexVar, rowIndex);
    }

    subTable.encodeAll(context);

    if (rowIndexVar != null) {
      context.getExternalContext().getRequestMap().remove(rowIndexVar);
    }
  }
  public void encodeTbody(FacesContext context, DataTable table, boolean dataOnly)
      throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    String rowIndexVar = table.getRowIndexVar();
    String clientId = table.getClientId(context);
    String emptyMessage = table.getEmptyMessage();
    UIComponent emptyFacet = table.getFacet("emptyMessage");
    SubTable subTable = table.getSubTable();
    SummaryRow summaryRow = table.getSummaryRow();

    if (table.isSelectionEnabled()) {
      table.findSelectedRowKeys();
    }

    int rows = table.getRows();
    int first = table.getFirst();
    int rowCount = table.getRowCount();
    int rowCountToRender =
        rows == 0 ? (table.isLiveScroll() ? table.getScrollRows() : rowCount) : rows;
    boolean hasData = rowCount > 0;

    if (!dataOnly) {
      writer.startElement("tbody", null);
      writer.writeAttribute("id", clientId + "_data", null);
      writer.writeAttribute("class", DataTable.DATA_CLASS, null);
    }

    if (hasData) {
      for (int i = first; i < (first + rowCountToRender); i++) {
        if (subTable != null) {
          encodeSubTable(context, table, subTable, i, rowIndexVar);
        } else {

          table.setRowIndex(i);
          if (!table.isRowAvailable()) {
            break;
          }

          encodeRow(context, table, clientId, i, rowIndexVar);

          if (summaryRow != null && !isInSameGroup(context, table, i)) {
            table.setRowIndex(i); // restore
            encodeSummaryRow(context, table, summaryRow);
          }
        }
      }
    } else {
      // Empty message
      writer.startElement("tr", null);
      writer.writeAttribute("class", DataTable.EMPTY_MESSAGE_ROW_CLASS, null);

      writer.startElement("td", null);
      writer.writeAttribute("colspan", table.getColumnsCount(), null);

      if (emptyFacet != null) emptyFacet.encodeAll(context);
      else writer.write(emptyMessage);

      writer.endElement("td");

      writer.endElement("tr");
    }

    if (!dataOnly) {
      writer.endElement("tbody");
    }

    // Cleanup
    table.setRowIndex(-1);
    if (rowIndexVar != null) {
      context.getExternalContext().getRequestMap().remove(rowIndexVar);
    }
  }