效果:
代码:
StringUtil.js
//去除字符串中间空格 String.prototype.Trim = function() { return this.replace(/(^s*)|(s*$)/g, ""); } //去除字符串左侧空格 String.prototype.LTrim = function() { return this.replace(/(^s*)/g, ""); } //去除字符串右侧空格 String.prototype.RTrim = function() { return this.replace(/(s*$)/g, ""); } //去除字符串中所有空格(包括中间空格,需要设置第2个参数为:g) function Trim(str,is_global){ var result; result = str.replace(/(^s+)|(s+$)/g,""); if(is_global.toLowerCase()=="g") result = result.replace(/s/g,""); return result; }
indexedDB.js
window.onload = function(){ if(!window.indexedDB){ window.indexedDB = window.mozIndexedDB || window.webkitIndexedDB; } var db = null; var request = indexedDB.open("mydb"); request.onupgradeneeded = function(e){ //db = request.result; db = e.target.result; createObjectStore(db); } function createObjectStore(db){ if(db.objectStoreNames.contains("customer")){ db.deleteObjectStore("customer"); } var objectStore = db.createObjectStore("customer",{keyPath:"id",autoIncrement:true}); objectStore.createIndex("name","name",{unique:false}); objectStore.createIndex("email","email",{unique:true}); objectStore.add({name: "Tom", sex: "male", age: 34, email: "tom@facebok.org"}); objectStore.add({name: "Jiny", sex: "female", age: 25, email: "jiny@home.org"}); objectStore.add({name: "Liam", sex: "male", age: 23, email: "liam@163.com"}); } request.onsuccess = function(e){ db = e.target.result; if(!db.version=="1.0"){ var request = db.setVersion("1.0"); request.onsuccess = function(e){ createObjectStore(db); showDataByCursor(); } request.onerror = function(e){ alert(e); } }else{ showDataByCursor(); } } function showDataByCursor(objectStore){ if(!objectStore){ var transaction = db.transaction(["customer"]); objectStore = transaction.objectStore("customer"); } console.log("Store-Name : "+objectStore.name); console.log("Store-KeyPath : "+objectStore.keyPath); var request = objectStore.openCursor(); request.onsuccess = function(e){ var cursor = e.target.result; if(cursor){ console.log(cursor.key); console.log(cursor.value); var data = cursor.value; data.id = cursor.key; showInTable(data); cursor.continue(); } } request.onerror = function(e){ console.log("ERROR"); } } var table = document.getElementsByTagName("table")[0]; function showInTable(data){ //table.childNodes var tbody = table.children[1]; //tbody = document.getElementsByTagName("tbody")[0]; var tr = document.createElement("tr"); var td_id = createTd(data,"id") var checkBox=document.createElement("input"); checkBox.setAttribute("type","checkbox"); checkBox.setAttribute("name","check"); var textNode = td_id.childNodes[0]; checkBox.setAttribute("id",textNode.nodeValue); //td_id.removeChild(textNode); td_id.appendChild(checkBox); //td_id.appendChild(textNode); tr.appendChild(td_id); var td_name = createTd(data,"name"); tr.appendChild(td_name); var td_sex = createTd(data,"sex"); tr.appendChild(td_sex); var td_age = createTd(data,"age"); tr.appendChild(td_age); var td_email = createTd(data,"email"); tr.appendChild(td_email); tbody.appendChild(tr); } function createTd(data,key){ var td = document.createElement("td"); td.contentEditable = "true"; var namedNodeMap = td.attributes; var attrKey = document.createAttribute("key"); attrKey.value = key; namedNodeMap.setNamedItem(attrKey); var attrValue = document.createAttribute("value"); attrValue.value = data[key]; namedNodeMap.setNamedItem(attrValue); var attrType = document.createAttribute("type"); attrType.value = typeof(data[key]); namedNodeMap.setNamedItem(attrType); td.innerText = data[key]; td.onblur = function(event){ console.log(this.parentNode.firstElementChild.innerText); console.log(this.innerText.LTrim().RTrim()); console.log(this.attributes["value"].value); console.log(this.attributes["key"].value); console.log(this.innerText); if(this.attributes["value"].value != this.innerText.LTrim().RTrim()){ saveOrUpdate(event.target); //或saveOrUpdate(this) } } return td; } function saveOrUpdate(obj){ var id = obj.parentNode.firstElementChild.innerText; var transaction = db.transaction(["customer"],"readwrite"); var objectStore = transaction.objectStore("customer"); var request = objectStore.get(parseInt(id)); //var range = IDBKeyRange.only("Donna"); //var request = objectStore.index("name").openCursor(range); request.onsuccess = function(event){ if(event.target.result){ //objectStore.delete(id); var data = event.target.result; console.log("Update : " + data); var value = obj.attributes["type"].value=="number"?parseInt(obj.innerText):obj.innerText; data[obj.attributes["key"].value] = value; var updateRequest = objectStore.put(data); updateRequest.onsuccess = function(event){ //console.log(event.target); console.log("UPDATE SUCCESS"); } updateRequest.onerror = function(event){ console.log("UPDATE ERROR"); } }else{ var data = {}; var tds = obj.parentNode.children; for(var i=0;i<tds.length;i++){ var td = tds[i]; var key = td.attributes["key"].value; data[key] = td.attributes["type"].value=="number"?parseInt(td.innerText):td.innerText; } console.log("Add : " + data); var saveRequest = objectStore.add(data); saveRequest.onsuccess = function(){ console.log("SAVE SUCCESS"); } saveRequest.onerror = function(){ console.log("SAVE ERROR"); } } } request.onerror = function(event){ alert(event); } } /*搜索*/ var name = document.getElementsByName("name")[0]; var email = document.getElementsByName("email")[0]; var select = document.getElementsByTagName("button")[0]; select.onclick = function(event){ var value_name = name.value.LTrim().RTrim(); var value_email = email.value.LTrim().RTrim(); var transaction = db.transaction(["customer"],"readonly"); var objectStore = transaction.objectStore("customer"); var tbody = table.children[1]; var elements = tbody.children; var count = elements.length; for(var i=0;i<elements.length;){ //动态移除,没有i++,始终移除第一个 tbody.removeChild(elements[i]); } if(value_name=="" && value_email==""){ var request = objectStore.openCursor(); request.onsuccess = function(event){ var cursor = event.target.result; if(cursor){ showInTable(cursor.value) cursor.continue(); } console.log("GETALL SUCCESS"); }; request.onerror = function(event){ console.log(event.target); console.log("GETALL ERROR"); }; }else{ if(value_name!=""){ var range = IDBKeyRange.only(value_name); var request = objectStore.index("name").openCursor(range); request.onsuccess = function(event){ var cursor = event.target.result; if(cursor){ console.log("key : "+cursor.key); console.log("value : "+cursor.value); showInTable(cursor.value) cursor.continue(); } console.log("SELECT BY NAME SUCCESS"); } request.onerror = function(event){ console.log("SELECT BY NAME ERROR"); } } if(value_email!=""){ var range = IDBKeyRange.only(value_email); var request = objectStore.index("email").openCursor(range); request.onsuccess = function(event){ var cursor = event.target.result; if(cursor){ console.log("key : "+cursor.key); console.log("value : "+cursor.value); showInTable(cursor.value) cursor.continue(); } console.log("SELECT BY EMIAL SUCCESS"); } request.onerror = function(event){ console.log("SELECT BY EMAIL ERROR"); } } } } /*删除和清空*/ var checkAll = document.getElementsByName("checkAll")[0]; var btnDelete = document.getElementsByName("delete")[0]; var btnClear = document.getElementsByName("clear")[0]; checkAll.onclick = function(event){ var checkBoxs = document.getElementsByName("check"); console.log(checkBoxs); if(this.checked==true){ for(var i=0;i<checkBoxs.length;i++){ checkBoxs[i].checked = true; } }else{ for(var i=0;i<checkBoxs.length;i++){ checkBoxs[i].checked = false; } } } btnDelete.addEventListener("click",function(event){ var checkBoxs = document.getElementsByName("check"); console.log(checkBoxs); var transaction = null; var objectStore = null; var request = null; for(var i=0;i<checkBoxs.length;i++){ var checkBox = checkBoxs[i]; if(checkBox.checked == true){ if(request == null){ transaction = db.transaction(["customer"],"readwrite"); objectStore = transaction.objectStore("customer"); } var id = parseInt(checkBox.attributes["id"].value); console.log("checked : "+id); request = objectStore.get(id); request.onsuccess = function(event){ if(event.target.result){ var record = event.target.result; objectStore.delete(record.id); for(var j=0;j<checkBoxs.length;j++){ if(checkBoxs[j].attributes["id"].value-record.id==0){ var tr = checkBoxs[j].parentElement.parentElement; var tbody = checkBoxs[j].parentElement.parentElement.parentElement; tbody.removeChild(tr); break; } } console.log("DELETE SUCCESS"); } }; request.onerror = function(event){ console.log("DELETE ERROR"); }; } } }); btnClear.addEventListener("click",function(event){ var objectStore = db.transaction(["customer"],"readwrite").objectStore("customer"); var request = objectStore.clear(); request.onsuccess = function(event){ console.log("CLEAR SUCCESS"); objectStore.openCursor().onsuccess = function(event){ var tbody = table.children[1]; var elements = tbody.children; for(var i=0;i<elements.length;){ //动态移除,没有i++,始终移除第一个 tbody.removeChild(elements[i]); } var cursor = event.target.result; if(cursor){ var data = cursor.value; data.id = cursor.key; showInTable(data); cursor.continue(); } console.log("SHOW SUCCESS"); } } request.onerror = function(event){ console.log("CLEAR ERROR"); } }); };
indexedDB.html
<!doctype html> <html> <head> <meta charset="utf-8"/> <title>IndexedDB</title> <script type="text/javascript" src="StringUtil.js"></script> <!--<script type="text/javascript" src="jquery-1.8.3.js"></script>--> <script type="text/javascript" src="indexedDB.js"></script> </head> <body> <div> <label>姓名:<input type="text" name="name" value="" style="width:50px;height:15px;"/></label> <label>邮箱:<input type="text" name="email" value="" style="width:80px;height:15px;"/></label> <button type="button">查询</button> </div> <table> <thead> <tr> <th>ID</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>邮箱</th> </tr> </thead> <tbody> </tbody> <tfoot> <tr> <td colspan="5"> <label style="font-size:14px"><input type="checkbox" name="checkAll"/>全选</label> <input type="button" name="delete" value="删除选中"/> <input type="button" name="clear" value="清空数据库"/> </td> </tr> </tfoot> </table> </body> </html>
相关推荐
本篇文章主要介绍了HTML5中indexedDB 数据库的使用实例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
但是如果在浏览器上需要用到数据库一般会使用Indexed DB数据库,webSQL基本上已经废弃了,具体原因小伙伴可以下来自己查查。 IndexedDB 是一种底层 API,用于在客户端存储大量的结构化数据(也包括文件/二进制大型...
nanoidb基于IndexedDB数据库的一个有趣封装
一个本地数据库(Web SQL IndexedDB)实现的通信录;介绍怎样创建打开数据库,创建表,添加数据,更新数据,删除数据,删除表 等等。
ngx-indexed-db ngx-indexed-db是一项服务,结合了ngx-indexed-db ,将IndexedDB数据库包装在Angular服务中。安装$ npm install ngx-indexed-db或者$ yarn add ngx-indexed-db用法导入NgxIndexedDBModule并启动它: ...
y-indexeddb Yjs的IndexedDB数据库提供程序。 使用IndexedDB数据库适配器将共享数据持久存储在浏览器中。 下次您加入会话时,所做的更改仍然存在。 最小化服务器和客户端之间交换的数据量使离线编辑成为可能入门您...
html5 indexeddb 存储
IndexedDB是一种能在浏览器中持久的存储结构化数据的对象数据库,并且为web应用提供了丰富的查询能力。 相比于Web SQL数据库它更加简单,而且官方标准中关于Web SQL的工作已经停止。 相比于Web Storage,IndexedDB...
浏览器端 indexed db 封装成更简洁易操作的接口, 数据库变化监控!
logs2indexeddb 该实用程序可将所有JavaScript控制台日志连续保存到IndexedDB数据库中。 关闭浏览器选项卡后,您可以访问console。*日志。 对于需要在一段时间后分析Web客户端日志的开发人员而言,此实用程序是一个...
介绍IndexedDB是HTML5 WEB数据库,允许HTML5 WEB应用在用户浏览器端存储数据。对于应用来说IndexedDB非常强大、有用,可以在客户端的chrome,IE,Firefox等WEB浏览器中存储大量数据,下面简单介绍一下IndexedDB的基本...
前段时间项目需要本地存储聊天数据,使用到indexedDB,经查阅大量文章,终于完成。感觉网上indexedDB的文章不够多,也不够完善,因此把代码分享出来,帮助需要的小伙伴。
IndexedDB 基本使用
html5开发
NULL 博文链接:https://liuna718-163-com.iteye.com/blog/2178433
indexeddb-promiseIndexedDB的所有操作都是异步的,API都是在回调函数中执行的,为了便于对其进行操作,使用ES6中的Promise来封装。1. 安装npm包// use npmnpm install --save-dev indexeddb-promise// use yarnyarn...
所以研究了一下HTML5的IndexedDB。 对于只存储某些字段的需求来说,可以使用Local Storage和 Session Storage来完成。但是一旦存储大量的数据,Local Storage和 Session Storage就远远不能满足需求了。这时,Indexed...
indexedDB.html
异步存储localStorage API 的异步版本,由一个 IndexedDB 数据库支持。 该文件定义了一个由 IndexedDB 数据库支持的 localStorage API 的异步版本。 它创建一个全局 asyncStorage 对象,该对象具有类似于 ...
NULL 博文链接:https://weistar.iteye.com/blog/2186673