locked
ListView DataSource, how to find items?

    Question

  • Hello, i have a question about ListView and its DataSource property. I create some datasource from array like in Quickstart.

        var dataArray = [
        { title: "Basic banana", text: "Low-fat frozen yogurt", picture: "images/60banana.png" },
        { title: "Banana blast", text: "Ice cream", picture: "images/60banana.png" },
        { title: "Brilliant banana", text: "Frozen custard", picture: "images/60banana.png" },
        ];
    
        var dataList = new WinJS.Binding.List(dataArray); 

    Now i have problem, i read many samples, forum threads, and still didnt find answer.

    I need to change items in datasource. To change it, we need to use iListDataSource.change(key, newData); But how to get key of the item, if i only know for example title of the item. For example, i want to change text of "Basic banana" from "Low-fat frozen yogurt" to "xxx":

        datasource.itemFromIndex(0).then(function (item) {
            item.data.text = "xxx";
            datasource.change(item.key, item.data);
        });

    But in this example i use "itemFromIndex(0)". What if i dont know index of the item? I found function
    "itemFromDescription(description)", but how to use it? Is it possible to make something like itemFromDescription(title: "Basic banana")? Or how to find index of Basic banana in other way.

    Tuesday, March 06, 2012 1:57 PM

Answers

  • I wrote some working code

    dataList.forEach(function (item, i) {
         if (item.title == "Basic banana") {
              item.text = "xxx";
              datasource.change(i, item);
         }
    });

    • Edited by raximus Tuesday, March 06, 2012 7:13 PM
    • Marked as answer by raximus Tuesday, March 06, 2012 7:13 PM
    Tuesday, March 06, 2012 4:03 PM

All replies

  • I wrote some working code

    dataList.forEach(function (item, i) {
         if (item.title == "Basic banana") {
              item.text = "xxx";
              datasource.change(i, item);
         }
    });

    • Edited by raximus Tuesday, March 06, 2012 7:13 PM
    • Marked as answer by raximus Tuesday, March 06, 2012 7:13 PM
    Tuesday, March 06, 2012 4:03 PM
  • Use datasource.list.setAt instead. Here's an example:

    function changeListItemTextByTitle(list, title, newText) {
        ///<summary>Searches a list by item.title, and changes the item.text of each cooresponding item</summary>
        ///<param name="list">The WinJS.Binding.List you wish to search and change</param>
        ///<param name="title">The item.title you wish to filter by</param>
        ///<param name="newText">The new item.text you wish to set to on items matching the title</param>
    
        list.forEach(function (item, i) {
            if (item.title === title) {
                item.text = newText;
                list.setAt(i, item);
            }
        });
    };

    Then call the function list this (I threw in a couple of delayed updates to demonstrate changing multiple items and the ListView getting updated):

    var dataArray = [
        { title: "Basic banana", text: "Low-fat frozen yogurt", picture: "images/60banana.png" },
        { title: "Banana blast", text: "Ice cream", picture: "images/60banana.png" },
        { title: "Brilliant banana", text: "Frozen custard", picture: "images/60banana.png" },
    ];
    
    var dataList = new WinJS.Binding.List(dataArray);
    
    lv.itemDataSource = dataList.dataSource;
    
    changeListItemTextByTitle(lv.itemDataSource.list, "Banana blast", "This is some new text");
    
    setTimeout(function () {
        changeListItemTextByTitle(lv.itemDataSource.list, "Banana blast", "This is some new new text");
    }, 2000);
                    
    setTimeout(function () {
        changeListItemTextByTitle(lv.itemDataSource.list, "Brilliant banana", "This is some new Brilliant banana text");
    }, 4000);


    Senior Dev for Windows Phone Services


    Tuesday, March 06, 2012 8:19 PM