-
FEATURED COMPONENTS
First time here? Check out the FAQ!
How implement the ZK Pivottable component in the pattern MVVM?
Good one. I just got mine working with MVVM yesterday. I'm happy to share my simple example with you:
ZUL
<?xml version="1.0" encoding="UTF-8"?> <?page automaticTimeout="false"?> <zk> <window border="normal" closable="true" mode="overlapped" title="Location Totals by Date" height="575px" width="950px"> <style src="assets/css/Customize.css" ></style> <vlayout apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('com.soc.ds.zk.viewmodel.LocationBatchesPivotViewModel')" height="100%" width="100%"> <pivottable model="@bind(vm.pivotModel)" pivotRenderer="@bind(vm.pivotRenderer)" grandTotalForColumns="@load(vm.grandTotalForColumns)" grandTotalForRows="@load(vm.grandTotalForRows)" ></pivottable> </vlayout> </window> </zk>
View Model
package com.soc.ds.zk.viewmodel; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.zkoss.bind.annotation.ExecutionArgParam; import org.zkoss.bind.annotation.Init; import org.zkoss.pivot.PivotField; import org.zkoss.pivot.PivotRenderer; import org.zkoss.pivot.Pivottable; import org.zkoss.pivot.impl.TabularPivotModel; import com.soc.ds.model.LocationBatch; import com.soc.ds.pivot.LocationBatchesPivotRenderer; import com.soc.ds.pivot.SimplePivotConfigurator; public class LocationBatchesPivotViewModel { private LocationBatchesPivotRenderer renderer; private List<LocationBatch> locationBatchList; private TabularPivotModel pivotModel; public LocationBatchesPivotViewModel() { super(); renderer = new LocationBatchesPivotRenderer(); } @Init public void init(@ExecutionArgParam("locationBatchList") List<LocationBatch> locationBatchList) { this.locationBatchList = locationBatchList; } public TabularPivotModel getPivotModel() { LocationBatch batch = null; Object[][] objs = new Object[locationBatchList.size()][5]; List<List<Object>> pivotModelList = new ArrayList<List<Object>>(); for (int i = 0; i < locationBatchList.size(); i++) { batch = locationBatchList.get(i); objs<i >[0] = batch.getLocationNumberAndName(); objs<i >[1] = batch.getBatchPostDate(); objs<i >[2] = batch.getGross(); objs<i >[3] = batch.getDiscount(); objs<i >[4] = batch.getNet(); } for (Object[] o : objs) { pivotModelList.add(Arrays.asList(o)); } pivotModel = new TabularPivotModel(pivotModelList, getColumns()); LOCATION_PERFORMANCE.configure(pivotModel); return pivotModel; } public PivotRenderer getPivotRenderer() { return renderer; } public static List<String> getColumns() { return Arrays.asList(new String[]{"Location", "Date", "Gross", "Discount", "Net"}); } public boolean getGrandTotalForColumns() { return false; } public boolean getGrandTotalForRows() { return true; } public static final SimplePivotConfigurator[] getConfigurators() { return new SimplePivotConfigurator[] {LOCATION_PERFORMANCE}; } public static final SimplePivotConfigurator LOCATION_PERFORMANCE = new SimplePivotConfigurator("Location Performance") { public void configure(TabularPivotModel model) { model.setFieldType("Gross", PivotField.Type.DATA); model.setFieldType("Discount", PivotField.Type.DATA); model.setFieldType("Net", PivotField.Type.DATA); model.setFieldType("Location", PivotField.Type.ROW); model.setFieldType("Date", PivotField.Type.COLUMN); } public void configure(Pivottable table) { table.setDataFieldOrient("row"); } }; }
Pivot Table Renderer
package com.soc.ds.pivot; import java.text.DecimalFormat; import org.zkoss.pivot.Calculator; import org.zkoss.pivot.PivotField; import org.zkoss.pivot.PivotHeaderContext; import org.zkoss.pivot.PivotRendererExt; import org.zkoss.pivot.Pivottable; public class LocationBatchesPivotRenderer implements PivotRendererExt { private DecimalFormat _fnf = new DecimalFormat("##,###.00"); private DecimalFormat _nnf = new DecimalFormat("##,###"); @Override public String renderCell(Number data, Pivottable table, PivotHeaderContext rowContext, PivotHeaderContext columnContext, PivotField dataField) { return data == null ? "" : data instanceof Integer ? _nnf.format(data) : _fnf.format(data); } @Override public String renderField(Object data, Pivottable table, PivotField field) { return field.getType() == PivotField.Type.DATA ? field.getTitle() : data == null ? "(null)" : String.valueOf(data); } @Override public String renderDataField(PivotField field) { return field.getFieldName(); } @Override public String renderSubtotalField(Object data, Pivottable table, PivotField field, Calculator calculator) { String calLabel = calculator.getLabel(); String dataLabel = data == null ? "Null" : data.toString(); return dataLabel + " " + calLabel; } @Override public String renderGrandTotalField(Pivottable table, PivotField field) { if (field == null) return "Grand Total"; return "Grand Total of " + field.getTitle(); } @Override public int getColumnSize(Pivottable table, PivotHeaderContext columnContext, PivotField field) { return field.getFieldName().equals("Location") ? 300 : 100; } @Override public int getRowSize(Pivottable table, PivotHeaderContext rowContext, PivotField field) { return 20; } @Override public String renderCellSClass(Number data, Pivottable table, PivotHeaderContext rowContext, PivotHeaderContext columnContext, PivotField dataField) { return null; } @Override public String renderCellStyle(Number data, Pivottable table, PivotHeaderContext rowContext, PivotHeaderContext columnContext, PivotField dataField) { return null; } }
Configurator
package com.soc.ds.pivot; import org.zkoss.pivot.PivotRenderer; import org.zkoss.pivot.Pivottable; import org.zkoss.pivot.impl.TabularPivotModel; public class SimplePivotConfigurator { private final String title; public SimplePivotConfigurator(String title) { this.title = title; } public void configure(TabularPivotModel model) { } public void configure(Pivottable table) { } public String getTitle() { return title; } public PivotRenderer getRenderer() { return null; } }
Calling View Model (vm that creates pivot table)
public void showPivot() { Map args = new HashMap(); args.put("locationBatchList", locationBatchList); Window w = (Window) Executions.createComponents("/batches/pivot.zul", null, args); w.setMode(Mode.OVERLAPPED); w.setPosition("center"); w.doOverlapped(); }
Asked: 2012-08-13 16:00:09 +0800
Seen: 234 times
Last updated: Aug 15 '12