<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 第六節:IDBObjectStore 對象 IDBObjectStore 對象對應一個對象倉庫(object store)。`IDBDatabase.createObjectStore()`方法返回的就是一個 IDBObjectStore 對象。 IDBDatabase 對象的`transaction()`返回一個事務對象,該對象的`objectStore()`方法返回 IDBObjectStore 對象,因此可以采用下面的鏈式寫法。 ~~~ db.transaction(['test'], 'readonly') .objectStore('test') .get(X) .onsuccess = function (e) {} ~~~ ## 一、屬性 IDBObjectStore 對象有以下屬性。 * `IDBObjectStore.indexNames`:返回一個類似數組的對象(DOMStringList),包含了當前對象倉庫的所有索引。 * `IDBObjectStore.keyPath`:返回當前對象倉庫的主鍵。 * `IDBObjectStore.name`:返回當前對象倉庫的名稱。 * `IDBObjectStore.transaction`:返回當前對象倉庫所屬的事務對象。 * `IDBObjectStore.autoIncrement`:布爾值,表示主鍵是否會自動遞增。 ## 二、方法 IDBObjectStore 對象有以下方法。 ### (1)IDBObjectStore.add() `IDBObjectStore.add()`用于向對象倉庫添加數據,返回一個 IDBRequest 對象。該方法只用于添加數據,如果主鍵相同會報錯,因此更新數據必須使用`put()`方法。 ~~~ objectStore.add(value, key) ~~~ 該方法接受兩個參數,第一個參數是鍵值,第二個參數是主鍵,該參數可選,如果省略默認為`null`。 創建事務以后,就可以獲取對象倉庫,然后使用`add()`方法往里面添加數據了。 ~~~ var db; var openRequest = window.indexedDB.open('demo', 1); openRequest.onsuccess = function (event) { db = openRequest.result; var transaction = db.transaction(['items'], 'readwrite'); transaction.oncomplete = function (event) { console.log('transaction success'); }; transaction.onerror = function (event) { console.log('transaction error: ' + transaction.error); }; var objectStore = transaction.objectStore('items'); var objectStoreRequest = objectStore.add({ foo: 1 }); objectStoreRequest.onsuccess = function (event) { console.log('add data success'); }; }; ~~~ ### (2)IDBObjectStore.put() `IDBObjectStore.put()`方法用于更新某個主鍵對應的數據記錄,如果對應的鍵值不存在,則插入一條新的記錄。該方法返回一個 IDBRequest 對象。 ~~~ objectStore.put(item, key) ~~~ 該方法接受兩個參數,第一個參數為新數據,第二個參數為主鍵,該參數可選,且只在自動遞增時才有必要提供,因為那時主鍵不包含在數據值里面。 ### (3)IDBObjectStore.clear() `IDBObjectStore.clear()`刪除當前對象倉庫的所有記錄。該方法返回一個 IDBRequest 對象。 ~~~ objectStore.clear() ~~~ 該方法不需要參數。 ### (4)IDBObjectStore.delete() `IDBObjectStore.delete()`方法用于刪除指定主鍵的記錄。該方法返回一個 IDBRequest 對象。 ~~~ objectStore.delete(Key) ~~~ 該方法的參數為主鍵的值。 ### (5)IDBObjectStore.count() `IDBObjectStore.count()`方法用于計算記錄的數量。該方法返回一個 IDBRequest 對象。 ~~~ IDBObjectStore.count(key) ~~~ 不帶參數時,該方法返回當前對象倉庫的所有記錄數量。如果主鍵或 IDBKeyRange 對象作為參數,則返回對應的記錄數量。 ### (6)IDBObjectStore.getKey() `IDBObjectStore.getKey()`用于獲取主鍵。該方法返回一個 IDBRequest 對象。 ~~~ objectStore.getKey(key) ~~~ 該方法的參數可以是主鍵值或 IDBKeyRange 對象。 ### (7)IDBObjectStore.get() `IDBObjectStore.get()`用于獲取主鍵對應的數據記錄。該方法返回一個 IDBRequest 對象。 ~~~ objectStore.get(key) ~~~ ### (8)IDBObjectStore.getAll() `DBObjectStore.getAll()`用于獲取對象倉庫的記錄。該方法返回一個 IDBRequest 對象。 ~~~ // 獲取所有記錄 objectStore.getAll() // 獲取所有符合指定主鍵或 IDBKeyRange 的記錄 objectStore.getAll(query) // 指定獲取記錄的數量 objectStore.getAll(query, count) ~~~ ### (9)IDBObjectStore.getAllKeys() `IDBObjectStore.getAllKeys()`用于獲取所有符合條件的主鍵。該方法返回一個 IDBRequest 對象。 ~~~ // 獲取所有記錄的主鍵 objectStore.getAllKeys() // 獲取所有符合條件的主鍵 objectStore.getAllKeys(query) // 指定獲取主鍵的數量 objectStore.getAllKeys(query, count) ~~~ ### (10)IDBObjectStore.index() `IDBObjectStore.index()`方法返回指定名稱的索引對象 IDBIndex。 ~~~ objectStore.index(name) ~~~ 有了索引以后,就可以針對索引所在的屬性讀取數據。 ~~~ var t = db.transaction(['people'], 'readonly'); var store = t.objectStore('people'); var index = store.index('name'); var request = index.get('foo'); ~~~ 上面代碼打開對象倉庫以后,先用`index()`方法指定獲取`name`屬性的索引,然后用`get()`方法讀取某個`name`屬性(`foo`)對應的數據。如果`name`屬性不是對應唯一值,這時`get()`方法有可能取回多個數據對象。另外,`get()`是異步方法,讀取成功以后,只能在`success`事件的監聽函數中處理數據。 ### (11)IDBObjectStore.createIndex() `IDBObjectStore.createIndex()`方法用于新建當前數據庫的一個索引。該方法只能在`VersionChange`監聽函數里面調用。 ~~~ objectStore.createIndex(indexName, keyPath, objectParameters) ~~~ 該方法可以接受三個參數。 * indexName:索引名 * keyPath:主鍵 * objectParameters:配置對象(可選) 第三個參數可以配置以下屬性。 * unique:如果設為`true`,將不允許重復的值 * multiEntry:如果設為`true`,對于有多個值的主鍵數組,每個值將在索引里面新建一個條目,否則主鍵數組對應一個條目。 假定對象倉庫中的數據記錄都是如下的`person`類型。 ~~~ var person = { name: name, email: email, created: new Date() }; ~~~ 可以指定這個對象的某個屬性來建立索引。 ~~~ var store = db.createObjectStore('people', { autoIncrement: true }); store.createIndex('name', 'name', { unique: false }); store.createIndex('email', 'email', { unique: true }); ~~~ 上面代碼告訴索引對象,`name`屬性不是唯一值,`email`屬性是唯一值。 ### (12)IDBObjectStore.deleteIndex() `IDBObjectStore.deleteIndex()`方法用于刪除指定的索引。該方法只能在`VersionChange`監聽函數里面調用。 ~~~ objectStore.deleteIndex(indexName) ~~~ ### (13)IDBObjectStore.openCursor() `IDBObjectStore.openCursor()`用于獲取一個指針對象。 ~~~ IDBObjectStore.openCursor() ~~~ 指針對象可以用來遍歷數據。該對象也是異步的,有自己的`success`和`error`事件,可以對它們指定監聽函數。 ~~~ var t = db.transaction(['test'], 'readonly'); var store = t.objectStore('test'); var cursor = store.openCursor(); cursor.onsuccess = function (event) { var res = event.target.result; if (res) { console.log('Key', res.key); console.dir('Data', res.value); res.continue(); } } ~~~ 監聽函數接受一個事件對象作為參數,該對象的`target.result`屬性指向當前數據記錄。該記錄的`key`和`value`分別返回主鍵和鍵值(即實際存入的數據)。`continue()`方法將光標移到下一個數據對象,如果當前數據對象已經是最后一個數據了,則光標指向`null`。 `openCursor()`方法的第一個參數是主鍵值,或者一個 IDBKeyRange 對象。如果指定該參數,將只處理包含指定主鍵的記錄;如果省略,將處理所有的記錄。該方法還可以接受第二個參數,表示遍歷方向,默認值為`next`,其他可能的值為`prev`、`nextunique`和`prevunique`。后兩個值表示如果遇到重復值,會自動跳過。 ### (14)IDBObjectStore.openKeyCursor() `IDBObjectStore.openKeyCursor()`用于獲取一個主鍵指針對象。 ~~~ IDBObjectStore.openKeyCursor() ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看