PDA

View Full Version : LIST: Get selected index on swipe



camdagr8
10 May 2012, 4:38 AM
I'm trying to get the selected of a list when you swipe it. I'd like to make sure the list item is selected before executing a function on the list item but I can't for the life of me figure out how to do this! Here's my code, please help!!!!


{
xtype: 'list',
flex: 1,
indexBar: true,
itemTpl: '{city} {name}',
store: 'TeamStore',
id: 'TeamsList',
items: [
{
xtype: 'toolbar',
docked: 'bottom',
height: 49,
cls: 'z-tabbar',
items: [
{xtype: 'searchfield', name: 'teamSearch', placeHolder: 'Search', centered: true, width: '95%'}
]
}
],
listeners: {
itemswipe: function (list, idx, target, record, evt) {

var selected = list.getActiveItem();
console.log(list.getActiveItem());
if (!selected) { return; }

var selectedIndex = selected[0];
console.log([selectedIndex, idx]);
Ext.Msg.alert('itemswipe', idx);


} // itemswipe
} // listeners
}

mitchellsimoens
14 May 2012, 5:44 AM
I'm curious why you tried getActiveItem() to get the selected?

To get the selection you should use getSelection() instead

camdagr8
14 May 2012, 8:23 PM
Mainly because the swipe event isn't fired on the actual selection at the time. For instance if you select item 1 but swipe item 3, the event is fired on item 3 not selection. My functionality requires that the item be selected before doing anything with the swipe event. So I need to check and find out what the selected index is before doing anything.

Also I get this back in the xcode debugger:
JSON.stringify cannot serialize cyclic structures.

And the documentation doesn't explain what getSelection() outputs.

So ultimately what I ended up doing was using jquery's .each function and search the list for the 'x-item-selected' class and matched that against the swipe event index like so:



var selected = false;
var listID = '#' + list.getId();
$(listID + ' .x-list-item').each(function (index, element) { if ($(this).hasClass('x-item-selected') && index == idx) { selected = true; } });
if (!selected) { return; }

mitchellsimoens
15 May 2012, 5:12 AM
Why not do list.getSelection() in the swipe event listener. According to the docs, getSelection does:


Returns an array of the currently selected records.