当ombobox onchange事件触发时,我需要在ZK数据透视表中重新加载数据。
如果用户更改了comobox中的值{如下面的代码所示},数据应根据用户选择进行更改。
index.zul
<window apply="org.zkoss.pivot.demo.PivotDemoBaseController" >
<hlayout>
<panel id="main" hflex="1" border="normal">
<caption label="2012 Data">
<toolbarbutton id="exportCsvBtn" label="Export CSV" />
<toolbarbutton id="exportXlsBtn" label="Export XLS" />
<toolbarbutton id="exportXlsxBtn" label="Export XLSX" />
</caption>
<panelchildren>
<vlayout spacing="0">
<pivottable id="pivot" hflex="1" pageSize="15" >
<combobox model="${lm}" id="selectGeo"/>
<div>All People</div>
</pivottable>
<div id="descDiv" />
</vlayout>
</panelchildren>
</panel>
<panel id="field" title="Control" width="300px" border="normal" >
<panelchildren>
<vlayout style="padding: 10px">
<!-- Predefined scenario: -->
<hlayout id="preDef" spacing="0" />
<div class="footnote" style="padding: 5px 0">(Drag fields among the areas below)</div>
<pivot-field-control id="pfc" height="300px" />
<hlayout hflex="1">
<checkbox id="autoUpdate" label="Auto Update" checked="true" />
<div hflex="1" />
<!-- <button id="updateBtn" label="Update" disabled="true" autodisable="+self" /> -->
</hlayout>
<separator />
<checkbox id="colGrandTotal" label="Enable grand total for columns" />
<checkbox id="rowGrandTotal" label="Enable grand total for rows" />
<div>
<radiogroup id="dataOrient">
Data field orientation:
<radio id="colOrient" label="column" />
<radio id="rowOrient" label="row" />
</radiogroup>
</div>
</vlayout>
</panelchildren>
</panel>
</hlayout>
</window>
以下是我的控制器的代码
public class PivotDemoBaseController扩展了SelectorComposer {
private static final long serialVersionUID = -7531153593366258488L;
private static final String[] TITLES = new String[] { "(Data Title)", "(Column Title)", "(Row Title)" };
@Wire
private Pivottable pivot;
@Wire
private PivotFieldControl pfc;
@Wire
private Button updateBtn;
@Wire
private Checkbox colGrandTotal, rowGrandTotal;
@Wire
private Radio colOrient, rowOrient;
@Wire
private Hlayout preDef;
@Wire
private Div descDiv;
private TabularPivotModel _model;
@Wire
private Combobox selectGeo;
private CellStyleConfigurator styleConfigurator;
public void onCheck$autoUpdate(CheckEvent event) {
boolean deferred = !event.isChecked();
pfc.setDeferredUpdate(deferred);
if (!deferred)
updateBtn.setDisabled(true);
}
@Listen("onChange = #selectGeo")
public void onChangeSelectGeo(Event event) {
String geography = selectGeo.getValue();
System.out.println("Value---"+geography);
}
public void onClick$updateBtn() {
pfc.update();
}
public void onPivotFieldControlChange$pfc() {
if (!pfc.isUpdated())
updateBtn.setDisabled(false);
}
public void onCheck$colGrandTotal(CheckEvent event) {
System.out.println("PivotDemoBaseController.onCheck$colGrandTotal()");
pivot.setGrandTotalForColumns(event.isChecked());
}
public void onCheck$rowGrandTotal(CheckEvent event) {
pivot.setGrandTotalForRows(event.isChecked());
}
public void onCheck$dataOrient(CheckEvent event) {
pivot.setDataFieldOrient(((Radio)event.getTarget()).getLabel());
}
public void onClick$exportCsvBtn() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PivotExportContext context = Exports.getExportContext(pivot, true, TITLES);
Exports.exportCSV(out, context);
Filedownload.save(out.toByteArray(), "text/csv", "pivot.csv");
try {
out.close();
} catch (IOException e) {}
}
public void onClick$exportXlsBtn() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PivotExportContext context = Exports.getExportContext(pivot, true, TITLES);
Exports.exportExcel(out, "xls", context, styleConfigurator);
Filedownload.save(out.toByteArray(), "application/vnd.ms-excel", "pivot.xls");
try {
out.close();
} catch (IOException e) {}
}
public void onClick$exportXlsxBtn() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PivotExportContext context = Exports.getExportContext(pivot, true, TITLES);
Exports.exportExcel(out, "xlsx", context, styleConfigurator);
Filedownload.save(out.toByteArray(), "application/vnd.ms-excel", "pivot.xlsx");
try {
out.close();
} catch (IOException e) {}
}
@NotifyChange("*")
@Override
public void doAfterCompose(Component comp) throws Exception {
System.out.println("PivotDemoBaseController.doAfterCompose()");
super.doAfterCompose(comp);
StaticPivotModelFactory pmf = StaticPivotModelFactory.INSTANCE;
//PivotModelFactory pmf= (PivotModelFactory) arg.get("factory");
_model = pmf.build();
pivot.setModel(_model);
pfc.setModel(_model);
Executions.createComponents(pmf.getDescriptionURI(), descDiv, null);
loadConfiguration(pmf.getDefaultConfigurator());
// load predefined scenario
for(PivotConfigurator conf : pmf.getConfigurators())
preDef.appendChild(getPreDefDiv(conf));
}
private void initControls() {
System.out.println("PivotDemoBaseController.initControls()");
// grand totals
colGrandTotal.setChecked(pivot.isGrandTotalForColumns());
rowGrandTotal.setChecked(pivot.isGrandTotalForRows());
// data orientation
("column".equals(pivot.getDataFieldOrient()) ?
colOrient : rowOrient).setChecked(true);
pfc.syncModel(); // field control
}
private Component getPreDefDiv(final PivotConfigurator conf) {
Div div = new Div();
div.setHflex("1");
div.setSclass("predef");
div.appendChild(new Label(conf.getTitle()));
div.addEventListener("onClick", new EventListener(){
public void onEvent(Event event) throws Exception {
loadConfiguration(conf);
}
});
return div;
}
private void loadConfiguration(PivotConfigurator conf) {
System.out.println("PivotDemoBaseController.loadConfiguration()");
_model.clearAllFields(true);
conf.configure(_model);
conf.configure(pivot);
pivot.setPivotRenderer(conf.getRenderer());
styleConfigurator = conf.getCellStyleConfigurator();
initControls();
}
}
非常感谢任何帮助。
答案 0 :(得分:2)
我认为你的意思是@NotifyChange注释,但它适用于你没有使用的MVVM数据绑定。
您可以切换到MVVM data binding,或者您可以像这样更新它:
@Listen("onChange = #selectGeo")
public void onChangeSelectGeo(CheckEvent event) {
// Manipulate pivot element here.
}
有关详细信息,请参阅CheckEvent和MVVM vs MVC。