Reputation: 2433
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.
Upvotes: 1
Views: 520
Reputation: 5535
you would have created groups from list if the listview is displaying grouped data. splice needs to be invoked on the groups object.
// this is for example. your data model will be different and hence,
// the createGrouped call details will be different.
var list = new WinJS.Binding.List(data);
var groups = list.createGrouped(
function groupKey(item)
{
return item.brand;
},
function groupData(item)
{
var result = { title: item.brand };
return result;
}
);
this.groups = groups;
listView.winControl.itemDataSource = groups.dataSource;
listView.winControl.groupDataSource = groups.groups.dataSource;
now, you appbar cmd handler will delete the selected list item something like this. note - it should not be done in the selectionchanged event handler.
_oncmdclick: function oncmdclick(event)
{
var indices = listView.winControl.selection.getIndices();
// assuming that delete should only handle single item selection
if (indices && indices.length == 1)
{
this.groups.splice(indices[0], 1);
}
},
Upvotes: 0
Reputation: 11054
I can't give you a code sample since I'm lacking your data model, but the general concept is
Upvotes: 1