You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by joelus <jo...@beach.co.za> on 2006/07/07 11:52:22 UTC

Selecting Row on sortable Datatable

Hi, can someone please explain the following behaviour to me: 
I have a dataTable and am using myfaces automatic sorting (although I have
the same issue using the old commandSortHeader method). For each row in the
table I have a link which is supposed to open an editable form representing
the data in the selected row. 
I have a managed bean which contains an ArrayList of data objects, this list
is passed to the datatable. The managed bean also has the sort method which
basically sorts or reverses the sort on the ArrayList. The link on each row
is an updateActionListener which passes the selected index to a method on
the managed bean which just retrieves the data object using the
arraylist.get(index) method. The problem is that there seems to be a copy of
the list which is not sorted. So if I select the item in the first row of
the datatable I get the first object in the arraylist. If I reverse the sort
on the table and select the 
-- 
View this message in context: http://www.nabble.com/Selecting-Row-on-sortable-Datatable-tf1905482.html#a5214553
Sent from the MyFaces - Users forum at Nabble.com.


Re: Selecting Row on sortable Datatable

Posted by Matthias Fischer <Ma...@doubleSlash.de>.
I'm not sure whether this helps you joelus, but I have a data table with
sort headers and a column with links that perform actions on the row.
In order to have access to the component I extended my backing bean from
HtmlDataTable and bound the component to it in the JSP.
Thus I have access to the currently clicked row and it works no matter
whether I sort the list or not.

=== Backing Bean ===
public class MyListBacking extends HtmlDataTable {

.....
   public String editAction() {

      // This index may not be very helpful    
      System.out.println("Clicked row index is " + this.getRowIndex());

      // But here I get the rows data object...
      MyRowDataObj obj = (MyRowDataObj) this.getDataModel().getRowData();
      //... in which I have a custom object id...
      System.out.println("Clicked obj id is " + obj.getId());

      // Initialize the editor backing bean with the object to edit i.e.
the clicked row's data...
      FacesContext context = FacesContext.getCurrentInstance();
      MyRowDataObjBacking editor = (MyRowDataObjBacking)
ContextHelper.getInstance().getBindingValue(
            "#{MyRowDataObjBacking}");
      editor.init(obj);
      return "success";
   }

....

=== In the JSP page ===
...
  <t:dataTable ...
      binding="#{MyListBacking }" ...>

  ....

      <h:column id="colActions">
        ...
            <t:commandLink rendered="#{user.showEditAction}"
               value="Edit" action="#{MyListBacking .editAction}" />
          ...
      </h:column>
...
   </t:dataTable>


I hope this helps...

Kind regards from the Lake of Constance,
Matthais


joelus wrote:
> Hi, can someone please explain the following behaviour to me: 
> I have a dataTable and am using myfaces automatic sorting (although I have
> the same issue using the old commandSortHeader method). For each row in the
> table I have a link which is supposed to open an editable form representing
> the data in the selected row. 
> I have a managed bean which contains an ArrayList of data objects, this list
> is passed to the datatable. The managed bean also has the sort method which
> basically sorts or reverses the sort on the ArrayList. The link on each row
> is an updateActionListener which passes the selected index to a method on
> the managed bean which just retrieves the data object using the
> arraylist.get(index) method. The problem is that there seems to be a copy of
> the list which is not sorted. So if I select the item in the first row of
> the datatable I get the first object in the arraylist. If I reverse the sort
> on the table and select the 
>   



Re: Selecting Row on sortable Datatable

Posted by Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de>.
Got some really mysterious thing.

Using UpdateActionListener with dataTable only - it only works with
preserveDataModel = false.
But using a dataScroller + the dataTable, i can use preserveDataModel =
true and it works - how could that be?

Can anyone confirm this odd behaviour?

kind regards

Am Freitag, den 04.08.2006, 11:08 -0400 schrieb Mike Kienenberger:
> For what it's worth, I never use preserveDataModel=true.   Instead I
> perform a t:saveState on my backing list.
> 
> On 8/4/06, Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de> wrote:
> > Got the same issue.
> > Using preserveDataModel=false works.
> >
> > Am Montag, den 10.07.2006, 10:11 -0700 schrieb Catalin Kormos:
> > > You're right, it's not working, but only if preserveDataModel=true; if
> > > you don't need your model to be preserved between requests, a
> > > temporary workarround, until this is resolved, might be to set it to
> > > false. Let me know if it works out for you with
> > > preserveDataModel=false and using the <t:updateActionListener>
> > > approach.
> > >
> > > Regards,
> > > Catalin
> > >
> > > joelus <jo...@beach.co.za> wrote:
> > >
> > >         Thanks Catalin, but that still doesn't work... I don't know
> > >         what to do now,
> > >         should I give up on myfaces or what? It seems like such an
> > >         obvious thing but
> > >         even using your suggestion, the 'car' I get is the one
> > >         originally occupying
> > >         that row, not the one thats there after the sort. Any more
> > >         suggestions? Has
> > >         anybody actually done this and have it work?
> > >         --
> > >         View this message in context:
> > >         http://www.nabble.com/Selecting-Row-on-sortable-Datatable-tf1905482.html#a5248448
> > >         Sent from the MyFaces - Users forum at Nabble.com.
> > >
> > >
> > >
> > >
> > >
> > > ______________________________________________________________________
> > > How low will we go? Check out Yahoo! Messenger's low PC-to-Phone call
> > > rates.
> >
> >


Re: Fwd: Selecting Row on sortable Datatable

Posted by Mike Kienenberger <mk...@gmail.com>.
On 8/8/06, Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de> wrote:
> How does saveState work - does it call the getter to get the data and
> then when restoring build the bean from scratch and calling the setter?

Yes, it calls the getter and puts the results into the component tree.

On restore,  it will restoreState the object.   If the object doesn't
implement StateHolder, it'll use Serialization.   [Un]Serialization
uses newInstance() to recreate the bean so no constructors will be
called, and the new object != to the old object in the Object.equals()
sense of the equality).   It then calls the setter with this value.

> Is the order relevant - e.g. calling the setWrappedData clears the
> sorted things - any call previous to this one to set the sort order back
> from the savedState will be lost in this case - what to do here?

Yes, I think the order is important.   You'll probably want to have
the saveState for the wrapped data before the saveState for the sort
behavior.

Re: Fwd: Selecting Row on sortable Datatable

Posted by Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de>.
Hi again.

Trying to get the second working.

How does saveState work - does it call the getter to get the data and
then when restoring build the bean from scratch and calling the setter?
Is the order relevant - e.g. calling the setWrappedData clears the
sorted things - any call previous to this one to set the sort order back
from the savedState will be lost in this case - what to do here?

I made some changes to the SortableModel so i can expose the lists and
criteria which are responsible for sorting.
I save the lists and sort criteria but because i am not very deep in
saveState i cant get it work.
Any comments helping to clear how it works more in detail would be nice.

kind regards

Am Montag, den 07.08.2006, 11:48 -0400 schrieb Mike Kienenberger:
> Well, this sounds like a design issue in SortedDataModel.
> There's two things that need to be saved.
> One is the backing list -- that's being done.
> The second is the sort order of the data.   That seems like it needs
> to be exposed in SortedDataModel so you can also preserve that.   If
> it were me (and it's not yet as I haven't needed to add sorting to any
> of my tables), I'd look at the SortedDataModel and see what it'd take
> to give it a sort order accessor (It could simply return a List of
> Integers, where each integer represents the real index of the element
> in the backing list).  There may be some other sort state that needs
> preserved as well (which columns are being used for sorting and  the
> direction of each sort).   Actually,  this second set of data should
> be sufficient to recreate the sorted order of the data.
> 
> On 8/7/06, Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de> wrote:
> > *light on*
> >
> > Yeah thats right.
> >
> > But still it doesnt work like preserveDataModel - the list is still
> > sorted like initial state if i go back to the page with a forward from a
> > detail one, so saving only the data is not enough, because like someone
> > already mentioned in the thread, the real list isn't sorted - this is
> > done somewhere in the model ( correct me if i am wrong ), because of
> > that i initially wanted to save the model - but thats not possible.
> >
> > So how can i preserve the state with saveState?
> >
> > Saving the SortCriteria + the Data did not help, neither saving the
> > sortColumn - ideas?


Re: Fwd: Selecting Row on sortable Datatable

Posted by Mike Kienenberger <mk...@gmail.com>.
Well, this sounds like a design issue in SortedDataModel.
There's two things that need to be saved.
One is the backing list -- that's being done.
The second is the sort order of the data.   That seems like it needs
to be exposed in SortedDataModel so you can also preserve that.   If
it were me (and it's not yet as I haven't needed to add sorting to any
of my tables), I'd look at the SortedDataModel and see what it'd take
to give it a sort order accessor (It could simply return a List of
Integers, where each integer represents the real index of the element
in the backing list).  There may be some other sort state that needs
preserved as well (which columns are being used for sorting and  the
direction of each sort).   Actually,  this second set of data should
be sufficient to recreate the sorted order of the data.

On 8/7/06, Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de> wrote:
> *light on*
>
> Yeah thats right.
>
> But still it doesnt work like preserveDataModel - the list is still
> sorted like initial state if i go back to the page with a forward from a
> detail one, so saving only the data is not enough, because like someone
> already mentioned in the thread, the real list isn't sorted - this is
> done somewhere in the model ( correct me if i am wrong ), because of
> that i initially wanted to save the model - but thats not possible.
>
> So how can i preserve the state with saveState?
>
> Saving the SortCriteria + the Data did not help, neither saving the
> sortColumn - ideas?

Re: Fwd: Selecting Row on sortable Datatable

Posted by Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de>.
*light on*

Yeah thats right.

But still it doesnt work like preserveDataModel - the list is still
sorted like initial state if i go back to the page with a forward from a
detail one, so saving only the data is not enough, because like someone
already mentioned in the thread, the real list isn't sorted - this is
done somewhere in the model ( correct me if i am wrong ), because of
that i initially wanted to save the model - but thats not possible.

So how can i preserve the state with saveState?

Saving the SortCriteria + the Data did not help, neither saving the
sortColumn - ideas?


Am Montag, den 07.08.2006, 10:56 -0400 schrieb Mike Kienenberger:
> On 8/7/06, Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de> wrote:
> > Hm ok thats an idea and its working, however you have to be very careful
> > for chained models.
> >
> > SortableDataModel -> ListDataModel -> List<Typ>.
> >
> > Calling wrappedData on the first one is still not Serializable, you will
> > have to call it twice to get the real Data.
> > But thats implementation specific so its ok - thx for the tipp - tried
> > to save the whole bean which failed, but saving the data only works.
> >
> > A method like "getRealWrappedData" would be nice, to get the real "leaf"
> > of the DataModel Tree which maybe a chain, opinions?
> 
> Like I said, I find it easier to expose the List rather than the
> DataModel, and just serialize the list directly.   I've never resorted
> to the getWrappedData trick.


Fwd: Selecting Row on sortable Datatable

Posted by Mike Kienenberger <mk...@gmail.com>.
On 8/7/06, Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de> wrote:
> Hm ok thats an idea and its working, however you have to be very careful
> for chained models.
>
> SortableDataModel -> ListDataModel -> List<Typ>.
>
> Calling wrappedData on the first one is still not Serializable, you will
> have to call it twice to get the real Data.
> But thats implementation specific so its ok - thx for the tipp - tried
> to save the whole bean which failed, but saving the data only works.
>
> A method like "getRealWrappedData" would be nice, to get the real "leaf"
> of the DataModel Tree which maybe a chain, opinions?

Like I said, I find it easier to expose the List rather than the
DataModel, and just serialize the list directly.   I've never resorted
to the getWrappedData trick.

Re: Selecting Row on sortable Datatable

Posted by Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de>.
Hm ok thats an idea and its working, however you have to be very careful
for chained models.

SortableDataModel -> ListDataModel -> List<Typ>.

Calling wrappedData on the first one is still not Serializable, you will
have to call it twice to get the real Data.
But thats implementation specific so its ok - thx for the tipp - tried
to save the whole bean which failed, but saving the data only works.

A method like "getRealWrappedData" would be nice, to get the real "leaf"
of the DataModel Tree which maybe a chain, opinions?

kind regards 

Am Montag, den 07.08.2006, 10:05 -0400 schrieb Mike Kienenberger:
> On 8/7/06, Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de> wrote:
> > Hm - thats not possible i guess. Using a SortableDataModel ( for
> > example ) which is not serializable by default, you have to make a
> > completly custom solution, extending and so on, to make it work.
> > So this parameter to preserve the model is nice, you get a stateful
> > model which can easily be sort by different SortCriterions.
> >
> > If you use sortable="true", your DataModel is automatically wrapped into
> > a SortableDataModel and you won't be able to use saveState component,
> > how did you solved this?
> >
> > kind regards
> >
> > Am Freitag, den 04.08.2006, 11:08 -0400 schrieb Mike Kienenberger:
> > > For what it's worth, I never use preserveDataModel=true.   Instead I
> > > perform a t:saveState on my backing list.
> > >
> 
> Again, you don't use saveState on your model.   You use it on the data
> inside the model.   However, I'm not sure if that's always possible.
> Again, I've never tried it, but DataModel.[gs]etWrappedData should
> return something you can serialize if you're not already making your
> underlying data available via accessors.


Re: Selecting Row on sortable Datatable

Posted by Mike Kienenberger <mk...@gmail.com>.
On 8/7/06, Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de> wrote:
> Hm - thats not possible i guess. Using a SortableDataModel ( for
> example ) which is not serializable by default, you have to make a
> completly custom solution, extending and so on, to make it work.
> So this parameter to preserve the model is nice, you get a stateful
> model which can easily be sort by different SortCriterions.
>
> If you use sortable="true", your DataModel is automatically wrapped into
> a SortableDataModel and you won't be able to use saveState component,
> how did you solved this?
>
> kind regards
>
> Am Freitag, den 04.08.2006, 11:08 -0400 schrieb Mike Kienenberger:
> > For what it's worth, I never use preserveDataModel=true.   Instead I
> > perform a t:saveState on my backing list.
> >

Again, you don't use saveState on your model.   You use it on the data
inside the model.   However, I'm not sure if that's always possible.
Again, I've never tried it, but DataModel.[gs]etWrappedData should
return something you can serialize if you're not already making your
underlying data available via accessors.

Re: Selecting Row on sortable Datatable

Posted by Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de>.
Hm - thats not possible i guess. Using a SortableDataModel ( for
example ) which is not serializable by default, you have to make a
completly custom solution, extending and so on, to make it work.
So this parameter to preserve the model is nice, you get a stateful
model which can easily be sort by different SortCriterions.

If you use sortable="true", your DataModel is automatically wrapped into
a SortableDataModel and you won't be able to use saveState component,
how did you solved this?

kind regards

Am Freitag, den 04.08.2006, 11:08 -0400 schrieb Mike Kienenberger:
> For what it's worth, I never use preserveDataModel=true.   Instead I
> perform a t:saveState on my backing list.
> 
> On 8/4/06, Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de> wrote:
> > Got the same issue.
> > Using preserveDataModel=false works.
> >
> > Am Montag, den 10.07.2006, 10:11 -0700 schrieb Catalin Kormos:
> > > You're right, it's not working, but only if preserveDataModel=true; if
> > > you don't need your model to be preserved between requests, a
> > > temporary workarround, until this is resolved, might be to set it to
> > > false. Let me know if it works out for you with
> > > preserveDataModel=false and using the <t:updateActionListener>
> > > approach.
> > >
> > > Regards,
> > > Catalin
> > >
> > > joelus <jo...@beach.co.za> wrote:
> > >
> > >         Thanks Catalin, but that still doesn't work... I don't know
> > >         what to do now,
> > >         should I give up on myfaces or what? It seems like such an
> > >         obvious thing but
> > >         even using your suggestion, the 'car' I get is the one
> > >         originally occupying
> > >         that row, not the one thats there after the sort. Any more
> > >         suggestions? Has
> > >         anybody actually done this and have it work?
> > >         --
> > >         View this message in context:
> > >         http://www.nabble.com/Selecting-Row-on-sortable-Datatable-tf1905482.html#a5248448
> > >         Sent from the MyFaces - Users forum at Nabble.com.
> > >
> > >
> > >
> > >
> > >
> > > ______________________________________________________________________
> > > How low will we go? Check out Yahoo! Messenger's low PC-to-Phone call
> > > rates.
> >
> >


Re: Selecting Row on sortable Datatable

Posted by Mike Kienenberger <mk...@gmail.com>.
For what it's worth, I never use preserveDataModel=true.   Instead I
perform a t:saveState on my backing list.

On 8/4/06, Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de> wrote:
> Got the same issue.
> Using preserveDataModel=false works.
>
> Am Montag, den 10.07.2006, 10:11 -0700 schrieb Catalin Kormos:
> > You're right, it's not working, but only if preserveDataModel=true; if
> > you don't need your model to be preserved between requests, a
> > temporary workarround, until this is resolved, might be to set it to
> > false. Let me know if it works out for you with
> > preserveDataModel=false and using the <t:updateActionListener>
> > approach.
> >
> > Regards,
> > Catalin
> >
> > joelus <jo...@beach.co.za> wrote:
> >
> >         Thanks Catalin, but that still doesn't work... I don't know
> >         what to do now,
> >         should I give up on myfaces or what? It seems like such an
> >         obvious thing but
> >         even using your suggestion, the 'car' I get is the one
> >         originally occupying
> >         that row, not the one thats there after the sort. Any more
> >         suggestions? Has
> >         anybody actually done this and have it work?
> >         --
> >         View this message in context:
> >         http://www.nabble.com/Selecting-Row-on-sortable-Datatable-tf1905482.html#a5248448
> >         Sent from the MyFaces - Users forum at Nabble.com.
> >
> >
> >
> >
> >
> > ______________________________________________________________________
> > How low will we go? Check out Yahoo! Messenger's low PC-to-Phone call
> > rates.
>
>

Re: Selecting Row on sortable Datatable

Posted by Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de>.
Got the same issue.
Using preserveDataModel=false works.

Am Montag, den 10.07.2006, 10:11 -0700 schrieb Catalin Kormos:
> You're right, it's not working, but only if preserveDataModel=true; if
> you don't need your model to be preserved between requests, a
> temporary workarround, until this is resolved, might be to set it to
> false. Let me know if it works out for you with
> preserveDataModel=false and using the <t:updateActionListener>
> approach.
> 
> Regards,
> Catalin
> 
> joelus <jo...@beach.co.za> wrote:
>         
>         Thanks Catalin, but that still doesn't work... I don't know
>         what to do now,
>         should I give up on myfaces or what? It seems like such an
>         obvious thing but
>         even using your suggestion, the 'car' I get is the one
>         originally occupying
>         that row, not the one thats there after the sort. Any more
>         suggestions? Has
>         anybody actually done this and have it work?
>         -- 
>         View this message in context:
>         http://www.nabble.com/Selecting-Row-on-sortable-Datatable-tf1905482.html#a5248448
>         Sent from the MyFaces - Users forum at Nabble.com.
>         
> 
> 
> 
> 
> ______________________________________________________________________
> How low will we go? Check out Yahoo! Messenger’s low PC-to-Phone call
> rates.


Re: Selecting Row on sortable Datatable

Posted by Catalin Kormos <ca...@yahoo.com>.
You're right, it's not working, but only if preserveDataModel=true; if you don't need your model to be preserved between requests, a temporary workarround, until this is resolved, might be to set it to false. Let me know if it works out for you with preserveDataModel=false and using the <t:updateActionListener> approach.

Regards,
Catalin

joelus <jo...@beach.co.za> wrote: 
Thanks Catalin, but that still doesn't work... I don't know what to do now,
should I give up on myfaces or what? It seems like such an obvious thing but
even using your suggestion, the 'car' I get is the one originally occupying
that row, not the one thats there after the sort. Any more suggestions? Has
anybody actually done this and have it work?
-- 
View this message in context: http://www.nabble.com/Selecting-Row-on-sortable-Datatable-tf1905482.html#a5248448
Sent from the MyFaces - Users forum at Nabble.com.



 		
---------------------------------
How low will we go? Check out Yahoo! MessengerÂ’s low  PC-to-Phone call rates.

Re: Selecting Row on sortable Datatable

Posted by joelus <jo...@beach.co.za>.
Thanks Catalin, but that still doesn't work... I don't know what to do now,
should I give up on myfaces or what? It seems like such an obvious thing but
even using your suggestion, the 'car' I get is the one originally occupying
that row, not the one thats there after the sort. Any more suggestions? Has
anybody actually done this and have it work?
-- 
View this message in context: http://www.nabble.com/Selecting-Row-on-sortable-Datatable-tf1905482.html#a5248448
Sent from the MyFaces - Users forum at Nabble.com.


Re: Selecting Row on sortable Datatable

Posted by Catalin Kormos <ca...@yahoo.com>.
You're not the only with this problem, but if you need to find out the row object from the selected row you could also use the <t:updateActionListener>, like the following: 

<h:commandLink value="#{car.id}">
       <t:updateActionListener property="#{autosortlist.selectedCar}" value="#{car}"/>
</h:commandLink>

HTH,
Catalin

joelus <jo...@beach.co.za> wrote: 
Thanks Holger, but I have the same problem using this method. 
As soon as I sort the rows in the datatable they are out of sync  with the
backing bean. 
Somehow, somewhere, a copy of the list exists as it was originally sorted,
and selecting a row, either with index or an actionlistener as in the link
you gave me, returns the from this original list. Sorting the datatable
doesn't effect this list so it goes out of sync. It's very frustrating.
Surely I can't be the only one with this problem?
-- 
View this message in context: http://www.nabble.com/Selecting-Row-on-sortable-Datatable-tf1905482.html#a5218575
Sent from the MyFaces - Users forum at Nabble.com.



 		
---------------------------------
Do you Yahoo!?
 Everyone is raving about the  all-new Yahoo! Mail Beta.

Re: Selecting Row on sortable Datatable

Posted by joelus <jo...@beach.co.za>.
Thanks Holger, but I have the same problem using this method. 
As soon as I sort the rows in the datatable they are out of sync  with the
backing bean. 
Somehow, somewhere, a copy of the list exists as it was originally sorted,
and selecting a row, either with index or an actionlistener as in the link
you gave me, returns the from this original list. Sorting the datatable
doesn't effect this list so it goes out of sync. It's very frustrating.
Surely I can't be the only one with this problem?
-- 
View this message in context: http://www.nabble.com/Selecting-Row-on-sortable-Datatable-tf1905482.html#a5218575
Sent from the MyFaces - Users forum at Nabble.com.


Re: Selecting Row on sortable Datatable

Posted by ho...@bayerbbs.com.
Did you try to use this approach instead of working with index parameter?
http://wiki.apache.org/myfaces/Working_with_DataTable_and_ActionListeners


Kind regards
Holger