3

I have a problem displaying information in an array with windows 8 app, I send information to the view with this.

var details = document.querySelector(".articlesection");

var data = [{
    "id": "1",
    "title": "Title text",
    "files": [
        {
            "id": "1",
            "title": "File1"
        },
        {
            "id": "2",
            "title": "File2"
        }
    ]
}];


binding.processAll(details, data);

I receive the data in the view like this (This works, but cant get info from the files array)

<div class="articlesection">
    <h2 data-win-bind="textContent: title">
        <!-- Displays 1 -->
    </h2>
    <h3 data-win-bind="textContent: id">
        <!-- Displays "Title text" -->
    </h3>
</div>

How can i make it show the 2 objects in the array? If I try

<div class="articlesection">
    <h2 data-win-bind="textContent: title">
        <!-- Displays 1 -->
    </h2>
    <h3 data-win-bind="textContent: id">
        <!-- Displays "Title text" -->
    </h3>
    <p data-win-bind="textContent: files">
        <!-- i get [Object object], [Object object] -->
    </p>
</div>

So clearly here is some data, So how can I do it if I want it like this. (Foreach doesnt exists, but I wanted to show how I want to do)

<div class="articlesection">
    <h2 data-win-bind="textContent: title">
        <!-- Displays 1 -->
    </h2>
    <h3 data-win-bind="textContent: id">
        <!-- Displays "Title text" -->
    </h3>
    <ul data-win-bind: foreach: files>
        <!-- Foreach all files, how can I do this? Foreach doesn't exist. -->
        <li>
            <span data-win-bind="textContent: id"></span> 
            <span data-win-bind="textContent: title"></span>
        </li>
    </ul>
</div>

Regards, Jim

1

2 Answers 2

1

Use the WinJS.UI.ListView control.

Edit because I wrote this from the train: A WinJS ListView control is what you want to use to display a list of objects. The control gives you the ability to indicate a type of layout (list or grid layout) and define behaviors around what (if anything) happens when an item is tapped. You get to provide an itemTemplate for each item or you can create custom templating functions that render all kinds of different magical things depending on your item.

This is a quick sample of how you would use it.

<div id='lv' data-win-control='WinJS.UI.ListView' />

var lv = $('#lv')[0].winControl;
var data = ...; // some js array
var bindinglist = new WinJS.Binding.List(data);
lv.itemDataSoutce = bindinglist.dataSource;
lv.itemTemplate = function(itemPromise) {
    return itemPromise.then(function(item) {
        var f = $('<div></div>');
        f.text(item.title);
    });
};

See this for an example of how to use it. Shameless plug, I wrote an article about how WinJS controls work. Might be some good background reading.

ItemTemplate can also be an HTML template (see WinJS control WinJS.Binding.Template). If you want you can also reuse other template libraries like Mustache.JS and hook them in through the itemTemplate function. Here is an article I wrote about reusing Knockout templates in WinJS.

Sign up to request clarification or add additional context in comments.

4 Comments

Can you desribe more please? This is already in a listview, do i need to create one more or what?
Done. It contains a fairly large amount of functionality but helps you get lists to have that Win8 look & feel.
Hi Dominic, i tried to use list inside of a list, but your anwers says that you cant do that. I have tried your example aswell. The thing is that i use the Microsoft SplitApp as basic. Then im in split.js in the function _selectionChanged: function (args) { where i want to show specific meetingdata. But files only return object object. I have tried to use your solution 1, but it never show any data in the view
0

Use converters to solve this issue. In your example code instead of this line:

<p data-win-bind="textContent: files"></p> // i get [Object object], [Object object]

You can just add in a converter as such

<p data-win-bind="innerHTML: files myFilesToStringConverter"></p> 

And define your converter in the .js file e.g.

var myFilesToStringConverter = WinJS.Binding.converter(function(files){
   var convertedValue = "<ul>";
   for(var i=0;i<files.length;i++){
      convertedValue += "<li><span>" +files[i].id + "</span> <span> " + files[i].title + "</span></li>";
   convertedValue += "<ul>";
   return convertedValue ;
   }
});

With this converter in place you'll see the IDs and names of the files listed as expected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.