diff --git a/app/ns-ui-widgets-category/list-view/basics/article.md b/app/ns-ui-widgets-category/list-view/basics/article.md index 3ecf95b..389aab9 100644 --- a/app/ns-ui-widgets-category/list-view/basics/article.md +++ b/app/ns-ui-widgets-category/list-view/basics/article.md @@ -2,11 +2,24 @@ The example demonstrates how to set a `ListView` component in XML page and how t In the sample is shown how to define custom UI while using ListView's `itemTemplate`. - - -> Note: Note, that changing the array after the list view is shown will not update the UI. You can force-update the UI using the refresh() method. +> **Note:** The ListView's item template can contain only a single root view container. + +In the example above, the items source property (`myTitles`) is an array and its members are not observable objects. This means that adding or removing array member won't trigger a property change. To update the UI, you will need to explicitly refresh the listview after an item is added or removed while using the `refresh` method. + + +> **Tip:** Instead of manually triggering the UI update with the help of ListView's `refresh` method, NativeScript provides the `ObservableArray`. Using an `ObservableArray` for your listview's items surce will make its members an observable objects and adding/removing an array item will automatically update the UI. +```TypeScript +const ObservableArray = require("tns-core-modules/data/observable-array").ObservableArray; +// Change the items source from Array to the NativeScript's ObservableArray +const titlesArray = new ObservableArray([ + { title: "The Da Vinci Code" }, + { title: "Harry Potter and the Chamber of Secrets" }, + { title: "The Alchemist" }, + { title: "The Godfather" }, + { title: "Goodnight Moon" }, + { title: "The Hobbit" } +]); +``` - -> Note: When using ObservableArray the list view will be automatically updated when items are added or removed form the array. \ No newline at end of file diff --git a/app/ns-ui-widgets-category/list-view/basics/basics-page.js b/app/ns-ui-widgets-category/list-view/basics/basics-page.js index 36e288d..fed6b3f 100644 --- a/app/ns-ui-widgets-category/list-view/basics/basics-page.js +++ b/app/ns-ui-widgets-category/list-view/basics/basics-page.js @@ -1,79 +1,42 @@ -const observableModule = require("tns-core-modules/data/observable"); -const observableArrayModule = require("tns-core-modules/data/observable-array"); -const dialogs = require("tns-core-modules/ui/dialogs"); // >> list-view-create-code -const colors = ["red", "green", "blue"]; -const secondArray = new observableArrayModule.ObservableArray( - [ - { - title:"The Da Vinci Code" - }, - { - title:"Harry Potter and the Chamber of Secrets" - }, - { - title:"The Alchemist" - }, - { - title:"The Godfather" - }, - { - title:"Goodnight Moon" - }, - { - title:"The Hobbit" - } - ]); +const Observable = require("tns-core-modules/data/observable").Observable; +const titlesArray = [ + { title: "The Da Vinci Code" }, + { title: "Harry Potter and the Chamber of Secrets" }, + { title: "The Alchemist" }, + { title: "The Godfather" }, + { title: "Goodnight Moon" }, + { title: "The Hobbit" } +]; + function onNavigatingTo(args) { const page = args.object; - const vm = new observableModule.Observable(); + const vm = new Observable(); - vm.set("myItems", colors); - vm.set("mySecondItems", secondArray); + // Setting the listview binding source + vm.set("myTitles", titlesArray); page.bindingContext = vm; } +exports.onNavigatingTo = onNavigatingTo; + +function onItemTap(args) { + const index = args.index; + console.log(`Second ListView item tap ${index}`); +} +exports.onItemTap = onItemTap; // << list-view-create-code + function onTap(args) { const page = args.object.page; - const listView = page.getViewById("firstListView"); - // >> list-view-array - colors.push("yellow"); - // Manually trigger the update so that the new color is shown. - listView.refresh(); - // << list-view-array -} + // >> list-view-refresh + const listView = page.getViewById("listView"); -function onSecondTap(args) { - // >> list-view-observable-array - secondArray.push( - { - title:"Alice's Adventures in Wonderland" - } - ); - // The ListView will be updated automatically. - // << list-view-observable-array -} - -function listViewItemTap(args) { - const index = args.index; - dialogs.alert(`ListView item tap ${index}`) - .then(() => { - console.log("Dialog closed!"); - }); -} + titlesArray.push({ title: "Game of Thrones" }); +// Manually trigger the update so that the new color is shown. +listView.refresh(); + // << list-view-refresh -function secondListViewItemTap(args) { - const index = args.index; - dialogs.alert(`Second ListView item tap ${index}`) - .then(() => { - console.log("Dialog closed!"); - }); } - -exports.onNavigatingTo = onNavigatingTo; exports.onTap = onTap; -exports.onSecondTap = onSecondTap; -exports.listViewItemTap = listViewItemTap; -exports.secondListViewItemTap = secondListViewItemTap; diff --git a/app/ns-ui-widgets-category/list-view/basics/basics-page.xml b/app/ns-ui-widgets-category/list-view/basics/basics-page.xml index f17e08d..ca52c73 100644 --- a/app/ns-ui-widgets-category/list-view/basics/basics-page.xml +++ b/app/ns-ui-widgets-category/list-view/basics/basics-page.xml @@ -3,21 +3,18 @@ - - +