6 Replies Latest reply on Aug 29, 2012 11:16 AM by Sascha Janz

    get sorted data out of datatable

    steve reinders Newbie

      I have a rich:datatable with multiple columns and some are sortable. I'm using the sort ability in data table itself not sort routines in the backing bean.

      It works fine.

       

      User wants to print out the table data with the sort applied.

       

      Using a debugger, I can see the sorted data (currentModel and rowKeys[]) but I can't get at it; no obvious method.  I've looked through UIComponent and UIDataTable.

       

      For UIDataTable I tried using getDataModel but compiler returns "Error(208,22):  getDataModel() has protected access in org.ajax4jsf.component.SequenceDataAdaptor "

       

      Any ideas how to do get the sorted data ?  If I can get at currentModel then use rowKeys[] to write the output I s/b OK.

       

      Thanks ...

        • 1. Re: get sorted data out of datatable
          steve reinders Newbie

          Essentially gave up.  Was able to see the sorted rowKeys in _modelMaps on the server side but could not access .

          Got the source distribution and tried altering the source but maven had build errors and I didn't bother with trying to track that down.  Couldn't find a dependacy list.

           

          Ended up having javascript go after the sorted table in the DOM ( form1:DataTableId:tb ) and passing that through h:inputHidden to the server side for processing.

          • 2. Re: get sorted data out of datatable
            jbosscommunity13 Newbie

            Is there not another way of getting the sorted keys in your hands?

            Don't want to build JS to solve something that is already there on server side.

             

            Anybody out there got an solution?

            • 3. Re: get sorted data out of datatable
              Sascha Janz Master

              take a look at

               

              https://community.jboss.org/wiki/HowToGetFilteredDataFromTableToList

               

              it helped me getting sorted data from extendeddatatable

              • 4. Re: get sorted data out of datatable
                jbosscommunity13 Newbie

                Hi Sascha,

                 

                I have found the same Link in the internet, but it explains ho to get FILTERED data, I need access to the SORTED data.

                 

                I did it by reflexion:

                          UIExtendedDataTable edt = (UIExtendedDataTable)FacesContext.getCurrentInstance().getViewRoot().findComponent(componentId);
                          if(edt != null) {
                               int index0Based = edt.getFirst();
                               
                               Field privateExtendedDataModelField;
                               try {
                                    
                                    privateExtendedDataModelField = UIDataAdaptor.class.getDeclaredField("extendedDataModel");
                                    if(privateExtendedDataModelField != null) {
                                         privateExtendedDataModelField.setAccessible(true);
                                         
                                         Object o = privateExtendedDataModelField.get(edt);
                                         
                                         /** SORTING */     
                                         if(o instanceof org.richfaces.model.ArrangeableModel){
                                              org.richfaces.model.ArrangeableModel model = (org.richfaces.model.ArrangeableModel)o;
                                              if(model != null){
                                                   Field rowKeysField = org.richfaces.model.ArrangeableModel.class.getDeclaredField("rowKeys");
                                                   rowKeysField.setAccessible(true);
                                                   
                                                   List rowKeys = (List)rowKeysField.get(model);
                                                   
                                                   // SORTED LIST EXIST
                                                   if(rowKeys != null && rowKeys.size() > 0){
                [...]
                
                

                 

                its pretttttyyyyy ugly, but it works for now, for more elegant solutions, bring them up!

                • 5. Re: get sorted data out of datatable
                  jbosscommunity13 Newbie

                  how didi it help you?

                  • 6. Re: get sorted data out of datatable
                    Sascha Janz Master

                    i changed to the ModifiableModel

                     

                    wrote this simple function

                     

                     

                     

                     

                     

                     

                     

                     

                     

                    publicList<Data> getSortedData(final ModifiableModel model) throws IOException {

                     

                    finalList<Data> filteredData = new ArrayList<Data>();

                     

                     

                    model.walk(FacesContext.getCurrentInstance(),

                    new

                    DataVisitor() {

                     

                    public void

                    process(FacesContext context, Object rowKey, Object argument) {

                    model.setRowKey((String)rowKey);

                     

                    if ( model.getRowData()!= null

                    ) {

                    filteredData.add((Data) model.getRowData());

                    }

                    }

                    },

                    new SequenceRange(0, -1), null

                    );

                     

                     

                    return filteredData;

                    }

                     

                    In the bean where i needed  the sorted data

                     

                     

                     

                    List<Data> hitlist = getSortedData( myBean.getDataModel());

                     

                    sure, not that simple. i never would have found this out without the tips from link above