Search code examples
javascriptlistviewwindows-8winjs

How to remove an element from group listView in Windows 8 app using JavaScript


I want user to select an item from listview and then from the appbar click on the delete button to remove the selected item....
I have done this kind of thing many times before but on a simple listview that is which doesnt have any groups, but that method of deleting an item from listView is not applicable here as it is a group listview because it sorts the objects in some order and hence mixing all the indices (index).

This is the method I have used to delete an element from simple (un-grouped) listview:

var deleteIndex=0;
var listView=document.getElementById('listview').winControl;
listView.addEventListener('selectionchanged', function () {
          deleteIndex=listView.selection.getIndices();
        });

  function deleteButtonOnClick(){
         data.splice(deleteIndex,1);
     }

This is not working on group listview, it deletes some other item in listView... I am really stuck at this point, i have to complete my app by tomorrow.


Solution

  • I can't give you a code sample since I'm lacking your data model, but the general concept is

    1. have some identifiable data in each object in the array. If it's coming from a DB, you probably have an ID field.
    2. on selectionchanged get that id for the selected item
    3. in you deleteButtonOnClick function first loop through your data array, checking if the id matches with the selected item's, when it does, you have the index of your object
    4. now using that index, splice it out of your array