Rating an Element With ListView in Windows Store Apps

Introduction

Today we are going to learn how to create Windows Store Apps for a rating in a list view using JavaScript. This example shows how to add rating controls to items in the List View.

In the previous article I described the page rating without using a list view, to see the article visit page rating in Windows Store Apps using JavaScript. In many apps we feel the need to rate several items. For this we use a list view to provide items in a list and allow the users to rate them easily. 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 apps, add two JavaScript pages by right-clicking on the js folder in the Solution Explorer and select Add > new item > JavaScript Page and then give an appropriate name. In the same way, add one HTML pages to your project.

rating-windows-store-apps.jpg

Write the following code in default.aspx:

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8" />

    <title>App</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/script1.js"></script>

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

</head>

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

    <center><div style="width:500px">  <div id="rootGrid">

        <div id="content">

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

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

        </div>

       </div>

      </div></center>

</body>

</html>

Write the following code in default.js:
 

(function () {

    "use strict";

    var sampleTitle = "Rating Apps";

    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 (evt) {

        var url = evt.detail.location;

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

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

        WinJS.Utilities.empty(host);

        WinJS.UI.Pages.render(url, host).then(function () {

            WinJS.Application.sessionState.lastUrl = url;

        });

    });

 

    WinJS.Namespace.define("App", {

        sampleTitle: sampleTitle,

        pages: pages

    });

 

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

    WinJS.Application.start();

})();


Write the following code in page.html:
 

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

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

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

</head>

<body>

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

    </div>                           

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

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

            <div class="regularListIconTextItem">

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

                    <div class="regularListIconTextItem-Detail">

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

                            <div class="ratings win-interactive" data-win-control="WinJS.UI.Rating" data-win-bind="winControl.userRating: rating">

                            </div>

                    </div>

            </div>

        </div>

        <div  data-win-control="WinJS.UI.ListView" data-win-options="{ itemDataSource: myData.dataSource, itemTemplate: MyItemTemplate, selectionMode: 'none', tapBehavior: 'none', swipeBehavior: 'none', layout: { type: WinJS.UI.ListLayout } }">

        </div>

    </div>

</body>

</html>


Write the following code in script.js:
 

var myData = new WinJS.Binding.List([

        { title: "", picture: "images/1.jpg", rating: 5 },

        { title: "", picture: "images/2.jpg", rating: 4 },

        { title: "", picture: "images/3.jpg", rating: 5 },

        { title: "", picture: "images/4.jpg", rating: 5 }     

]);

(function () {

    "use strict";

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

        ready: function (element, options) {           

         if (document.styleSheets[0].disabled === true) {

                document.getElementById("darkStyle").checked = true;

                document.getElementById("lightStyle").checked = false;

            }

        }

    });

})();


Write the following code in script1.js:
 

(function () {

    var pageOutput = WinJS.Class.define(

        function (element, options) {

            element.winControl = this;

            this.element = element;

            new WinJS.Utilities.QueryCollection(element)

                        .setAttribute("role", "region")

                        .setAttribute("aria-labelledby", "outputLabel")

                        .setAttribute("aria-live", "assertive");

            element.id = "output";

 

            this._addOutputLabel(element);

            this._addStatusOutput(element);

        }, {

            _addOutputLabel: function (element) {

                var label = document.createElement("h2");

                label.id = "outputLabel";

                label.textContent = "Output";

                element.parentNode.insertBefore(label, element);

            },

            _addStatusOutput: function (element) {

                var statusDiv = document.createElement("div");

                statusDiv.id = "statusMessage";

                element.insertBefore(statusDiv, element.childNodes[0]);

            }

        }

    );

    WinJS.log = function (message, tag, type) {

        var statusDiv = document.getElementById("statusMessage");

    };

    function activated(e) {

        WinJS.Utilities.query("#featureLabel")[0].textContent = App.sampleTitle;

    }

 

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

    WinJS.Namespace.define("App", {

        pageOutput: pageOutput

    });

})(); 


Output:

page-rating-windows-store-apps.jpg

Summary


In this article I described how to create a Windows Store App for Listview Rating using JavaScript. I hope this article has helped you in understanding this topic. Please share it. If you know more about this, your feedback and constructive contributions are welcome.


Similar Articles