2012-07-15 21 views
6

के परिणामों को सॉर्ट करना मैं अनुक्रमित डीबी से प्राप्त परिणामों को सॉर्ट करना चाहता हूं।
प्रत्येक रिकॉर्ड में संरचना {आईडी, टेक्स्ट, दिनांक} है जहां 'आईडी' कीपैथ है।अनुक्रमित डीबी क्वेरी

मैं तारीख के अनुसार परिणामों को सॉर्ट करना चाहता हूं।

मेरे वर्तमान कोड के रूप में नीचे है:

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

-Where अपने SQL क्वेरी है - क्षमा करें? , मेरी गलती - मैंने WebSQL के बारे में सोचा! – Oliver

+0

देखें http://stackoverflow.com/questions/12084177/in-indexeddb-is-there-a-way-to-make-a-sorted-compound-query/15625231#15625231 संक्षेप में, कुंजी की सरणी का उपयोग करें एक सूचकांक के रूप में। – 173210

उत्तर

-4

ZOMG के लिए, जावास्क्रिप्ट irc के hughfdjackson धन्यवाद, मैं अंतिम सरणी सॉर्ट किया। संशोधित कोड के रूप में नीचे:

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

इस प्रकार की अनुक्रमणिका अनुक्रमित डीबी का उपयोग करने के पूरे बिंदु को याद करती है। गैर-प्राथमिक-कुंजी संपत्ति द्वारा क्रमबद्ध करने के लिए आप एक अनुक्रमित डीबी 'इंडेक्स' का उपयोग करना चाहते हैं। फिर आप एक कर्सर को एक इंडेक्स पर खोल सकते हैं और चार तरीकों में से एक में पुनरावृत्ति कर सकते हैं (अगला, पिछला, अगला यूनिक, prevUnique)। गैर-मूल रूप से सॉर्ट करने की आपकी पसंद इष्टतम नहीं है। – Josh

+0

यह समझ में आता है। धन्यवाद! अगली बार अनुक्रमित डीबी का उपयोग करते समय मैं इसे ध्यान में रखूंगा। –

+2

यह उत्तर सबसे सही नहीं है। – buley

14

वास्तव में आप सूचकांक को msgs ObjectStore में date क्षेत्र है और ObjectStore पर एक सूचकांक कर्सर खोलें।

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

यह क्रमबद्ध परिणाम प्राप्त करेगा। इस तरह इंडेक्स का उपयोग किया जाना चाहिए।

6

जोश द्वारा सुझाए गए अधिक कुशल तरीके यहां दिए गए हैं।

जाना चाहिए तुम्हें "तिथि" पर एक सूचकांक बनाया:

// 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.... 
    } 
}; 

कर्सर दिशा यहां पर अधिक: http://www.w3.org/TR/IndexedDB/#cursor-concept

IDBIndex एपीआई यहाँ है: http://www.w3.org/TR/IndexedDB/#idl-def-IDBIndex