All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.extjs.gxt.ui.client.widget.grid.GroupSummaryView Maven / Gradle / Ivy

There is a newer version: 2.3.1-gwt22
Show newest version
/*
 * Sencha GXT 2.3.0 - Sencha for GWT
 * Copyright(c) 2007-2013, Sencha, Inc.
 * [email protected]
 * 
 * http://www.sencha.com/products/gxt/license/
 */
 package com.extjs.gxt.ui.client.widget.grid;

import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.core.DomHelper;
import com.extjs.gxt.ui.client.core.El;
import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.core.Template;
import com.extjs.gxt.ui.client.core.XDOM;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Params;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.dom.client.TableSectionElement;

/**
 * A GroupingView with support for a summary row.
 */
public class GroupSummaryView extends GroupingView {

  protected Template rowTpl, cellTpl;

  /**
   * Returns the summary node element.
   * 
   * @param g the group element
   * @return the summary node
   */
  public El getSummaryNode(Element g) {
    if (g != null) {
      return fly(g).down(".x-grid3-summary-row");
    }
    return null;
  }

  /**
   * Returns true if summaries are visible.
   * 
   * @return true for visible
   */
  public boolean isSummaryVisible() {
    return !grid.el().hasStyleName("x-grid-hide-summary");
  }

  /**
   * Toggles the summary information visibility.
   * 
   * @param visible true for visible, false to hide
   */
  public void toggleSummaries(boolean visible) {
    El el = grid.el();
    if (el != null) {
      if (visible) {
        el.removeStyleName("x-grid-hide-summary");
      } else {
        el.addStyleName("x-grid-hide-summary");
      }
    }
  }

  @SuppressWarnings({"unchecked", "rawtypes"})
  protected Map calculate(List models, List cs) {
    Map data = new FastMap();

    for (int j = 0, jlen = models.size(); j < jlen; j++) {
      ModelData m = models.get(j);
      for (int i = 0, len = cs.size(); i < len; i++) {
        ColumnData c = cs.get(i);
        SummaryColumnConfig cf = (SummaryColumnConfig) cm.getColumn(i);
        if (cf.getSummaryType() != null) {
          data.put(c.name, cf.getSummaryType().render(data.get(c.name), m, c.name, data));
        }
      }
    }
    return data;
  }

  protected void doAllWidths(List ws, int tw) {
    if (!enableGrouping) return;
    NodeList gs = getGroups();
    for (int i = 0, len = gs.getLength(); i < len; i++) {
      Element s = gs.getItem(i).getChildNodes().getItem(2).cast();
      El.fly(s).setWidth(tw);
      if (s.getFirstChild() == null) return;
      El.fly(s.getFirstChildElement()).setWidth(tw);
      TableSectionElement tse = s.getFirstChildElement().cast();
      NodeList cells = tse.getRows().getItem(0).getChildNodes().cast();
      for (int j = 0, wlen = ws.size(); j < wlen; j++) {
        El.fly(cells.getItem(j)).setWidth(ws.get(j));
      }
    }
  }

  @SuppressWarnings({"unchecked", "rawtypes"})
  @Override
  protected void doGroupEnd(StringBuilder buf, GroupColumnData g, List cs, int colCount) {
    Map data = calculate(g.models, cs);
    buf.append("
"); buf.append(renderSummary(data, cs)); buf.append("
"); } protected void doWidth(int col, int w, int tw) { if (!enableGrouping) return; NodeList gs = getGroups(); for (int i = 0, len = gs.getLength(); i < len; i++) { Element s = gs.getItem(i).getChildNodes().getItem(2).cast(); El.fly(s).setWidth(tw); El.fly(s.getFirstChildElement()).setWidth(tw); TableSectionElement tse = s.getFirstChildElement().cast(); Element e = tse.getRows().getItem(0).getChildNodes().getItem(col).cast(); El.fly(e).setWidth(w); } } @Override protected void initTemplates() { super.initTemplates(); if (rowTpl == null) { StringBuffer sb = new StringBuffer(); sb.append("
"); sb.append(""); sb.append("{cells}"); sb.append("
"); rowTpl = new Template(sb.toString()); rowTpl.compile(); } if (cellTpl == null) { StringBuffer sb = new StringBuffer(); sb.append(""); sb.append("
{value}
"); sb.append(""); cellTpl = new Template(sb.toString()); cellTpl.compile(); } } @Override protected void onRemove(ListStore ds, ModelData m, int index, boolean isUpdate) { super.onRemove(ds, m, index, isUpdate); if (!isUpdate) { String groupField = getGroupField(); refreshSummary(groupField, getGroup(m.get(groupField), m, index, cm.findColumnIndex(groupField), ds)); } } @Override protected void onUpdate(ListStore store, ModelData model) { super.onUpdate(store, model); String groupField = getGroupField(); if (groupField != null) { refreshSummary(groupField, getGroup(model.get(groupField), model, -1, cm.findColumnIndex(groupField), ds)); } } protected void refreshSummary(String groupField, String group) { Element g = XDOM.getElementById(getGroupId(grid.getId(), groupField, group)); if (g == null) { return; } List models = grid.store.findModels(groupField, group); List cs = getColumnData(); Map data = calculate(models, cs); String markup = renderSummary(data, cs); El existing = getSummaryNode(g); if (existing != null) { g.removeChild(existing.dom); } DomHelper.append((com.google.gwt.user.client.Element) g, markup); } @SuppressWarnings("rawtypes") protected String renderSummary(Map data, List cs) { StringBuilder buf = new StringBuilder(); int last = cs.size() - 1; for (int i = 0, len = cs.size(); i < len; i++) { ColumnData c = cs.get(i); SummaryColumnConfig cf = (SummaryColumnConfig) cm.getColumn(i); Params p = new Params(); p.set("id", c.id); p.set("style", c.style); String css = i == 0 ? "x-grid3-cell-first " : (i == last ? "x-grid3-cell-last " : ""); p.set("css", css); if (cf.getSummaryFormat() != null) { p.set("value", cf.getSummaryFormat().format(((Number) data.get(c.name)).doubleValue())); } else if (cf.getSummaryRenderer() != null) { p.set("value", cf.getSummaryRenderer().render(data.get(c.name), data)); } else { p.set("value", data.get(c.name)); } buf.append(cellTpl.applyTemplate(p)); } Params rp = new Params(); rp.set("tstyle", "width:" + getTotalWidth() + "px;"); rp.set("cells", buf.toString()); return rowTpl.applyTemplate(rp); } @Override protected void templateOnAllColumnWidthsUpdated(List ws, int tw) { super.templateOnAllColumnWidthsUpdated(ws, tw); doAllWidths(ws, tw); } @Override protected void templateOnColumnHiddenUpdated(int col, boolean hidden, int tw) { if (!enableGrouping) return; NodeList gs = getGroups(); String display = hidden ? "none" : ""; for (int i = 0, len = gs.getLength(); i < len; i++) { Element s = gs.getItem(i).getChildNodes().getItem(2).cast(); El.fly(s).setWidth(tw); El.fly(s.getFirstChildElement()).setWidth(tw); TableSectionElement tse = s.getFirstChildElement().cast(); Element e = tse.getRows().getItem(0).getChildNodes().getItem(col).cast(); e.getStyle().setProperty("display", display); } } @Override protected void templateOnColumnWidthUpdated(int col, int w, int tw) { super.templateOnColumnWidthUpdated(col, w, tw); doWidth(col, w, tw); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy