--- title: IDBCursor slug: Web/API/IDBCursor translation_of: Web/API/IDBCursor ---
{{APIRef("IndexedDB")}}
O IDBCursor
é uma interface da API IndexedDB que representa o cursor para atravessar ou interagir sobre vários registros em um banco de dados.
O cursor tem uma fonte que indica qual índice ou armazenamento o objeto está sobre a iteração. Ele tem uma posição dentro do intervalo, e move-se numa direcção que é aumentar ou diminuir na ordem de chaves ficha. O cursor permite que um aplicativo para processar de forma assíncrona todos os registros na faixa do cursor.
Pode ter um número ilimitado de cursores ao mesmo tempo. Você sempre consegue o mesmo objeto IDBCursor representando um determinado cursor. As operações são realizadas na loja de índice ou objeto subjacente.
{{AvailableInWorkers}}
chave
opcional.undefined
. The cursor's key can be any data type.These constants are no longer available. You should use the string constants directly instead. ({{ bug(891944) }})
NEXT
: "next"
: The cursor shows all records, including duplicates. It starts at the lower bound of the key range and moves upwards (monotonically increasing in the order of keys).NEXTUNIQUE
: "nextunique"
: The cursor shows all records, excluding duplicates. If multiple records exist with the same key, only the first one iterated is retrieved. It starts at the lower bound of the key range and moves upwards.PREV
: "prev"
: The cursor shows all records, including duplicates. It starts at the upper bound of the key range and moves downwards (monotonically decreasing in the order of keys).PREVUNIQUE
: "prevunique"
: The cursor shows all records, excluding duplicates. If multiple records exist with the same key, only the first one iterated is retrieved. It starts at the upper bound of the key range and moves downwards.In this simple fragment we create a transaction, retrieve an object store, then use a cursor to iterate through all the records in the object store. The cursor does not require us to select the data based on a key; we can just grab all of it. Also note that in each iteration of the loop, you can grab data from the current record under the cursor object using cursor.value.foo
. For a complete working example, see our IDBCursor example (view example live.)
function displayData() {
var transaction = db.transaction(['rushAlbumList'], "readonly");
var objectStore = transaction.objectStore('rushAlbumList');
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if(cursor) {
var listItem = document.createElement('li');
listItem.innerHTML = cursor.value.albumTitle + ', ' + cursor.value.year;
list.appendChild(listItem);
cursor.continue();
} else {
console.log('Entries all displayed.');
}
};
};
Specification | Status | Comment |
---|---|---|
{{SpecName('IndexedDB', '#idl-def-IDBCursor', 'cursor')}} | {{Spec2('IndexedDB')}} |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 23{{property_prefix("webkit")}} 24 |
10 {{property_prefix("moz")}} {{CompatGeckoDesktop("16.0")}} |
10, partial | 15 | 7.1 |
Available in workers | {{CompatVersionUnknown}} | {{CompatGeckoMobile("37.0")}} | {{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatUnknown}} |
Feature | Android | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 4.4 | {{CompatGeckoMobile("22.0")}} | 1.0.1 | 10 | 22 | 8 |
Available in workers | {{CompatVersionUnknown}} | {{CompatGeckoMobile("37.0")}} | {{CompatVersionUnknown}} | {{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatUnknown}} |
Be careful in Chrome as it still implements the old specification along with the new one. Similarly it still has the prefixed webkitIndexedDB
property even if the unprefixed indexedDB
is present.