jsf 2 - Primefaces ColumnToggler does'nt work with pagination -
i've got datatable along sort, filter , columntoggler. firstly, when select , unselect column in same page fine.
can see, first column has disappeared
my problem here when go next page pagination tool , if want unselect column, displays column header , not rows can see below : initial hidden column displayed we've got gap. last column right empty , first 1 take value second column.
this datatable structure :
<p:datatable id="datatable" var="mon" value="#{x.resultquery}" first="#{datatablecontroller.first}" resizablecolumns="true" rows="20" paginator="true" paginatortemplate="{currentpagereport} {firstpagelink} {previouspagelink} {pagelinks} {nextpagelink} {lastpagelink} {rowsperpagedropdown}" rowsperpagetemplate="30,40,50" draggablecolumns="true" paginatorposition="bottom" selectionmode="single" rowkey="#{mon[4]}" > <f:facet name="header"> <p:commandbutton id="toggler" type="button" value="hide columns" icon="ui-icon-calculator" /> <p:columntoggler datasource="datatable" trigger="toggler"> <p:ajax event="toggle" listener="#{columntogglercontroller.ontoggle}" /> </p:columntoggler> </f:facet>
this columntogglercontroller :
public class columntogglercontroller implements serializable { private list<boolean> list; /** * creates new instance of columntogglercontroller */ public columntogglercontroller() { } public list<boolean> getlist() { return list; } public void setlist(list<boolean> list) { this.list = list; } @postconstruct public void init() { setlist(arrays.aslist(true, true, true, true, true, true, true, true, true, true, true)); } public void ontoggle(toggleevent e) { list.set((integer) e.getdata(), e.getvisibility() == visibility.visible); }
}
basically program based on blog : http://blog.primefaces.org/?p=3341
thanks help.
i happen encounter recently. blog works. problem draggablecolumns="true"
makes index of columns change. so, instead of list used 2 maps:
private final map<string, boolean> colvisibilitymap = new hashmap<>(); private final map<integer, string> colindexmap = new hashmap<>();
during initialization, set colindexmap , colvisibilitymap (and used id/clientid key). on column reorder, update colindexmap. , on toggle, update colvisibilitymap base on colindexmap.
public map<string, boolean> getcolvisibilitymap() { return collections.unmodifiablemap(colvisibilitymap); } private string getcolumnid(string fullid) { string[] idparts = fullid.split(":"); return idparts[idparts.length - 1]; } @postconstruct public void init() { facescontext context = facescontext.getcurrentinstance(); datatable table = (datatable) context.getviewroot().findcomponent(":form:tableid"); list<uicolumn> columns = table.getcolumns(); (int = 0; < columns.size(); i++) { final string columnid = this.getcolumnid(columns.get(i).getclientid()); colindexmap.put(i, columnid); colvisibilitymap.put(columnid, true); }); } public void oncolumnreorder(ajaxbehaviorevent e) { list<uicolumn> columns = ((datatable) e.getsource()).getcolumns(); (int = 0; < columns.size(); i++) { this.colindexmap.put(i, this.getcolumnid(columns.get(i).getclientid())); } } public void ontoggle(toggleevent e) { // if use list here, e.getdata() may not correct index due column reordering. this.colvisibilitymap.put(this.colindexmap.get((integer) e.getdata()), e.getvisibility() == visibility.visible); }
here code on jsf page:
<p:datatable id="tableid" widgetvar="tablewidgetvar" draggablecolumns="true" paginator="true" <!-- ommitted other attributes --> > <p:ajax event="colreorder" listener="#{bean.oncolumnreorder}"/> <f:facet name="header"> <p:commandbutton id="toggler" type="button" value="columns"/> <p:columntoggler datasource="tableid" trigger="toggler"> <p:ajax event="toggle" listener="#{bean.ontoggle}"/> </p:columntoggler> </f:facet> <p:column id="col1" visible="#{bean.colvisibilitymap['col1']}"> <!-- ommitted --> </p:column> <p:column id="col2" visible="#{bean.colvisibilitymap['col2']}"> <!-- ommitted --> </p:column> <!-- , on... --> </p:datatable>