none
How to get the deselect item in ListView with Html+JS

    Question

  • I have a feature, when user selects or deselects one item in listview, a list maintaining the selected items will add the item or delete the item automatically.

    I am new to use JS and develop winapp. I have tried many methods to get the item in js when user deselect one in onselectionchanged or onselectionchanging function, but not works.

    Seems the eventObject only related to the listview instead of one item. 

    _itemSelected: function (eventObject) {}

    Actually, i can't get the latest selected item either, but i use a workaround:

    //add item to selectedList               
    listView.selection.getItems().done(function(items) {                  
                        selectedList.push(items.pop().data); //this is not correct, the selection is ordered by item index, instead of select order.
                    });
                }

    Please help. Thanks!

    Supplement:  use the workaround to filter the added and removed item using two loop.

    • Edited by Forzas Thursday, August 29, 2013 7:02 AM fix incorrect discription
    Tuesday, August 27, 2013 12:47 PM

Answers

  • Hi Forzas,

    Normally the onselectionchanged event notifies you when the ListView control's selection changes, but it won't tell you how the selection changed. But you could do it by yourself.

    Before the selection changes, cache the current selection by calling selection.getIndices and storing the result. selection.getIndices returns an Array that contains the indexes of the currently selected items.

    var prevSelection = lv.selection.getIndices(); 

    Register for the onselectionchanged event.

    When the onselectionchanged event fires, call selection.getIndices again to get an Array of the currently selected item indexes.

    var currentSelection = lv.selection.getIndices();

    Compare the two arrays. One way to calculate the difference is to use the Array.filter method

    But you should note that when modify the item for the ListView, Don't make changes to the itemDataSource or groupDataSource in your event handler. Instead, call msSetImmediate from your event handler and pass it a function that makes the changes to the data source.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, August 29, 2013 3:21 AM

All replies

  • Hi,

    The "onselectionchanged" is for the Listview and not for individual items. You are right here.

    What puzzles me is that why are you tracking it if the only thing that you intent to do is to keep a list of items that is selected at the end. The "listView.selection.getItems()" will give you the data anyways.

    Let me know if my understanding is wrong here.

    - Girija


    Please mark responses as answered if it helped you.. This helps others... - Girija Shankar Beuria

    Tuesday, August 27, 2013 1:45 PM
  • Hi,

    The list maintains the selected items is also a listView, it will auto show the added item or not show the removed item. So i need to handle the individual item event.

    Do you know which event i can use to do this? Because when select event occurs, only the related item style is changed, so i think it should be possible to get the item control...

    Tuesday, August 27, 2013 2:05 PM
  • Hi Forzas,

    Normally the onselectionchanged event notifies you when the ListView control's selection changes, but it won't tell you how the selection changed. But you could do it by yourself.

    Before the selection changes, cache the current selection by calling selection.getIndices and storing the result. selection.getIndices returns an Array that contains the indexes of the currently selected items.

    var prevSelection = lv.selection.getIndices(); 

    Register for the onselectionchanged event.

    When the onselectionchanged event fires, call selection.getIndices again to get an Array of the currently selected item indexes.

    var currentSelection = lv.selection.getIndices();

    Compare the two arrays. One way to calculate the difference is to use the Array.filter method

    But you should note that when modify the item for the ListView, Don't make changes to the itemDataSource or groupDataSource in your event handler. Instead, call msSetImmediate from your event handler and pass it a function that makes the changes to the data source.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, August 29, 2013 3:21 AM
  • Thanks James for your help.

    I have use the workaround to filter the added and removed item using two loop.

    Thursday, August 29, 2013 6:59 AM