-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
91 lines (71 loc) · 3.35 KB
/
index.md
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
---
title: "IDBIndex: multiEntry property"
short-title: multiEntry
slug: Web/API/IDBIndex/multiEntry
page-type: web-api-instance-property
browser-compat: api.IDBIndex.multiEntry
---
{{ APIRef("IndexedDB") }} {{AvailableInWorkers}}
The **`multiEntry`** read-only property of the
{{domxref("IDBIndex")}} interface returns a boolean value that affects how the index
behaves when the result of evaluating the index's key path yields an array.
This is decided when the index is created, using the
{{domxref("IDBObjectStore.createIndex")}} method. This method takes an optional
`options` parameter whose `multiEntry` property is set to `true`/`false`.
## Value
A boolean value:
| Value | Effect |
| ----- | ------------------------------------------------------------------- |
| true | There is one record in the index for each item in an array of keys. |
| false | There is one record for each key that is an array. |
## Examples
In the following example we open a transaction and an object store, then get the
index `lName` from a simple contacts database. We then open a basic cursor on
the index using {{domxref("IDBIndex.openCursor")}} — this works the same as opening a
cursor directly on an `ObjectStore` using
{{domxref("IDBObjectStore.openCursor")}} except that the returned records are sorted
based on the index, not the primary key.
The multi-entry status of the index is logged to the console: it should be returned
as `false`.
Finally, we iterate through each record, and insert the data into an HTML table. For a
complete working example, see our [IndexedDB-examples demo repo](https://github.com/mdn/dom-examples/tree/main/indexeddb-examples/idbindex) ([View the example live](https://mdn.github.io/dom-examples/indexeddb-examples/idbindex/)).
```js
function displayDataByIndex() {
tableEntry.innerHTML = "";
const transaction = db.transaction(["contactsList"], "readonly");
const objectStore = transaction.objectStore("contactsList");
const myIndex = objectStore.index("lName");
console.log(myIndex.multiEntry);
myIndex.openCursor().onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
const tableRow = document.createElement("tr");
tableRow.innerHTML =
`<td>${cursor.value.id}</td>` +
`<td>${cursor.value.lName}</td>` +
`<td>${cursor.value.fName}</td>` +
`<td>${cursor.value.jTitle}</td>` +
`<td>${cursor.value.company}</td>` +
`<td>${cursor.value.eMail}</td>` +
`<td>${cursor.value.phone}</td>` +
`<td>${cursor.value.age}</td>`;
tableEntry.appendChild(tableRow);
cursor.continue();
} else {
console.log("Entries all displayed.");
}
};
}
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Using IndexedDB](/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB)
- Starting transactions: {{domxref("IDBDatabase")}}
- Using transactions: {{domxref("IDBTransaction")}}
- Setting a range of keys: {{domxref("IDBKeyRange")}}
- Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}
- Using cursors: {{domxref("IDBCursor")}}
- Reference example: [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) ([View the example live](https://mdn.github.io/dom-examples/to-do-notifications/)).