IDBCursorWithValue: thuộc tính value

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Note: This feature is available in Web Workers.

Thuộc tính chỉ đọc value của giao diện IDBCursorWithValue trả về giá trị của con trỏ hiện tại, bất kể đó là gì.

Giá trị

Giá trị của con trỏ hiện tại.

Ví dụ

Trong ví dụ này, chúng ta tạo một giao dịch, lấy một object store, sau đó sử dụng con trỏ để lặp qua tất cả các bản ghi trong object store. Trong mỗi lần lặp, chúng ta ghi giá trị của con trỏ với cursor.value.

Con trỏ không yêu cầu chúng ta chọn dữ liệu dựa trên khóa; chúng ta có thể lấy tất cả. Cũng lưu ý rằng trong mỗi lần lặp, bạn có thể lấy dữ liệu từ bản ghi hiện tại dưới đối tượng con trỏ bằng cursor.value.foo. Để có ví dụ hoạt động đầy đủ, xem ví dụ IDBCursor (xem ví dụ trực tiếp.)

js
function displayData() {
  const transaction = db.transaction(["rushAlbumList"], "readonly");
  const objectStore = transaction.objectStore("rushAlbumList");

  objectStore.openCursor().onsuccess = (event) => {
    const cursor = event.target.result;
    if (cursor) {
      const listItem = document.createElement("li");
      listItem.textContent = `${cursor.value.albumTitle}, ${cursor.value.year}`;
      list.appendChild(listItem);

      console.log(cursor.value);
      cursor.continue();
    } else {
      console.log("Entries all displayed.");
    }
  };
}

Thông số kỹ thuật

Thông số kỹ thuật
Indexed Database API 3.0
# ref-for-dom-idbcursorwithvalue-value①

Tương thích trình duyệt

Xem thêm