aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/idbobjectstore/indexnames/index.html
blob: c124f70a9bcee4d74b965472fb8beeaa1e9c5638 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
---
title: IDBObjectStore.indexNames
slug: Web/API/IDBObjectStore/indexNames
translation_of: Web/API/IDBObjectStore/indexNames
---
<p>{{ APIRef("IndexedDB") }}</p>

<div>
<p>{{domxref("IDBObjectStore")}} 的只读属性 <strong><code>indexNames</code></strong> 返回此对象存储中对象的 <a href="https://developer.mozilla.org/en/IndexedDB#gloss_index" title="en/IndexedDB#gloss index">indexes</a> 名称(name)列表。</p>

<p>{{AvailableInWorkers}}</p>
</div>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">var <em>myindexNames</em> = <em>objectStore</em>.indexNames;</pre>

<h3 id="Value">Value</h3>

<p>一个 {{domxref("DOMStringList")}}.</p>

<h2 id="Example">Example</h2>

<p>在下面的代码片段中,我们在数据库上打开一个读/写事务并使用 <code>add()</code> 向对象存储添加一些数据。创建对象存储后,我们将打印 <span style="background-color: #fafbfc; font-family: consolas,monaco,andale mono,monospace; font-size: 1rem; line-height: 19px; white-space: pre;"><code>objectStore.indexNames</code></span> 到控制台。有关完整的工作示例,请参阅我们的 <a href="https://github.com/mdn/to-do-notifications/">待办事项通知</a>应用程序<span style="line-height: 1.5;"> ( </span><a href="http://mdn.github.io/to-do-notifications/">实时查看示例</a><span style="line-height: 1.5;"> )</span></p>

<pre class="brush: js" style="font-size: 14px;">// 让我们来打开我们的数据库
<span style="line-height: 1.5;">var DBOpenRequest = window.indexedDB.open("toDoList", 4);

</span><span style="line-height: 1.5;">DBOpenRequest.onsuccess = function(event) {</span>
  note.innerHTML += '&lt;li&gt;Database initialised.&lt;/li&gt;';

  // 将打开数据库的结果存储在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 += '&lt;li&gt;Transaction completed.&lt;/li&gt;';
  };


  transaction.onerror = function(event) {
  note.innerHTML += '&lt;li&gt;Transaction not opened due to error. Duplicate items not allowed.&lt;/li&gt;';
  };

  // 在事务上创建对象存储
  var objectStore = transaction.objectStore("toDoList");
  console.log(objectStore.indexNames);

  // 请求将 newItem 对象 添加到对象存储区
  var objectStoreRequest = objectStore.add(newItem[0]);

  objectStoreRequest.onsuccess = function(event) {
    // 报告我们请求的成功
    note.innerHTML += '&lt;li&gt;Request successful.&lt;/li&gt;';
  };
};</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">解释</th>
  </tr>
  <tr>
   <td>{{SpecName('IndexedDB', '#dom-idbobjectstore-indexnames', 'indexNames')}}</td>
   <td>{{Spec2('IndexedDB')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName("IndexedDB 2", "#dom-idbobjectstore-indexnames", "indexNames")}}</td>
   <td>{{Spec2("IndexedDB 2")}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>

<div>


<p>{{Compat("api.IDBObjectStore.indexNames")}}</p>
</div>

<h2 id="查看其它内容">查看其它内容</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">使用 IndexedDB</a></li>
 <li><span id="w_40">启动</span><span id="w_41">事务</span> : {{domxref("IDBDatabase")}}</li>
 <li>使用事务 : {{domxref("IDBTransaction")}}</li>
 <li>设置键的范围 : {{domxref("IDBKeyRange")}}</li>
 <li>检索和更改数据 : {{domxref("IDBObjectStore")}}</li>
 <li>使用游标 : {{domxref("IDBCursor")}}</li>
 <li>参考示例 : <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li>
</ul>