Search code examples
listviewfor-looptitaniumtitanium-alloy

Marking items in a listview then looping through them to grab checked items properties


I have this code here:

$.clientList.addEventListener('itemclick', function(e){
    var item = e.section.getItemAt(e.itemIndex);
    var items = e.section.getItems();

    if (item.properties.accessoryType == Ti.UI.LIST_ACCESSORY_TYPE_NONE) {
        item.properties.accessoryType = Ti.UI.LIST_ACCESSORY_TYPE_CHECKMARK;
    }
    else {
        item.properties.accessoryType = Ti.UI.LIST_ACCESSORY_TYPE_NONE;
    }
    e.section.updateItemAt(e.itemIndex, item); 

 });

which allows me to check and uncheck items in my listview. I want to, after the user is done checking items from this listview. Grab the values of item.properties.clientname and item.properties.clientid from the listview.

How do I do this? I want to loop through this listview and only grab the selected items of the listview.

Thanks, Kenny


Solution

  • function convertListToArrayOfClients(list) {
        var sections = list.sections,
            retVal = [];
        for(var i = 0, iL = sections.length; i < iL; i++) {
            var section = sections[i],
                items = section.items;
            for(var j = 0, jL = items.length; j < jL; j++) {
                var item = items[j];
                retVal.push({
                    clientid: item.properties.clientid,
                    clientname: item.properties.clientname,
                    checked: item.properties.accessoryType == Ti.UI.LIST_ACCESSORY_TYPE_CHECKMARK
                });
            }
        }
        return retVal;
    }
    var arr = convertListToArrayOfClients($.clientList);