clouddb_wilddog.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  6. <title></title>
  7. <script src="../js/mui.min.js"></script>
  8. <script src="https://cdn.wilddog.com/js/client/current/wilddog.js"></script>
  9. <link href="../css/mui.min.css" rel="stylesheet" />
  10. <script type="text/javascript" charset="utf-8">
  11. mui.init();
  12. </script>
  13. <style type="text/css">
  14. .title{
  15. background-color: rgba(0,0,0,.25);
  16. font-weight: 400;
  17. }
  18. #list td{
  19. padding:5px 5px;
  20. word-break: break-word;
  21. }
  22. #list tr:active{
  23. background-color: rgba(0,0,0,.25);
  24. }
  25. #popover{
  26. min-width: 282px;
  27. height: 300px ;
  28. top:50% !important;
  29. margin-top: -150px;
  30. left: 50% !important;
  31. margin-left: -141px;
  32. }
  33. @media (min-width: 400px){
  34. #popover{
  35. width: 80%;
  36. margin-left: -40%;
  37. }
  38. }
  39. .mui-popover .mui-popover-arrow{
  40. display: none !important;
  41. }
  42. .operate{
  43. text-align: center;
  44. margin-top: 20px
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <header class="mui-bar mui-bar-nav">
  50. <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
  51. <a class="mui-icon mui-icon-plusempty mui-pull-right" id="open"></a>
  52. <h1 class="mui-title">cloud DB(云端数据库)</h1>
  53. </header>
  54. <div class="mui-content">
  55. <div class="mui-content-padded">
  56. <p style="text-indent: 22px;">这是mui无后端开发示例,集成野狗云服务,通过js操作云端数据库,实现常见的增删改查功能。
  57. </p>
  58. </div>
  59. <div style="background-color: #fff;padding-bottom: 15px;">
  60. <div class="mui-content-padded" >
  61. <h4 style="text-align: center;margin-bottom: 15px;padding-top: 15px;">云端数据库</h4>
  62. <table id="list" border="1" width="100%" style="padding: 0;text-align: center;table-layout: fixed;" >
  63. </table>
  64. </div>
  65. </div>
  66. </div>
  67. <div id="popover" class="mui-popover">
  68. <div class="mui-popover-arrow"></div>
  69. <div class="mui-content-padded">
  70. <h4 id="title" style="text-align: center;margin-top: 20px;">新增数据</h4>
  71. <form class="mui-input-group" style="margin-top: 30px;">
  72. <div class="mui-input-row">
  73. <label>字段1:</label>
  74. <input type="text" class="mui-input-clear" id="col1" placeholder="请输入">
  75. </div>
  76. <div class="mui-input-row">
  77. <label>字段2:</label>
  78. <input type="text" class="mui-input-clear" id="col2" placeholder="请输入">
  79. </div>
  80. <div class="mui-input-row">
  81. <label>字段3:</label>
  82. <input type="text" class="mui-input-clear" id="col3" placeholder="请输入">
  83. </div>
  84. </form>
  85. <div id="add_div" class="operate mui-hidden">
  86. <button type="button" class="mui-btn mui-btn-blue" id="add">确定</button>
  87. </div>
  88. <div id="edit_div" class="operate mui-hidden">
  89. <button type="button" class="mui-btn mui-btn-blue" id="update" style="margin-right: 30px;">修改</button>
  90. <button type="button" class="mui-btn mui-btn-red" id="del">删除</button>
  91. </div>
  92. </div>
  93. </div>
  94. <script type="text/javascript">
  95. function trim(str){ //删除左右两端的空格
  96.    return str.replace(/(^\s*)|(\s*$)/g, "");
  97.    }
  98. var network = false;
  99. var ref = null;
  100. mui.plusReady(function () {
  101. if(plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE){
  102. mui.toast("当前网络不给力,无法连接云端数据库~");
  103. }else{
  104. network = true;
  105. ref = new Wilddog("https://muidemo.wilddogio.com/demo");
  106. getList();
  107. }
  108. });
  109. var list = document.getElementById("list");
  110. var col1 = document.getElementById("col1");
  111. var col2 = document.getElementById("col2");
  112. var col3 = document.getElementById("col3");
  113. var add_div = document.getElementById("add_div");
  114. var edit_div = document.getElementById("edit_div");
  115. var title = document.getElementById("title");
  116. document.getElementById("open").addEventListener('tap',function () {
  117. if(!network){
  118. mui.toast("当前网络不给力,请稍后再试");
  119. }else{
  120. //清空数据
  121. col1.value = "";
  122. col2.value = "";
  123. col3.value = "";
  124. title.innerHTML = "新增数据";
  125. add_div.classList.remove("mui-hidden");
  126. if(!edit_div.classList.contains("mui-hidden")){
  127. edit_div.classList.add("mui-hidden");
  128. }
  129. mui("#popover").popover("toggle");
  130. }
  131. });
  132. document.getElementById("add").addEventListener('tap', function() {
  133. if(!ref){
  134. mui.toast("当前网络不给力,请稍后再试");
  135. }else{
  136. var check = true;
  137. mui(".mui-input-group input").each(function () {
  138. if(!this.value||trim(this.value)==""){
  139. var label = this.previousElementSibling;
  140. mui.alert(label.innerText+"不允许为空");
  141. check = false;
  142. return false;
  143. }
  144. });
  145. if(check){
  146. var newRef = ref.push({
  147. "col1": col1.value,
  148. "col2": col2.value,
  149. "col3": col3.value,
  150. });
  151. add_div.classList.add("mui-hidden");
  152. //关闭modal
  153. mui("#popover").popover("toggle");
  154. mui.toast("添加成功~");
  155. //刷新界面
  156. getList();
  157. }
  158. }
  159. });
  160. document.getElementById("update").addEventListener("tap", function() {
  161. var check = true;
  162. mui(".mui-input-group input").each(function () {
  163. if(!this.value||trim(this.value)==""){
  164. var label = this.previousElementSibling;
  165. mui.alert(label.innerText+"不允许为空");
  166. check = false;
  167. return false;
  168. }
  169. });
  170. if(check){
  171. var newRef = ref.child(document.getElementById("col1").getAttribute("demoid"));
  172. newRef.update({
  173. "col1": col1.value,
  174. "col2": col2.value,
  175. "col3": col3.value,
  176. });
  177. //关闭modal
  178. mui("#popover").popover("toggle");
  179. edit_div.classList.add("mui-hidden");
  180. mui.toast("修改成功~");
  181. //刷新界面
  182. getList();
  183. }
  184. })
  185. document.getElementById("del").addEventListener("tap", function() {
  186. var newRef = ref.child(document.getElementById("col1").getAttribute("demoid"));
  187. newRef.remove();
  188. //关闭modal
  189. mui("#popover").popover("toggle");
  190. if(!edit_div.classList.contains("mui-hidden")){
  191. edit_div.classList.add("mui-hidden");
  192. }
  193. mui.toast("删除成功~");
  194. //刷新界面
  195. getList();
  196. })
  197. //列表事件绑定,点击编辑数据
  198. mui("#list").on("tap", "tr", function() {
  199. var id = this.getAttribute("id");
  200. if(id){
  201. document.getElementById("col1").value = this.querySelector(".col1").innerHTML;
  202. document.getElementById("col2").value = this.querySelector(".col2").innerHTML;
  203. document.getElementById("col3").value = this.querySelector(".col3").innerHTML;
  204. document.getElementById("col1").setAttribute("demoid", id);
  205. title.innerHTML = "编辑数据";
  206. if(!add_div.classList.contains("mui-hidden")){
  207. add_div.classList.add("mui-hidden");
  208. }
  209. edit_div.classList.remove("mui-hidden");
  210. mui("#popover").popover("toggle");
  211. }
  212. })
  213. /**
  214. * 获取最新记录
  215. */
  216. function getList() {
  217. ref.once("value", function(snapshot) {
  218. list.innerHTML = "<tr class='title'><td>字段1</td><td>字段2</td><td>字段3</td></tr>";
  219. var result = snapshot.val();
  220. mui.each(result, function(key, elem) {
  221. var li = document.createElement("tr");
  222. li.setAttribute("id", key);
  223. var col1 = document.createElement("td");
  224. col1.className = "col1"
  225. col1.innerHTML = elem.col1;
  226. li.appendChild(col1);
  227. var col2 = document.createElement("td");
  228. col2.className = "col2"
  229. col2.innerHTML = elem.col2;
  230. li.appendChild(col2);
  231. var col3 = document.createElement("td");
  232. col3.className = "col3"
  233. col3.innerHTML = elem.col3;
  234. li.appendChild(col3);
  235. list.appendChild(li);
  236. })
  237. });
  238. }
  239. </script>
  240. </body>
  241. </html>