This is a migrated thread and some comments may be shown as answers.

Selecting previous item after a remove

1 Answer 200 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 24 Jul 2012, 10:46 PM
Hello, I've got a listview whose template includes a button that causes removal of the clicked item from the listview.  I also followed another forum post explaining how to suppress the mouseclick event so that my button click event doesn't select the clicked item. 

However, when the remove happens in my click handler via the listview.remove(item) method, it looks like the listview syncs to the datasource.  The problem is that when this occurs the listview appears to lose it's selection.  I'm guessing this is by design, but is there a way in which I can store the selected index before the remove and then reselect the item afterwards?  The main task is getting the selected index in my click handler.  I see a way to get the selected item using listView().select() but not the index.  After the rebind, if I try to pass this selected item into the select function it no longer finds it, so I guess it goes out of scope after the rebind. 

Do you have any guidance for me?

Thanks,
Eric

1 Answer, 1 is accepted

Sort by
0
Eric
Top achievements
Rank 1
answered on 24 Jul 2012, 11:07 PM
Well I discovered a way to do this:

1) Get the id of the selected item (where listView represent a reference to the listview object per the usual method):
var selectedItem = listView.select()[0].id;

2) The after the remove, find this item using jquery
$("#" + selectedItem.id);

3) and reselect it:
listView.select(image);

I did figure out how to get the selected index: listView.select().index() but didn't use it here.

Thanks,
Eric
Tags
ListView
Asked by
Eric
Top achievements
Rank 1
Answers by
Eric
Top achievements
Rank 1
Share this question
or