提出问题:如何利用原生的js实现基本的增删改查功能???
解决问题
假如你已经对JS有一定基础
假如你对bootstrap有一定基础
下面是具体的例子,
包含两个文件(index.jsp 和 index.js)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <!-- Bootstrap --> <link href="https://www.jb51.net/resource/bootstrap/css/bootstrap.min.css" media="screen"> <link href="https://www.jb51.net/resource/bootstrap/css/bootstrap-theme.min.css" media="screen"> <title>JS框架学习</title> </head> <body> <div> <table> <caption><h2>练习一</h2></caption> <caption> <button type="button" data-toggle="modal" data-target="#myModal">新增</button> <button type="button">删除</button> <button type="button" data-toggle="modal" data-target="#myModal">编辑</button> <button type="button">查询</button> <input type="text" placeholder="按工号查询"> <input type="text" placeholder="按姓名查询"> <input type="text" placeholder="全文搜索"> </caption> <thead> <tr> <th>序号</th> <th>工号</th> <th>姓名</th> <th>性别</th> <th>密码</th> <th>年龄</th> <th>出生日期</th> </tr> </thead> <tbody> </tbody> </table> <!-- 模态框(Modal) --> <div role="dialog" > <div> <div> <div> <button type="button" data-dismiss="modal" aria-hidden="true"> × </button> <h4> 新增用户 </h4> </div> <div> <label for="name">工号:</label> <input type="text" placeholder="请输入工号"> <label for="name">姓名:</label> <input type="text" placeholder="请输入姓名"> <label for="name">性别:</label> <input type="text" placeholder="请输入性别"> <label for="name">密码:</label> <input type="text" placeholder="请输入密码"> <label for="name">年龄:</label> <input type="text" placeholder="请输入年龄"> <label for="name">出生日期:</label> <input type="text" placeholder="请输入出生日期"> </div> <div> <button type="button" data-dismiss="modal">保存 </button> <button type="button">提交更改</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal --> </div> <!-- 这里需要引入相关的js,很重要,请记住 --> <script type="text/javascript" src="https://www.jb51.net/resource/jquery/jquery.js"></script> <script type="text/javascript" src="https://www.jb51.net/resource/bootstrap/js/bootstrap.min.js"></script> <script type="text/javascript" src="https://www.jb51.net/index.js"></script> </body> </html> 复制代码 复制代码 //存放所有用户 var users = users || {}; //操作类型 var operateType = ""; //存放搜索对象 var searchUsers = searchUsers || {}; //用户构造方法 var User = { Create:function(code,userName,sex,passWord,age,birthday){ this.code = code; this.userName = userName; this.sex = sex; this.passWord = passWord; this.age = age; this.birthday = birthday; }, //添加用户 addUserData:function(){ if(this.code != ""){ users[this.code] = this; } }, //删除用户 deleteUserData:function (){ for(var i in users){ if(this.code == users[i].code){ delete users[i]; } } }, //编辑用户 editUserData:function(){ for(var i in users){ if(this.code == users[i].code){ users[i].userName = this.userName; users[i].sex = this.sex; users[i].passWord = this.passWord; users[i].birthday = this.birthday; users[i].age = this.age; } } }, //查找用户 findUserData:function(data){ for(var i in users){ if(data.code.indexOf(users[i].code) >= 0 || data.userName.indexOf(users[i].userName) >= 0){ searchUsers[users[i].code] = users[i]; refreshDatas(searchUsers); } } } }; function New(aClass,aParams){ function new_(){ aClass.Create.apply(this,aParams); } new_.prototype = aClass; return new new_(); } //bootstrap模态框事件 $('#myModal').on('hide.bs.modal', function () { // 执行一些动作... var inputElements = this.getElementsByTagName("input"); var userArr = []; for(var i=0;i<inputElements.length;i++){ userArr[i] = inputElements[i].value; } var user = New(User,userArr); //添加操作 if(operateType == "add"){ user.addUserData(); refreshDatas(users); //编辑操作 }else if(operateType == "edit"){ user.editUserData(); refreshDatas(users); } }); /** * 首次加载页面执行方法 */ function loadUserDatas(){ var userArray = initUserDatas(); addRowData(userArray); refreshDatas(users); } /** * 初始化用户数据 */ function initUserDatas(){ var initUser1 = New(User,["1001","小兰","女","1234","13","1991-1-1"]); var initUser2 = New(User,["1002","小毅","男","1234","13","1991-1-1"]); var initUser3 = New(User,["1003","兰花","女","1234","13","1991-1-1"]); var initUser4 = New(User,["1004","兰儿","女","1234","13","1991-1-1"]); users[initUser1.code] = initUser1; users[initUser2.code] = initUser2; users[initUser3.code] = initUser3; users[initUser4.code] = initUser4; return users; } /** * 往表格添加一行html数据 */ function addRowData(datas){ var tbodyElement = document.getElementById("tbody"); var html = ""; var color = "warning"; var flag = true; for(var i in datas){ if(flag){ color = "info"; }else{ color = "warning"; } html = html + "<tr><td><input type='checkbox'></td><td>" + datas[i].code +"</td><td>" + datas[i].userName +"</td><td>" + datas[i].sex +"</td><td>" + datas[i].passWord +"</td><td>" + datas[i].age +"</td><td>" + datas[i].birthday +"</td>" +"</tr>"; flag = !flag;//颜色转换 } tbodyElement.innerHTML = html; } /** * 刷新用户数据 */ function refreshDatas(datas){ addRowData(datas); }; /** * 收集一行数据 */ function collectionRowData(param){ var tdElement = param.getElementsByTagName("td"); var userArr = []; for(var i=1;i<tdElement.length;i++){ var temp = tdElement[i].textContent; userArr[i-1] = temp; } var user = New(User,userArr); return user; } /** * 用户操作方法 */ function optionUserData(param){ //获得操作类别 var optionType = param.getAttribute("id"); if(optionType == "user_add"){ operateType = "add"; }else if(optionType == "user_delete"){ var checkRowData = isCheckedData(); var user = collectionRowData(checkRowData); user.deleteUserData(); refreshDatas(users); }else if(optionType == "user_edit"){ operateType = "edit"; var checkRowData = isCheckedData(); var user = collectionRowData(checkRowData); var modal_body = document.getElementById("modal-body"); var inputElements= modal_body.getElementsByTagName("input"); for(var i=0;i<inputElements.length;i++){ var temp = inputElements[i].id.substring(2,inputElements[i].id.length) inputElements[i].value = user[temp]; } }else if(optionType == "user_find"){ var s_code = document.getElementById("s_code").value; var s_userName = document.getElementById("s_userName").value; var s_all= document.getElementById("s_all").value; //搜索数据 var s_data = s_data || {}; s_data.code = s_code; s_data.userName = s_userName; s_data.all = s_all; var user = New(User,[]); user.findUserData(s_data); }else{ } } /** * 是否选中数据,返回选中数据的行 */ function isCheckedData(){ var tbodyElement =document.getElementById("tbody"); var trElements = tbodyElement.getElementsByTagName("tr"); var flag = false; for(var i=0;i<trElements.length;i++){ var inputElement = trElements[i].getElementsByTagName("input")[0]; if(inputElement.checked){ flag = true; return trElements[i]; } } if(!flag){ alert("请选择一条记录!"); $('#myModal').unbind("on"); } }
效果图: