1

i have issues with my lazy loaded Datatable. First of all, here the code:

 <p:tabView>
    <!-- Tabs A and B, working fine -->
    <p:tab title="C">       
        <p:commandButton value="get C" id="openC" actionListener="#{backingBean.initC}" render="cTable" update="cTable"></p:commandButton>
        <p:separator/>
        <p:dataTable id="cTable" var="cTable" value="#{backingBean.lazyC}" paginator="true" 
            paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
            rowsPerPageTemplate="5,10,15" rows="10" sortMode="multiple" lazy="true" rendered="#{backingBean.cAvailable}">
            <c:forEach var="colC" items="#{backingBean.headerAllocationC}"> 
                <p:column headerText="#{colC.header}" sortBy="#{cTable[colC.property]}">
                    <div align="center">
                        <h:outputText value="#{cTable[colC.property]}"></h:outputText>
                    </div>
                </p:column>
            </c:forEach>
        </p:dataTable>
    </p:tab>
</p:tabView>

Following my sort-Method of my Lazy-Datamodel:

@Override
public List<cBean> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String,Object> filters){
    List<cBean> data = new ArrayList<cBean>();
    if (multiSortMeta != null) {
        for (SortMeta sortMeta : multiSortMeta) {
            System.out.println("SORTFIELD:" +sortMeta.getSortField());
            System.out.println("SORTORDER:" +sortMeta.getSortOrder());
            //System.out.println("SORTFUNCTION:"+sortMeta.getSortFunction());
            System.out.println("COLUMN:" +sortMeta.getColumn());
            System.out.println("CLASS:" +sortMeta.getClass());
        }
    }
    for (cBean c : datasource) {
        boolean match = true;
        if (filters != null) {
            for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
                try {
                    String filterProperty = it.next();
                    Object filterValue = filters.get(filterProperty);
                    String fieldValue = String.valueOf(c.getClass().getField(filterProperty).get(c));
                    if (filterValue == null || fieldValue.startsWith(filterValue.toString())) {
                        match = true;
                    }
                    else {
                        match = false;
                        continue;
                    }
                } catch (Exception e) {
                    match = false;
                }
            }
        }
        if (match) {
            data.add(c);
        }
    }
    int dataSize = data.size();
    this.setRowCount(dataSize);
    if (dataSize > pageSize) {
        try {
            return data.subList(first, first+pageSize);
        } catch (IndexOutOfBoundsException e) {
            return data.subList(first, first+(dataSize%pageSize));
        }
    } else {
        return data;
    }
}

My Issue: The datatable gets rendered, and displays the data how it is supposed to do. Now I want to sort the table depending on one (or multiple) columns. The load method gets called, but the sortfield-string which is handed over to my load method is wrong (to be exact: "property]" gets printed).

As far as i understand my syntax shouldn't be wrong, as I said the display of Data is totally correct. (so the syntax works just fine with the outputTexts, but doesn't work with my sortBy-clause in p:column?!)

Is there an issue with my Syntax for primefaces-components? And why is only property] handed over, not the complete String? (i would somehow understand the situation if it was cTable[colC.property] which is handed over, but since its only the later part of the string i am utterly clueless to be honest.

Would be great if so could clear things up for me and in the best case present a workaround :)

Proph3cy
  • 173
  • 2
  • 12
  • I have never worked with dynamic columns so far, but I fear that using `c:forEach` might not be the right choice, have you tried using `p:columns` as shown in the [showcase](https://www.primefaces.org/showcase/ui/data/datatable/columns.xhtml)? – perissf Aug 21 '18 at 12:07
  • This should be helpful to explain why: https://stackoverflow.com/questions/29021036/why-can-cforeach-or-uirepeat-not-access-pdatatable-var – perissf Aug 21 '18 at 12:14

1 Answers1

0

Ok, so i have gotten it to work. Seems like the forEach was indeed the cause of trouble here.Thanks for bringing me onto the right track @perissf

Last thing to that would be interesting to understand is why it only handed over a part of the expression, but well^^

For anyone interested/facing a similar problem here is the code which is working fine for me:

DataTable:

        <p:dataTable id="cTable" var="cTable" value="#{backingbean.lazyC}" paginator="true" 
            paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
            rowsPerPageTemplate="5,10,15" rows="10" sortMode="multiple" lazy="true" rendered="#{backingBean.cAvailable}">
            <p:columns value="#{backingBean.headerAllocationC}" var="colC" sortBy="#{cTable[colC.property]}">
                <f:facet name="header">
                    <h:outputText value="#{colC.header}"/>
                </f:facet>
                <h:outputText value="#{cTable[colC.property]}"/>                    
            </p:columns>
        </p:dataTable>

LazyDataModel:

public class LazyCDataModel extends LazyDataModel<cBean>{

    private static final long serialVersionUID = 1L;

    private List<cBean> datasource;

    public LazyCDataModel (List<cBean> datasource) {
    this.datasource = datasource;
    }
    @Override
    public List<cBean> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String,Object> filters){
        List<cBean> data = new ArrayList<cBean>();
        for (cBean c : datasource) {
            boolean match = true;
            if (filters != null) {
                for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
                    try {
                        String filterProperty = it.next();
                        Object filterValue = filters.get(filterProperty);
                        String fieldValue = String.valueOf(bestand.getClass().getField(filterProperty).get(bestand));
                        if (filterValue == null || fieldValue.startsWith(filterValue.toString())) {
                            match = true;
                        }
                        else {
                            match = false;
                            continue;
                        }
                    } catch (Exception e) {
                        match = false;
                    }
                }
            }
            if (match) {
                data.add(c);
            } 
        }
        if (multiSortMeta != null) {
            for (SortMeta sortMeta : multiSortMeta) {
                if (sortMeta.getSortField() != null) {
                    Collections.sort(data, new LazyCSort(sortMeta.getSortField(),sortMeta.getSortOrder()));
                }
            }
        }
        int dataSize = data.size();
        this.setRowCount(dataSize);
        if (dataSize > pageSize) {
            try {
                return data.subList(first, first+pageSize);
            } catch (IndexOutOfBoundsException e) {
                return data.subList(first, first+(dataSize%pageSize));
            }
        } else {
            return data;  
        }
    }
}

And last my LazySorter:

public class LazyCSort implements Comparator<cBean>{
    private String sortField;
    private SortOrder sortOrder;
    public LazyBestandsSort(String sortField, SortOrder sortOrder) {
        this.sortField = sortField;
        this.sortOrder = sortOrder;
    }
    public int compare(cBean c1, cBean c2) {
        try {
            Field field1 = BestandsBean.class.getDeclaredField(this.sortField);
            Field field2 = BestandsBean.class.getDeclaredField(this.sortField);
            field1.setAccessible(true);
            field2.setAccessible(true);
            Object value1 = field1.get(bestand1);
            Object value2 = field2.get(bestand2);
            int value = ((Comparable) value1).compareTo(value2);
            return SortOrder.ASCENDING.equals(sortOrder) ? value : -1 * value;
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

Note: In the LazySorter i need to go for "cBean.class.getDeclaredField", cause my fields in the cBean are private. If you have public fields you can go with the normal "getField" as shown in the primefaces-Showcase

Proph3cy
  • 173
  • 2
  • 12
  • the sortby parsing is most likely related to https://stackoverflow.com/questions/51387450/pdatatable-sorting-without-getters-in-primefaces-4-0 – Kukeltje Aug 22 '18 at 17:13