2 Replies Latest reply on Feb 28, 2008 12:53 PM by Rulin Sun

    scrollableDataTable selection after sorting

    Claus Nyhus Newbie


      I cannot figure out how to get the data for the selected row in a scrollableDataTable after I have sorted one of the columns.

      The problems can be seen in the Richfaces Demo application too.
      If you go to: http://livedemo.exadel.com/richfaces-demo/richfaces/scrollableDataTable.jsf and select the first row, and the press the "Show Current Selection" the correct row is shown in the panel. Now click the header of the "Model" column so "Toyota 4-runner" is the first row. Press the "Show Current Selection" button again, and the panel will still show the Corvette, i.e. the first data object in the list before the table got sorted.

      So using the selection attribute of the scrollableDataTable gives me the correct int value for the selected row in the table, but I cannot use this to lookup the correct data in the list backing the table, since this list is not sorted (looking at the code for the backing bean for the example in the demo application this is also what goes wrong here).

      Am I using the component in a wrong way, or has anyone figured out a way of handeling this senario - it kind of breaks the whole purpose of having a component which handles sorting for you :)
      Maybe there is some way I kan get a hold on the sorted list used internally by the component ?

      Hope someone can help.


        • 2. Re: scrollableDataTable selection after sorting

          There is workaround to this issue. Actually, I think the way it was used in demo was not right for the selection. Codes to make it works:

          In the backbean define:
          private HtmlScrollableDataTable selectTable; // binding in scrollableDataTable
          private SimpleSelection selection = new SimpleSelection();

          private int selectData() {
          if(getDataList()==null)return 0;
          // this fix the richFaces bug in case of only one row, selection doesn't work
          return 1;
          Iterator keys = getSelection().getKeys();
          while(keys.hasNext()) {
          SimpleRowKey key = keys.next();
          if(getSelectedData()==null) return 0;
          return getSelection().size();

          For multiple selections, change setSelectedData() to getSelectedData().add(). In your action/actionListener to process selectedData, call selectData() first.

          But I do find that in order to make sorting, selection, scrolling working, the dataList of the table has to be static data (like all cars in demo), or the bean to hold list has to be in the session scope. If the datalist is retrieved based on the request parameter in the form, it will not work. When I debug, some ajax calls for scrolling, sorting happen before form fields been populated.