From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/idbobjectstore/indexnames/index.html | 110 +++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 files/zh-cn/web/api/idbobjectstore/indexnames/index.html (limited to 'files/zh-cn/web/api/idbobjectstore/indexnames') diff --git a/files/zh-cn/web/api/idbobjectstore/indexnames/index.html b/files/zh-cn/web/api/idbobjectstore/indexnames/index.html new file mode 100644 index 0000000000..c124f70a9b --- /dev/null +++ b/files/zh-cn/web/api/idbobjectstore/indexnames/index.html @@ -0,0 +1,110 @@ +--- +title: IDBObjectStore.indexNames +slug: Web/API/IDBObjectStore/indexNames +translation_of: Web/API/IDBObjectStore/indexNames +--- +

{{ APIRef("IndexedDB") }}

+ +
+

{{domxref("IDBObjectStore")}} 的只读属性 indexNames 返回此对象存储中对象的 indexes 名称(name)列表。

+ +

{{AvailableInWorkers}}

+
+ +

Syntax

+ +
var myindexNames = objectStore.indexNames;
+ +

Value

+ +

一个 {{domxref("DOMStringList")}}.

+ +

Example

+ +

在下面的代码片段中,我们在数据库上打开一个读/写事务并使用 add() 向对象存储添加一些数据。创建对象存储后,我们将打印 objectStore.indexNames 到控制台。有关完整的工作示例,请参阅我们的 待办事项通知应用程序 ( 实时查看示例 )

+ +
// 让我们来打开我们的数据库
+var DBOpenRequest = window.indexedDB.open("toDoList", 4);
+
+DBOpenRequest.onsuccess = function(event) {
+  note.innerHTML += '<li>Database initialised.</li>';
+
+  // 将打开数据库的结果存储在db变量中
+  // 下面经常用到这个
+  db = this.result;
+
+  // 运行 addData() 函数将数据添加到数据库
+  addData();
+};
+
+function addData() {
+  // 创建一个新对象以准备插入到IDB中
+  var newItem = [ { taskTitle: "Walk dog", hours: 19, minutes: 30, day: 24, month: "December", year: 2013, notified: "no" } ];
+
+  // 打开读/写数据库事务,准备添加数据
+  var transaction = db.transaction(["toDoList"], "readwrite");
+
+  // 当所有事情都完成时,报告事务完成的成功情况
+  transaction.oncomplete = function(event) {
+    note.innerHTML += '<li>Transaction completed.</li>';
+  };
+
+
+  transaction.onerror = function(event) {
+  note.innerHTML += '<li>Transaction not opened due to error. Duplicate items not allowed.</li>';
+  };
+
+  // 在事务上创建对象存储
+  var objectStore = transaction.objectStore("toDoList");
+  console.log(objectStore.indexNames);
+
+  // 请求将 newItem 对象 添加到对象存储区
+  var objectStoreRequest = objectStore.add(newItem[0]);
+
+  objectStoreRequest.onsuccess = function(event) {
+    // 报告我们请求的成功
+    note.innerHTML += '<li>Request successful.</li>';
+  };
+};
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
规范状态解释
{{SpecName('IndexedDB', '#dom-idbobjectstore-indexnames', 'indexNames')}}{{Spec2('IndexedDB')}}
{{SpecName("IndexedDB 2", "#dom-idbobjectstore-indexnames", "indexNames")}}{{Spec2("IndexedDB 2")}}
+ +

浏览器兼容性

+ +
+ + +

{{Compat("api.IDBObjectStore.indexNames")}}

+
+ +

查看其它内容

+ + -- cgit v1.2.3-54-g00ecf