2012-07-15 3 views
6

Desidero ordinare i risultati ottenuti da indexedDB.
Ogni record ha struttura {id, text, date} dove 'id' è il keyPath.Ordinamento dei risultati di una query IndexedDB

Desidero ordinare i risultati per data.

mio codice attuale è la seguente:

var trans = db.transaction(['msgs'], IDBTransaction.READ); 
    var store = trans.objectStore('msgs'); 

    // Get everything in the store; 
    var keyRange = IDBKeyRange.lowerBound(""); 
    var cursorRequest = store.openCursor(keyRange); 

    cursorRequest.onsuccess = function(e) { 
    var result = e.target.result; 
    if(!!result == false){ 
     return; 
    } 
    console.log(result.value); 
    result.continue(); 
    }; 
+0

-Dove è la query SQL - Scusa? , colpa mia - ho pensato a WebSQL! – Oliver

+0

Vedere http://stackoverflow.com/questions/12084177/in-indexeddb-is-there-a---a-make-a-sorted-compound-query/15625231#15625231 In breve, utilizzare una serie di chiavi come un indice. – 173210

risposta

-4

Grazie a zomg, hughfdjackson di javascript IRC, ho risolto la matrice finale. codice modificato come segue:

var trans = db.transaction(['msgs'], IDBTransaction.READ); 
var store = trans.objectStore('msgs'); 

// Get everything in the store; 
var keyRange = IDBKeyRange.lowerBound(""); 
var cursorRequest = store.openCursor(keyRange); 

var res = new Array(); 

cursorRequest.onsuccess = function(e) { 
    var result = e.target.result; 
    if(!!result == false){ 
     **res.sort(function(a,b){return Number(a.date) - Number(b.date);});** 
     //print res etc.... 
     return; 
    } 
    res.push(result.value); 
    result.continue(); 
}; 
+7

Questo tipo di manca l'intero punto di utilizzo di indexedDB. Si consiglia di utilizzare un "indice" indexedDB per ordinare in base a una proprietà chiave non primaria. È quindi possibile aprire un cursore su un indice e iterare in uno dei quattro modi (next, prev, nextUnique, prevUnique). La scelta dell'ordinamento non nativo non è ottimale. – Josh

+0

Questo ha senso. Grazie! Lo terrò a mente quando userò indexedDB la prossima volta. –

+2

Questa risposta non è la più corretta. – buley

14

In realtà si devono indicizzare il campo date nel msgs ObjectStore e aprire un cursore indice sul ObjectStore.

var cursorRequest = store.index('date').openCursor(null, 'next'); // or prev 

Questo otterrà il risultato ordinato. Ecco come dovrebbero essere usati gli indici.

6

Ecco il modo più efficiente suggerito da Josh.

Supponendo si è creato un indice su "Data":

// Use the literal "readonly" instead of IDBTransaction.READ, which is deprecated: 
var trans = db.transaction(['msgs'], "readonly"); 
var store = trans.objectStore('msgs'); 
var index = store.index('date'); 

// Get everything in the store: 
var cursorRequest = index.openCursor(); 
// It's the same as: 
// var cursorRequest = index.openCursor(null, "next"); 
// Or, if you want a "descendent ordering": 
// var cursorRequest = index.openCursor(null, "prev"); 
// Note that there's no need to define a key range if you want all the objects 

var res = new Array(); 

cursorRequest.onsuccess = function(e) { 

    var cursor = e.target.result; 
    if (cursor) { 
     res.push(cursor.value); 
     cursor.continue(); 
    } 
    else { 
     //print res etc.... 
    } 
}; 

più sul cursore direzione qui: http://www.w3.org/TR/IndexedDB/#cursor-concept

IDBIndex API è qui: http://www.w3.org/TR/IndexedDB/#idl-def-IDBIndex