locked
How to get the wincontrol of a listview, that was created programmatically in JS

    Question

  • Hi all,

    I am able to get the wincontrol of the element if it was created in html. But if it was created dynamically, I get "Unable to get wincontrol of a null or undefined reference". this is what I am trying now.   Any help on this?

            var newDiv = document.createElement("div");
            var y = new WinJS.UI.ListView(newDiv);
            y.id = "basicListView" + n;
            y.class = "basiclistviewclass";
            y.itemDataSource = DataExamplenew.itemList.dataSource;
            y.itemTemplate = listtemplatejs;
            y.selectionMode = 'multi';
            y.tapBehavior = 'toggleSelect';
       //    y.winControl = this;
       //    y.newDiv = newDiv;
            y.onselectionchanged = function () { hideappbarnow() };
    
            nextlistjs.appendChild(newDiv);
    
    
    
     WinJS.UI.processAll().then(function () { var che = document.getElementsByClassName("basiclistviewclass").winControl; });
    
     WinJS.UI.processAll().then(function () { var che = document.getElementsById("basicListView"+n).winControl; });
    
     WinJS.UI.processAll().then(function () { var che = document.querySelector(".basiclistviewclass").winControl; });

    Sunday, December 16, 2012 4:14 PM

Answers

  • The object you have in the variable y is already the winControl. You only need to use .winControl when al lyou have is the HTML element for the ListView, as you have in the calls to document.get*/querySelector. In your code above, if you look at newDiv.winControl you should see the same object that's in y.

    .Kraig

    • Marked as answer by ArunKarthika Tuesday, December 18, 2012 9:36 PM
    Sunday, December 16, 2012 4:53 PM

All replies

  • The object you have in the variable y is already the winControl. You only need to use .winControl when al lyou have is the HTML element for the ListView, as you have in the calls to document.get*/querySelector. In your code above, if you look at newDiv.winControl you should see the same object that's in y.

    .Kraig

    • Marked as answer by ArunKarthika Tuesday, December 18, 2012 9:36 PM
    Sunday, December 16, 2012 4:53 PM
  • Hi ArunKarthika,

    As I've replied in the previous thread below, when you create a new WinJS.UI.ListView control, the HTML element (which should be an <div> element already or will be added on your app page) you pass to the constructor is just the hosting element of the ListView control. You should give the element a valid id so that you can reference it later

    http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/96deae22-6cd8-4850-b20a-395e143270ad/

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, December 17, 2012 7:31 AM
    Moderator
  •  Hi Kraig,

    Yes, :)     y.selection itself works perfectly. I am creating listviews dynamically, when newlist button is clicked each time.  So I am having an array which will hold my listview wincontrol names.   Hope this is teh right approach.

    var y = new Array(); WinJS.Namespace.define("dataexample", y);

    .....

    y[i] = new WinJS.UI.ListView(newDiv);

    ......

    y[i].selection.getItems().done();

    BTW, Your ebook is very helpful and addresses needy issues. Thanks so much for the nice resource. Keep the good work going !!!

    -Karthi.

    Tuesday, December 18, 2012 10:00 PM
  • Your array is fine, so long as you're managing the lifetimes of those variables rightly. For example, if you destroy the element to which the control is attached, you'll want to invalidate the y[i] value for that.

    Glad you're enjoying the book and finding it helpful! Because it's free (meaning I'm not making anything on it), I can shamelessly say to spread the word. :) And I'm already looking at the next edition...always more that I could have written and things to clean up and refine.

    Tuesday, December 18, 2012 10:26 PM