Hi all,
I'm trying to use Primefaces (latest version from Subversion) to render data tables with lazy loading, pagination, dynamic columns, and sorting.
First I tried to use the <p:columns> element to get dynamic columns, but then it struck me that <p:columns> has no attributes to control the sort columns and the sort order.
Instead I decided to try to use JSTL's <c:forEach> together with <p:column> (i.e. I am simply looping through a vector of column names). This seems to work well with lazy loading (by creating a LazyDataModel), pagination, and dynamic columns. However, I simply cannot get sorting to work.
Has anyone managed to get all these things to work together? Is there some special trick that I need to know about?
Regards,
Mads
dataTable with lazy loading, dynamic columns, and sorting
-
- Prime
- Posts: 18616
- Joined: 05 Jan 2009, 00:21
- Location: Cybertron
- Contact:
Dynamic columns do not support sorting or filtering, there are feature requests in our tracker you can vote.
Thanks for the response.
Actually, I have already votes for sort/filter support for dynamic columns, so I knew about the limitation for the <p:columns> element.
However, I had hoped that I could have made a "work-around" by using JSTL's <c:forEach> together with <p:column> (without the trailing "s") -- see the following code snippet:
When I view the data table in the browser it looks perfect. However, if I click the column header in order to sort by this column it does not work.
I have found out that the problem lies in the data that is passed to the to the "load(...)" method in my lazy data model. Here's a snippet of my "load(...)" method:
(In my current implementation, I keep ALL cars in a vector called "allCars" -- of course this needs to be changed in the final implementation.)
Anyway -- when I click the column header (in order to sort), the following gets printed to STDOUT:
SortField/SortOrder:value[col]/ASCENDING
Hence, the problem is that the "SortField" is set to ""value[col]" -- if I could just get "SortField" get the value of the table name I would be happy! That would (I believe) make everything work perfectly.
Regards,
Mads
Actually, I have already votes for sort/filter support for dynamic columns, so I knew about the limitation for the <p:columns> element.
However, I had hoped that I could have made a "work-around" by using JSTL's <c:forEach> together with <p:column> (without the trailing "s") -- see the following code snippet:
Code: Select all
<c:forEach var="col" items="#{cartable.columns}">
<p:column sortBy="#{car.value[col]}" headerText="#{col}">
<h:outputText value="#{car.value[col]}" />
</p:column>
</c:forEach>
I have found out that the problem lies in the data that is passed to the to the "load(...)" method in my lazy data model. Here's a snippet of my "load(...)" method:
Code: Select all
@Override
public List<Car> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
System.out.println("SortField/SortOrder:" + sortField + "/" + sortOrder);
sort(sortField, sortOrder);
List<Car> lazyCars = new ArrayList<Car>();
for (int i = first; (i < first + pageSize && (i < allCars.size()); i++) {
lazyCars.add(allCars.elementAt(i));
}
setPageSize(pageSize);
setRowCount(allCars.size());
return lazyCars;
}
Anyway -- when I click the column header (in order to sort), the following gets printed to STDOUT:
SortField/SortOrder:value[col]/ASCENDING
Hence, the problem is that the "SortField" is set to ""value[col]" -- if I could just get "SortField" get the value of the table name I would be happy! That would (I believe) make everything work perfectly.
Regards,
Mads
Thanks for the tip. However, I have already tried to put almost ANY type of combination into the "sortBy" attribute, but I simply cannot get it to pass the column name (or some other identified that I can use to uniquely identify the selected column).
Regards,
Mads
Regards,
Mads
msh321 wrote:I have tried that also. If I put "#{col}" into the "sortBy" attribute it resolves to "col" -- see the following that is printed to STDOUT:
SortField/SortOrder:col/ASCENDING
Thanks for the tip, anyway.
Regards,
Mads
hi msh321
you find a solution to this, I have the same problem
Try this. Needs some improvement
<h:form>
<p:dataTable var="test" value="#{aBean.lazyModel}" rows="6" id="lazytable" lazy="true" rowKey="#{test.id}"
selectionMode="single" selection="#{aBean.selected}">
<f:facet name="header">
<p:commandButton type="button" onclick="initL();colD.show()" value="Add a New Column"/>
</f:facet>
<p:column rendered="#{aBean.showId}" filterBy="#{test.id}" sortBy="#{test.id}">
<f:facet name="header">
<p:commandButton actionListener="#{aBean.remCol('id')}" image="remove"
update="lazytable aCol" title="Remove Column" style="float:right"/>
ID
</f:facet>
<h:outputText value="#{test.id}"/>
</p:column>
<p:column rendered="#{aBean.showTitle}" filterBy="#{test.title}" filterMatchMode="contains" sortBy="#{test.title}">
<f:facet name="header">
<p:commandButton actionListener="#{aBean.remCol('title')}" image="remove"
update"lazytable aCol" title="Remove Column" style="float:right"/>
Naziv
</f:facet>
<h:outputText value="#{test.title}"/>
</p:column>
.............................................
</p:dataTable>
<p:remoteCommand name="initL" update="aCol" action="#{aBean.initShow()}" />
<p:dialog widgetVar="colD" header="New Column">
<h:panelGrid columns="3" id="aCol">
<h:outputText value="Column Name:" />
<h:selectOneMenu value="#{aBean.columnName}">
<f:selectItems value="#{aBean.showCols}"
var="Col" itemLabel="#{col}"
itemValue="#{col}" />
</h:selectOneMenu>
<p:commandButton value="Add" actionListener="#{aBean.addCol}"
update="lazyable aCol" oncomplete="colD.hide()"/>
</h:panelGrid>
</p:dialog>
</h:form>
/******* aBean *******/
private List<String> showCols;
private String columnName;
// rendered columns T/F
private boolean showId = false;
private boolean showTitle = true;
private boolean showYear = true;
......................................
// add getters and setters for boolean
public void addCol() {
if(columnName.equalsIgnoreCase("id")){
setShowId(true);
}else if(columnName.equalsIgnoreCase("title")){
setShowTitle(true);
}
..................................................
columnName = null;
}
public void remCol(String columnToRemove) {
if(columnToRemove.equalsIgnoreCase("id")){
setShowId(false);
}else if(columnToRemove.equalsIgnoreCase("title")){
setShowTitle(false);
}
............................................
}
public void initShow(){
showCols = new ArrayList<String>();
if(!showId){
showCols.add("id");
}
if(!showTitle){
showCols.add("title");
}
....................................
}
public List<String> getShowCols() {
if(showCols==null){
initShow();
}
return showCols;
}
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
Have fun!
<h:form>
<p:dataTable var="test" value="#{aBean.lazyModel}" rows="6" id="lazytable" lazy="true" rowKey="#{test.id}"
selectionMode="single" selection="#{aBean.selected}">
<f:facet name="header">
<p:commandButton type="button" onclick="initL();colD.show()" value="Add a New Column"/>
</f:facet>
<p:column rendered="#{aBean.showId}" filterBy="#{test.id}" sortBy="#{test.id}">
<f:facet name="header">
<p:commandButton actionListener="#{aBean.remCol('id')}" image="remove"
update="lazytable aCol" title="Remove Column" style="float:right"/>
ID
</f:facet>
<h:outputText value="#{test.id}"/>
</p:column>
<p:column rendered="#{aBean.showTitle}" filterBy="#{test.title}" filterMatchMode="contains" sortBy="#{test.title}">
<f:facet name="header">
<p:commandButton actionListener="#{aBean.remCol('title')}" image="remove"
update"lazytable aCol" title="Remove Column" style="float:right"/>
Naziv
</f:facet>
<h:outputText value="#{test.title}"/>
</p:column>
.............................................
</p:dataTable>
<p:remoteCommand name="initL" update="aCol" action="#{aBean.initShow()}" />
<p:dialog widgetVar="colD" header="New Column">
<h:panelGrid columns="3" id="aCol">
<h:outputText value="Column Name:" />
<h:selectOneMenu value="#{aBean.columnName}">
<f:selectItems value="#{aBean.showCols}"
var="Col" itemLabel="#{col}"
itemValue="#{col}" />
</h:selectOneMenu>
<p:commandButton value="Add" actionListener="#{aBean.addCol}"
update="lazyable aCol" oncomplete="colD.hide()"/>
</h:panelGrid>
</p:dialog>
</h:form>
/******* aBean *******/
private List<String> showCols;
private String columnName;
// rendered columns T/F
private boolean showId = false;
private boolean showTitle = true;
private boolean showYear = true;
......................................
// add getters and setters for boolean
public void addCol() {
if(columnName.equalsIgnoreCase("id")){
setShowId(true);
}else if(columnName.equalsIgnoreCase("title")){
setShowTitle(true);
}
..................................................
columnName = null;
}
public void remCol(String columnToRemove) {
if(columnToRemove.equalsIgnoreCase("id")){
setShowId(false);
}else if(columnToRemove.equalsIgnoreCase("title")){
setShowTitle(false);
}
............................................
}
public void initShow(){
showCols = new ArrayList<String>();
if(!showId){
showCols.add("id");
}
if(!showTitle){
showCols.add("title");
}
....................................
}
public List<String> getShowCols() {
if(showCols==null){
initShow();
}
return showCols;
}
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
Have fun!
-
- Posts: 17
- Joined: 15 Nov 2013, 16:40
is it possible to have huge datatable loading without lazyloading
how is it ?
how is it ?
-
- Information
-
Who is online
Users browsing this forum: No registered users and 23 guests