可观察的分节数组
一个具有项目分节的观察数组
npm i --save observable-sectioned-array

可观察分节数组。

一个用于数据绑定的 NativeScript 插件,用于使用项目分节。它可以与 分节列表视图 一起使用以支持分节。

安装

在您项目的根目录下运行以下命令

$ tns plugin add observable-sectioned-array

此命令会自动安装必要的文件,并将 observable-sectioned-array 作为依赖项存储在项目中的 package.json 文件中。

示例

以下代码导入模块

var observableSectionArrayModule = require("observable-sectioned-array");

以下代码创建一个数组并向其添加分节。

    var students = [
{"name" : "Alice", gender:"female"},
{"name": "Adam", gender: "male"},
{"name": "Bob", gender: "male"},
{"name": "Brittany", gender: "female"},
{"name": "Evan", gender: "male"}
];

var boys = students.filter(function(student) { return student.gender ==="male";});

var girls = students.filter(function(student) { return student.gender ==="female";});

var sectionedArray = new observableSectionArrayModule.ObservableSectionArray();
sectionedArray.addSection("Boys", boys);
sectionedArray.addSection("Girls", girls);

sectionedArray.on(observableSectionArrayModule.ObservableSectionArray.CHANGE, function (args) {
//Fired when we add "Eve" below.
console.log("Event name: " + args.eventName + ", action: " + args.action);
//Event name: change, action: add

console.log("args.row: " + args.row + ", args.section: " + args.section + ", added count: "+args.addedCount);
//args.row: 2, args.section: null, added count: 1
});

console.log('No of sections: ' + sectionedArray.getNoOfSections());
//No of sections: 2

console.log('No of items in section 1: ' + sectionedArray.getNoOfItemsInSection(1));
//No of items in section 1: 2

console.log('Item at {1,1}: ' + JSON.stringify(sectionedArray.getItem(1,1)));
//Item at {1,1}: { "name": "Brittany", gender: "female" }

//Now add a new student "Eve" to girls after some delay.
setTimeout(function() {
sectionedArray.push([{"name": "Eve", gender:"female"}], 1);
}, 3000);