Detail List View in Windows Store App

Introduction

In this article I describe how to create a Windows Store App for Detail List View using JavaScript. This app shows how to use the List View to create a detail list, similar to what you'd see in most news screens. The List View provides a list of news. Selecting an item in that list updates the bottom pane.

I assume you can create a simple Windows Store App using JavaScript. For more help visit Simple Windows Store Apps using JavaScript.

To start the creation of the app, add two JavaScript pages by right-clicking on the js folder in the Solution Explorer and select "Add" > "New item" > "JavaScript Page" and then provide an appropriate name. In the same way, add one HTML page to your project.

detail-list-in-windows-store-apps.jpg

Write the following code in default.html:

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8" />

    <title></title>

    <link rel="stylesheet" href="//Microsoft.WinJS.1.0/css/ui-light.css" />

    <script src="//Microsoft.WinJS.1.0/js/base.js"></script>

    <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>

    <link rel="stylesheet" href="/css/default.css" />

    <script src="/js/default.js"></script>

</head>

<body role="application" style="background-color: lightblue">

    <center><div id="rootGrid">

        <div id="content">

            <h1 id="featureLabel"></h1>

            <div id="contentHost"></div>

        </div>

    </div></center>

</body>

</html>

Write the following code in default.js:
 

(function () {

    "use strict";

    var appTitle = "";

    var pages = [       

        { url: "page.html"}

    ];

    function activated(eventObject) {

        if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {

            eventObject.setPromise(WinJS.UI.processAll().then(function () {

                var url = WinJS.Application.sessionState.lastUrl || pages[0].url;

                return WinJS.Navigation.navigate(url);

            }));

        }

    }

    WinJS.Navigation.addEventListener("navigated", function (eventObject) {

        var url = eventObject.detail.location;

        var host = document.getElementById("contentHost");

        host.winControl && host.winControl.unload && host.winControl.unload();

        WinJS.Utilities.empty(host);

        eventObject.detail.setPromise(WinJS.UI.Pages.render(url, host, eventObject.detail.state).then(function () {

            WinJS.Application.sessionState.lastUrl = url;

        }));

    });

    WinJS.Namespace.define("App", {

        appTitle: appTitle,

        pages: pages

    });

    WinJS.Application.addEventListener("activated", activated, false);

    WinJS.Application.start();

})(); 


Write the following code in page.html:
 

<!DOCTYPE html>

<html>

<head>

    <title></title>

    <script src="/js/script.js"></script>

</head>

<body>

    <div data-win-control="App.pageInput">

    </div>

    <div data-win-control="App.pageOutput">

        <div id="mediumListIconTextTemplate" data-win-control="WinJS.Binding.Template" style="display: none">

            <div class="mediumListIconTextItem">

                <img src="#" class="mediumListIconTextItem-Image" data-win-bind="src: picture" />

                <div class="mediumListIconTextItem-Detail">

                    <h4 data-win-bind="innerText: title"></h4>

                    <h6 data-win-bind="innerText: text"></h6>

                </div>

            </div>

        </div>

        <div id="inbox">

            <div id="listView2"

                class="win-selectionstylefilled"

                data-win-control="WinJS.UI.ListView"

                data-win-options="{

                    itemDataSource: myNewsData.dataSource,

                    itemTemplate: mediumListIconTextTemplate,

                    selectionMode: 'multi',

                    tapBehavior: 'directSelect',

                    layout: { type: WinJS.UI.ListLayout }

                }"

            ></div>

            <div id="inboxNews">

                <h2 id="inboxNews_Text"></h2>

                <h6 id="inboxNews_Title"></h6>

                <div id="inboxNews_Body"></div>

            </div>

        </div>

    </div>

</body>

</html>


Write the following code in script.js:
 

var myNewsData = new WinJS.Binding.List([

        { title: "Sports News", text:"Deepak Arora", picture: "images/1.jpg" },

        { title: "Wheather News", text:"Deepak Arora", picture: "images/1.jpg" },

        { title: "Politics New ", text:"Deepak Arora", picture: "images/1.jpg" },

        { title: "World news", text:"Deepak Arora", picture: "images/1.jpg" }

]);

(function () {

    "use strict";

    var page = WinJS.UI.Pages.define("page.html", {

        ready: function (element, options) {

            var NewsTitle = element.querySelector("#inboxNews_Title");

            var NewsText = element.querySelector("#inboxNews_Text");

            var NewsBody = element.querySelector("#inboxNews_Body");

            var listView = element.querySelector("#listView2").winControl;

            listView.forceLayout();

            function selectionChangedHandler() {

                var selectionCount = listView.selection.count();

                if (selectionCount === 1) {

                    listView.selection.getItems().done(function (items) {

                        inboxNews_Title.innerText = "From: " + items[0].data.text;

                        inboxNews_Text.innerText = items[0].data.title;

                        inboxNews_Body.style.color = "black";

                        inboxNews_Body.innerText = "\n\nNews Body";

                    });

                } else {

                    inboxNews_Title.innerText = "";

                    inboxNews_Text.innerText = "";

                    inboxNews_Body.style.color = "green";

                    if (selectionCount > 1) {

                        inboxNews_Body.innerText = "Multiple Newss are selected!";

                    } else {

                        inboxNews_Body.innerText = "No News selected!";

                    }

                }

            }

            listView.addEventListener("selectionchanged", selectionChangedHandler, false);

            listView.itemDataSource.getCount().done(function (itemCount) {

                var initialSelection = (itemCount > 0) ? 0 : [];

                listView.selection.set(initialSelection);

            });

        }

    });

})();

Output:

detail-list-in-windows-store-app-.jpg
 

Summary

In this app I described how to get the details of an item in a List View in a Windows Store App using JavaScript. I hope this article has helped you to understand this topic. Please share if you know more about this. Your feedback and constructive contributions are welcome.

Up Next
    Ebook Download
    View all
    Learn
    View all