从 Indexdb 填充列表视图
Populate ListView from Indexdb
我正在使用Windows 8.1 winJS,并尝试从indexedb填充listView,但无法使其工作。我从 jsonStr 第 76 行获得一个字符串,但我无法绑定到 html。
我的JavaScript
(function () {
"use strict";
var nav = WinJS.Navigation;
var session = WinJS.Application.sessionState;
var util = WinJS.Utilities;
WinJS.Namespace.define("DataExampleA",
{
itemList: new WinJS.Binding.List()
})
var plantsData1 = [
{ id: 1, title: "Banana blast1", text: "Ice cream", picture: "images/plants/Acalypha pendula.jpg" },
{ id: 2, title: "Banana blast2", text: "Ice cream", picture: "images/plants/Acalypha pendula.jpg" },
{ id: 3, title: "Banana blast3", text: "Ice cream", picture: "images/plants/Acalypha pendula.jpg" }
];
var indexedDB = window.msIndexedDB;
var IDBTransaction = window.IDBTransaction;
if (!window.indexedDB) {
window.console.log("Your browser doesn't support a stable version of IndexedDB.")
}
var db;
var request = window.msIndexedDB.open("plantsDatabase", 1);
request.onerror = function (event) {
console.log("error: ");
};
request.onsuccess = function (event) {
db = request.result;
console.log("success: " + db);
};
request.onupgradeneeded = function (event) {
console.log('Creating object stores');
var db = event.target.result;
var objectStore = db.createObjectStore("plantsStore", { keyPath: "id" });
for (var i in plantsData1) {
objectStore.add(plantsData1[i]);
}
}
function read() {
var transaction = db.transaction(["plantsStore"]);
var objectStore = transaction.objectStore("plantsStore");
var request = objectStore.get("00-03");
request.onerror = function (event) {
console.log("Unable to retrieve daa from database!");
};
request.onsuccess = function (event) {
// Do something with the request.result!
if (request.result) {
// console.log("Name: " + request.result.name + ", Age: " + request.result.age + ", Email: " + request.result.email);
} else {
console.log("Kenny couldn't be found in your database!");
}
};
}
function buttonreadAll(plantsBind) {
var transaction = db.transaction(["plantsStore"]);
var objectStore = transaction.objectStore("plantsStore");
objectStore.openCursor().onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
var plantsBind = cursor.value;
var jsonStr = JSON.stringify(plantsBind);
console.log(jsonStr);
var dataList = new WinJS.Binding.List(cursor.value);
// Create a namespace to make the data publicly accessible.
var publicMembers =
{
itemList: dataList
};
WinJS.Namespace.define("DataExampleA", publicMembers);
cursor.continue();
}
else {
console.log("No more entries!");
}
};
}
WinJS.UI.Pages.define("/pages/hub/hub.html", {
processed: function (element) {
return WinJS.Resources.processAll(element);
},
// 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) {
var readAll = document.getElementById("readAll");
readAll.addEventListener("click", buttonreadAll, false);
},
unload: function () {
// TODO: Respond to navigations away from this page.
},
updateLayout: function (element) {
/// <param name="element" domElement="true" />
// TODO: Respond to changes in layout.
},
});
})();
和我的网页
<div class="section3" data-win-control="WinJS.UI.HubSection" data-win-options="{ isHeaderStatic: true }" data-win-res="{ winControl: {'header': 'Section3'} }">
<div id="IconTextTemplate1" class="win-container win-itembox win-item" data-win-control="WinJS.Binding.Template">
<div style=" padding: 5px; overflow: hidden; display: -ms-grid;">
<img data-win-bind="alt: title; src: picture" src="#" style="width: 80%; margin-left: 10%; margin-right:10%; margin-bottom:2%; -ms-grid-column: 1;" />
<div style="margin: 15px; -ms-grid-row: 2">
<h3 data-win-bind="innerText: title"></h3>
<h3 data-win-bind="innerText: picture"></h3>
<h6 data-win-bind="innerText: text"></h6>
</div>
</div>
</div>
<div id="basicListView" data-win-control="WinJS.UI.ListView"
data-win-options="{
itemDataSource : DataExampleA.itemList.dataSource,
itemTemplate: select('#IconTextTemplate1'),
layout : {type: WinJS.UI.GridLayout}}">
</div>
</div>
要添加到 Kristof 的响应中,您需要替换以下代码:
var dataList = new WinJS.Binding.List(cursor.value);
// Create a namespace to make the data publicly accessible.
var publicMembers =
{
itemList: dataList
};
WinJS.Namespace.define("DataExampleA", publicMembers);
仅使用此行,因为您已经创建了一个列表:
DataExampleA.itemList.push(cursor.value);
我在一个项目中尝试了这个,它工作得很好。
那么问题是如何让 ListView 在向数据库添加内容时自行更新。答案是,如果将项目添加到 ListView 绑定到的 DataExampleA.itemList,这将更新 ListView。这意味着您需要有一个例程,在数据库更改时更新列表,基本上在应用程序运行时并行维护两者。当然,当应用程序重新启动时,您将从数据库重新初始化 Binding.List,以便它们同步启动。
您可以采取的另一种方法是在 IndexedDB 之上创建自定义数据源,方法是通过使用 IListDataAdapter 接口方法的某些部分实现对象来实现,然后使用它来创建 WinJS.UI.VirtualizedDataSource。这需要做更多的工作,但如果您预计会有大型数据集,则会更好地扩展。有关此内容的详细信息,请参阅我的第二本预览电子书(免费)的第 7 章"集合控制数据源"部分中。
您正在为检索到的每个项目创建一个新列表。
使用推送方法将新项添加到列表视图集合。
我不是 100% 确定这会起作用。我也尝试了一段时间,但没有按照我想要的方式工作。我相信我遇到的问题是,当我添加数据时,事件没有触发,这样它们就不会被可视化。
不要使用全局数据库变量。
- 无法在mvc视图中使用ajax调用获取操作返回的模型对象列表
- 如何将参数从列表视图中的项传递到模板
- 根据手机上是否存在文件,在jQuery mobile中动态填充列表视图
- 如何在我传递给视图的ViewModel集合中获得jQuery可选对象的值,以作为有序列表进行迭代
- 取消选择滚动启动时的所有列表视图项目
- 使用PhoneGap和jQuery Mobile在android设备上显示动态列表视图
- 通过格式化将jquery移动组件动态添加到列表视图中
- 如何让li项目在这个jQuery列表中滑动和/或淡出视图
- jqmobile列表视图中元素之间的间距相等
- 如何在Vue.js中使用列表视图
- jQuery移动列表视图和面板
- 剑道移动列表视图更改过滤器onclick
- 无法将对象列表从视图模型设置为 javascript 变量
- Metro 应用程序中的列表视图项目单击
- 列表视图在Android上滚动断断续续且缓慢
- 带有砖石结构的轴网视图/列表视图
- 在不涉及控制器的情况下对MVC视图列表中的项进行排序
- 网格视图列表视图切换
- 推送到数组不会通过ng repeat指令更新视图列表
- Javascript / jquery在两个树视图列表之间拖放