answered
2016-01-06 14:41:48 +0800
WilliamB 1609 ● 1 ● 6 Asked our paid support and this is the answer I got :
"I believe that the easiest way to implement the described behavior would be to add a check to the custom grid attribute on the client side before sending the page changing event.
I have attached a simple example to illustrate a possible solution.
A custom grid is created, with a "_forbidNavigation" boolean attribute.
When the attribute is changed, the widget is notified by smartUpdate, to make the state available on the client side.
The paging widget $class.go(...) method (called to trigger onPaging) is overridden to check if its parent (the grid) have the _forbidNavigation:true state. If not, the original method is called. If the grid _forbidNavigation widget attribute is set to true, then a custom event is sent, and can be handled on server side in an event listener.
I created the event handler on the custom grid for simplicity, but in a real-world solution, it would probably be defined at composer level if using MVC."
With those files :
Zul :
<zk>
<zscript>
public ListModelList myModel = new ListModelList(Locale.getAvailableLocales());
</zscript>
<script>
<![CDATA[
zk.afterLoad('zul.mesh', function() {
var _xPaging = {};
zk.override(zul.mesh.Paging.prototype.$class, _xPaging, {
go: function (anc, pgno, inp) {
var wgt = zk.Widget.isInstance(anc) ? anc : zk.Widget.$(anc);
//if() statement to block navigation if Grid._forbidNavigation is true.
if(!wgt.parent._forbidNavigation){
//apply original method
_xPaging.go.apply(this,arguments);
//If navigation forbidden, send onForbidNavigation event.
}else{
wgt.fire('onForbidNavigation');
zAu.send(new zk.Event(wgt.parent, "onForbidNavigation", null, {toServer:true}));
}
}
});
});
]]>
</script>
<window>
<grid id="grid" model="${myModel}" mold="paging" autopaging="true" use="local.zkoss.support.Cgi.support2736.CustomGrid"/>
<hlayout>
<button label="Toggle Forbid Navigation"
onClick="grid.set_forbidNavigation(!grid.get_forbidNavigation());statusLabel.setValue(grid.get_forbidNavigation().toString())" />
<label value="navigation forbidden: "/><label id="statusLabel"/>
</hlayout>
</window>
</zk>
Component :
package local.zkoss.support.Cgi.support2736;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Messagebox;
public class CustomGrid extends Grid {
private Boolean _forbidNavigation = false;
private Messagebox myMessagebox = new Messagebox();
public Boolean get_forbidNavigation() {
return _forbidNavigation;
}
public void set_forbidNavigation(Boolean _forbidNavigation) {
this._forbidNavigation = _forbidNavigation;
//send state to widget
smartUpdate("_forbidNavigation", _forbidNavigation);
}
@Override
public void onInitRender() {
super.onInitRender();
this.addEventListener("onForbidNavigation", new EventListener<Event>() {
public void onEvent(Event event) throws Exception {
myMessagebox.show("navigation forbidden");
}
});
}
}
Now I can't get this code to work by moving the CDATA code inside my component with
getPagingChild().setWidgetOverride().
I need all the code to be in my component java file not in the zul.
Thanks for your help