javascript - 如何从 indexeddb 中检索数据并显示在 div 中

标签 javascript jquery html css

我创建了一个 readAll() 函数,它正在将数据提取到 todos div 中,但它没有在点击函数上显示数据,但它不工作数据正在索引数据库中成功添加,但它没有显示。现在我首先清空表格,然后将数据从 javascript 追加到 html 中。

function readAll() {
  var objectStore = db.transaction("todostore").objectStore("todostore");
  $("#todos").empty();
  objectStore.openCursor().onsuccess = function(event) {
   var cursor = event.target.result;
   if (cursor) {
    $("#todos").append("<tr><td>" + cursor.key + "</td><td>" + cursor.value.todo + "</td><td><button onclick=\"update('" + cursor.key + "','" + cursor.value.todo + "')\">Update</button></td><td><button onclick=\"removetodo('" + cursor.key + "','" + cursor.value.todo + "')\">Delete</button></td></tr>");
    cursor.continue();
  }
 };
};

现在我在点击时调用函数 我只是调用 readAll 函数

$(".clickme").click(function(){
  readAll();
}); 

我在 div 中创建了一个 id View 的 div,我现在使用了一个表头,我只是附加 JavaScript 中的代码并填充到 html 中

<div id="view">
 <thead>
   <tr class="ui-bar-d">
     <th data-priority="2">Timestamp</th>
     <th data-priority="1">To Do</th>
     <th data-priority="3">Update</th>
     <th data-priority="4">Delete</th>
   </tr>
 </thead>
 <tbody id="todos">
 </tbody>
</table>
</div>
<label for="todo">To Do:</label>
<input id="todo" type="text">
<input id="toprice" type="text">
<button class="clickme">clickme</button>
<button class="ui-shadow ui-btn ui-corner-all" onclick="add()">Add</button>

最佳答案

由于 IDB 是异步的。您需要调用您的函数......

 transaction.oncomplete = function(event) {
      $(".clickme").click(function(){
      readAll();
   });     
  };

https://developer.mozilla.org/en-US/docs/Web/API/IDBTransaction/oncomplete

关于javascript - 如何从 indexeddb 中检索数据并显示在 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41045952/

相关文章:

jquery - 如何在 jQuery 中获取之前的评论元素?

javascript - 当 div 底部到达视口(viewport)底部时删除类

html - CSS 网格布局被隐藏的 child 破坏了

javascript - 柱形图上的 Highcharts 工具提示

javascript - 如何禁用/隐藏基于 IList 值的局部 View ?

javascript - 如何使函数 (f1) 运行单独的函数 (f2),但 f2 使用 "this"参数?

jquery - 我收到 JSON 的 XML 错误消息?

html - 当 "viewing source"时,一些网站有整洁的标记,一些网站没有。为什么? (附图)

javascript - 重新调查 "Trying to make different HTML paragraph show depending on selection"

javascript - 区分 +0 和 -0