다음을 통해 공유


ListView.onselectionchanged event

Raised after the current selection changes.

Syntax

<div data-win-control="WinJS.UI.ListView" 
    data-win-options="{onselectionchanged : handler}">
</div>
function handler(eventInfo) { /* Your code */ }
 
// addEventListener syntax
listView.addEventListener("selectionchanged", handler);
listView.removeEventListener("selectionchanged", handler);
 
- or -

listView.onselectionchanged = handler;

Event information

Synchronous No
Bubbles Yes
Cancelable No

 

Event handler parameters

  • eventInfo
    Type: CustomEvent**

    An object that contains info about the event. The detail property of this object is null. To obtain the selected items, use the ListView.selection property.

Remarks

Warning  Don't make changes to the itemDataSource or groupDataSource in your event handler. Instead, call setImmediate from your event handler and pass it a function that makes the changes to the data source.

 

ListView events execute synchronously, so avoid performing expensive operations in your event handler. Performing expensive operations in your event handler can make the ListView seem unresponsive to the user. If you need the event handler to perform an expensive operation, call setImmediate from your event handler and pass it a function that performs the operation or perform the operation as an asynchronous operation. For more info about asynchronous programming, see Asynchronous programming in JavaScript.

This event doesn't fire if you delete the data source or reload it.

Setting event handlers declaratively (in HTML)

To set the event handler declaratively, it must be accessible to the global scope, and you must also call WinJS.Utilities.markSupportedForProcessing or WinJS.UI.eventHandler on the handler. You can make the handler accessible to the global scope by using WinJS.Namespace.define. For more information, see How to set event handlers declaratively.

Updating your AppBar when the selection changes

When the ListView selection changes or is the process of changing, consider the right way to update the your AppBar. For example, if the user selects a ListView item that contains an image, you might display a custom AppBar that shows image editing commands and set the AppBar controls sticky property to true so that it won’t light dismiss. For info about creating custom AppBar controls, see Quickstart: Adding a custom app bar. For general guidelines for using AppBar controls (such as when to use sticky mode), see the Guidelines and checklist for app bars.

Examples

This example demonstrates how to use the selection property to select all items in a ListView, clear the selection, and display the number of items selected.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>selectionExample</title>

    <!-- WinJS references -->
    <link href="//Microsoft.WinJS.2.0/css/ui-light.css" rel="stylesheet" />
    <script src="//Microsoft.WinJS.2.0/js/base.js"></script>
    <script src="//Microsoft.WinJS.2.0/js/ui.js"></script>

    <link href="selectionExample.css" rel="stylesheet" />
    <script src="selectionExample.js"></script>
    <script src="/js/data.js"></script>
</head>
<body>
        <div id="mediumListIconTextTemplate" data-win-control="WinJS.Binding.Template">
            <div style="width: 282px; height: 70px; padding: 5px; overflow: hidden; display: -ms-grid;">

                <!-- Displays the "picture" field. -->
                <img data-win-bind="alt: title; src: picture"
                    src="#"
                    style="width: 60px; height: 60px; margin: 5px; -ms-grid-column: 1;" />
                <div style="margin: 5px; -ms-grid-column: 2">

                    <!-- Displays the "title" field. -->
                    <h4 data-win-bind="innerText: title"></h4>

                    <!-- Displays the "text" field. -->
                    <h6 data-win-bind="innerText: text"></h6>
                </div>
            </div>
        </div>

        <div id="selectionExampleListView" data-win-control="WinJS.UI.ListView"
            data-win-options="{itemDataSource : DataExample.itemList.dataSource, 
            itemTemplate: select('#mediumListIconTextTemplate'),
            tapBehavior: 'toggleSelect',
            layout : {type: WinJS.UI.GridLayout}}">
        </div>

        <button id="selectAllButton">Select all</button>
        <button id="clearSelection">Clear selection</button>
        <p>Items selected:</p>
        <p id="outputParagraph"></p>
</body>
</html>
(function () {
    "use strict";

    var lView;
    var outputParagraph;


    WinJS.UI.Pages.define("/pages/selection/selectionExample.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {

            lView = element.querySelector("#selectionExampleListView").winControl;
            outputParagraph = element.querySelector("#outputParagraph");
            lView.addEventListener("selectionchanged", this.selectionChanged); 

            element.querySelector("#selectAllButton").addEventListener("click", this.selectAllClicked);
            element.querySelector("#clearSelection").addEventListener("click", this.clearSelection);
            
        },

        unload: function () {
 
        },

        updateLayout: function (element, viewState, lastViewState) {
 
        },

        selectAllClicked: function (eventInfo) {
            lView.selection.selectAll();
        },

        clearSelection: function (eventInfo) {
            lView.selection.clear();
        },

        selectionChanged: function (eventInfo) {
            outputParagraph.innerText = lView.selection.count(); 
        }
    });

})();

Requirements

Minimum WinJS version

WinJS 3.0

Namespace

WinJS.UI

See also

ListView

detail

WinJS.Namespace.define

WinJS.UI.eventHandler

WinJS.Utilities.markSupportedForProcessing

XAML ListView and GridView customizing interactivity sample (Windows)