0

I have the following problem: I want to order my list of datatables with the sorting functionality of datatable, but the problem is that I don't know the number of datatables if not at runtime.

<p:tabView>
    <ui:repeat>
      ....
        <p:dataTable var="value" value="#{myCtrl.getValues(thisTable)}">
                <p:column headerText="MyClass"
                                        sortBy="#{value.name}"
                                        style="width:30%">
    ...
    </ui:repeat>
</p:tabView>

I found this response that says I should hold a List to let the datatable order my elements: Sorting is not working in datatable in PrimeFaces? But the problem is that I don't know how many lists should I create in the backing bean to hold the results of each table. Does someone know how I can approach the problem?

EDIT: The model that I use is:

@ViewScoped
public class MyCtrl implements Serializable {
    private String FIRST_TABLE = ...;
    @Inject
    private transient MyService service;

    private Map<String, MyClass> tableValues = new TreeMap<>();


    @PostConstruct
    public void postConstruct() {
         //initialize the map with a default table
         tableValues.put(FIRST_TABLE, service.getTable(FIRST_TABLE);
    }

    List<MyClass> getValues(String table) {
        if(tableValues.get(table) == null) 
             tableValues.put(table, service.getTable(table));
        return tableValues.get(table);
    }
...
}

EDIT 2: The problem is, when I click on the first column of one of the various tables that I created, defined as MyClass.name, it performs no sort, even if it calls again the method myCtrl.getValues(String table).

Primefaces version: 6.1

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Paolof76
  • 889
  • 1
  • 9
  • 23
  • What's the problem with your current implementation? The showcase shows a [similar example](https://www.primefaces.org/showcase/ui/data/datatable/sort.xhtml) to yours, except that you've got a method returning the values for each table. There's one bean managing the three sortable data tables. – Aritz Sep 17 '18 at 13:14
  • @XtremeBiker No, in that example there are three Lists at compile time in the backing bean... in my case myCtrl.getValues(thisTable) get a list from an hashmap > that is the model, but how can I let primefaces change the sort order of the lists in my map? – Paolof76 Sep 17 '18 at 13:54
  • Again, what's the problem with your current impl? Do you have any error at client or server side? Which PF version? – Aritz Sep 17 '18 at 13:58
  • @XtremeBiker The problem is, the tables are not sorted when I click on the header... – Paolof76 Sep 17 '18 at 14:04
  • 1
    As a rule, use `c:forEach` when working with `p:dataTable`: https://forum.primefaces.org/viewtopic.php?t=1336 – Aritz Sep 17 '18 at 14:13
  • It doesn't work, because I use it within a tabView. https://stackoverflow.com/questions/22086382/cforeach-not-iterating-through-list-in-a-tabview Sorry if I give bits of information every time, but I cannot post the company code so as it is. Anyway, I think this has nothing to do with the issue. I think I should use some sort of custom model but I don't understand how... Primefaces version: 6.1 – Paolof76 Sep 17 '18 at 14:20
  • 1
    _"Sorry if I give bits of information every time, but I cannot post the company code so as it is."_ Keep in mind that if, because of this, it starts taking to much time to try to help, it might be that help stops. And the link you post about `c:foreach` in relation to a tab is trange... That answer does not seem valid. A `ui:repeat` is not working inside a tabview if the tabs are within the repeat!... [mcve] please otherwise help really becomes too difficult. – Kukeltje Sep 17 '18 at 15:01
  • 1
    @XtremeBiker You say "A ui:repeat is not working inside a tabview if the tabs are within the repeat!". Sorry but for me is true the opposite,I can't let the foreach work, instead I had to use the ui:repeat. Maybe because ui:repeat is a JSF component anf foreach not... (https://stackoverflow.com/questions/3493395/recursion-in-jsf-cforeach-vs-uirepeat) The problem was that within the foreach I didn't get the variable from the list given to the foreach. With ui:repeat it works.... – Paolof76 Sep 18 '18 at 06:38
  • 1
    I understand you cannot post the company's code. So make it easier, as @Kukeltje says, post a MCVE that you have tested just for the question, then we'll be able to help you. I cannot have your code tested as you've posted it. – Aritz Sep 18 '18 at 06:43
  • You can post any reference to questions telling to use `ui:repeat` in `p:tabview`. Most are wrongly answered... A `ui:repeat` is not supposed to work in a tabview whichmany others that e.g. tell you. A ui:repeat also does not work for adding columns in a datatable andmore. A `c:foreach` can be used for this. If you have a problem with the variable, ask about that (maybe using the index is better). And you hopefully know (from the docs and showcase) that the tabview can be backed by a datamodel, espcially for this – Kukeltje Sep 18 '18 at 07:04

0 Answers0