12 Replies Latest reply on Feb 12, 2009 2:25 PM by Max Laenzlinger

    How to get the DataTable sort state ?

    Flavio Henrique Newbie

      hi there.
      I'm using r:dataTable with javax.faces.model.DataModel as wrappedData.

      I'm wondering if it's possible to get, in backing bean, the dataTable sort state... meaning which columns get clicked to sort the data ?

      I would like to know if there's an elegant way to do this (without firing onclick events).

      Thank you in advance.

        • 1. Re: How to get the DataTable sort state ?
          Konstantin Mishin Novice

          You might create own model and use it as value of dataTable. This model should extend org.ajax4jsf.model.ExtendedDataModel or its subclasses (I think you should use org.ajax4jsf.model.SequenceDataModel in your case) and implement org.richfaces.model.Modifiable. The information interesting you can be received in method modify. See org.richfaces.model.ModifiableModel for more info.

          • 2. Re: How to get the DataTable sort state ?
            Max Laenzlinger Newbie

            Hi Hwoarang,

            Does it work? I need to show the sorted Table afterwards in a pdf. So i'm wondering if the data/table is sorted for easy reuse in the jasper-report. I'm afraid not, I guess its as idea i get this sort information and resort a table for the jasper; if anyone sees a better solution many thanks.

            • 3. Re: How to get the DataTable sort state ?
              Flavio Henrique Newbie

               

              "maxmustang" wrote:
              Hi Hwoarang,

              Does it work? I need to show the sorted Table afterwards in a pdf. So i'm wondering if the data/table is sorted for easy reuse in the jasper-report. I'm afraid not, I guess its as idea i get this sort information and resort a table for the jasper; if anyone sees a better solution many thanks.


              Hi Max,
              unfortunately I did not try yet. I couldn't figure out how to use those interfaces/classes... still reading about...
              I'm trying to do exactly what you do, to print the dataTable records in the same order that appears on screen without re-asking the database.
              If I got success with this I'll let you know.
              Cya!

              Hwoarang

              • 4. Re: How to get the DataTable sort state ?
                Max Laenzlinger Newbie

                Hi Hwoarang and konstantin,

                I use your solution, as you told, the method modifyList gets called once when i click a sorted row-header.

                Within that method i can see the selected field (field.getExpression()) and the ordering (Field.getOrdering()). But: Unfortunately the gui does not update any more...

                Greetings from Zurich Max

                ---------------------------------------------------------------

                Hwoarang here is my code for my model 'MonitoringDataModel':

                ...
                import javax.faces.model.DataModel;
                import org.ajax4jsf.model.SequenceDataModel;
                import org.richfaces.model.FilterField;
                import org.richfaces.model.Modifiable;
                import org.richfaces.model.SortField2;
                ...

                public class MonitoringDataModel extends SequenceDataModel implements Modifiable {

                public MonitoringDataModel(DataModel wrapped) {
                super(wrapped);
                // TODO Auto-generated constructor stub
                }

                @Override
                public void modify(List arg0, List<SortField2> arg1) {
                // TODO Auto-generated method stub

                Iterator<SortField2> fieldIt = arg1.iterator();
                while ( fieldIt.hasNext() )
                {

                SortField2 field = fieldIt.next();
                log.debug("field.getExpression().getExpressionString="+field.getExpression().getExpressionString()); log.debug("Field.getOrdering().name="+field.getOrdering().name());

                }

                • 5. Re: How to get the DataTable sort state ?
                  Max Laenzlinger Newbie

                  .. actually, i'm much less interested in the ordered row - i just need a table sorted in the same way as in the gui, can i get this out of this SequenceDataModel?? Why could my gui not be updated any more? (The logging shows me correct clicked rows and sortings the data is correct in the gui but its not updated (sorted any more there))

                  • 6. Re: How to get the DataTable sort state ?
                    Konstantin Mishin Novice

                    Hi Max.
                    You also might use attribute "sortOrder" of rich:column and attribute "sortPriority" of rich:dataTable. Or you might extend ModifiableModel instead of SequenceDataModel and override method modify.

                    • 7. Re: How to get the DataTable sort state ?
                      Max Laenzlinger Newbie

                      Hi konstantin,

                      Thanks for your help. I used sortOrder already, ModifiableModel also does not update my gui table.

                      I found a solution, as i just need the gui table as it is sorted in the gui, i read from the gui the table content. Easy. It gives me back the sorted Data, code below.

                      (Hwoarang: If sorted column info is needed, HtmlDataTable provides .getSortPriority(), or go on with trying solution of konstantin maybe i made something wrong that my gui not updates any more)

                      Thanks to all for help, Max

                      ------------------------------------------------------------------------------------

                      Getting sorted Data from the GUI:

                      import org.richfaces.component.html.HtmlDataTable;

                      HtmlDataTable personsTable = (HtmlDataTable)FacesContext.getCurrentInstance().getViewRoot().findComponent("form1:personen");
                      for (int i = 0; i < personsTable.getRowCount(); i++) {
                      //loop over the rows
                      personsTable.setRowIndex(i);
                      //get the values from row and add it a Person-Bean
                      Person person = (Person) myTable.getRowData();
                      // Add the person to a List for further needs (in my case a pdf)
                      persons.add()... etc.

                      • 8. Re: How to get the DataTable sort state ?
                        Konstantin Mishin Novice

                        Hi Max.
                        Maybe you didn't call method of super class when overrode method modify.

                        • 9. Re: How to get the DataTable sort state ?
                          Max Laenzlinger Newbie

                          Unfortunately there is no super method to call.
                          My solution works to get the actual table content inclusive sorting, but when i rerender my table with other persons, and get the values via HtmlDataTable (by findComponent or binding) i always get the values from the table before the rerender... mm

                          • 10. Re: How to get the DataTable sort state ?
                            Max Laenzlinger Newbie

                            ok works fine for me with mentioned richfaces.component.html .HtmlDataTable. It was my fault, my controller was request instead session ..:-)

                            • 11. Re: How to get the DataTable sort state ?
                              Flavio Henrique Newbie

                              Max, thanks a lot for your code.
                              I have your previous problem now: the table gui doesn't update.
                              My bean is session mode, so what you did to work this out?

                              In my case, I need to update the gui too.

                              Thank you.

                              • 12. Re: How to get the DataTable sort state ?
                                Max Laenzlinger Newbie

                                Hi,

                                I dont use SequenceDataModel nor ModifiableModel, with them my gui did not update any more; for the rows for the table i use just normal Type DataModel:

                                public DataModel getPersonen()

                                DataModel data = new ListDataModel();
                                data.setWrappedData( persons ); // (persons are normal ArrayList of Type (Person))
                                return data;

                                }

                                This way the Gui does not updating/ordering the data, i get the sorted data from the Gui via HtmlDataTable as soon as i need it for my pdf... Works perfecly also with sorting=multi columns.