這篇文章給大家分享的是有關(guān)Bootstrap中table如何實現(xiàn)樹形表格聯(lián)動選中聯(lián)動取消功能的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
高港網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應式網(wǎng)站設(shè)計等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)公司2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
公司最近有需求要做樹形式table。因為是前后端不分離項目,且之前已經(jīng)引入了bootstrap table插件,現(xiàn)把實現(xiàn)方式分享一下:
<!DOCTYPE HTML> <html lang="zh-cn"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta content="width=device-width,initial-scale=1.0" name="viewport"> <meta content="yes" name="apple-mobile-web-app-capable"> <meta content="black" name="apple-mobile-web-app-status-bar-style"> <meta content="telephone=no" name="format-detection"> <meta content="email=no" name="format-detection"> <title>系統(tǒng)管理</title> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet"> <link href="https://cdn.bootcss.com/bootstrap-table/1.11.1/bootstrap-table.min.css" rel="external nofollow" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-treegrid/0.2.0/css/jquery.treegrid.min.css" rel="external nofollow" > </head> <body> <div class="container"> <h2>樹形表格 : Table Treegrid</h2> <table id="table"></table> <br/> <button onclick="test()">選擇</button> </div> </body> <script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script> <script src="https://cdn.bootcss.com/bootstrap-table/1.12.1/bootstrap-table.min.js"></script> <script src="https://cdn.bootcss.com/bootstrap-table/1.12.0/extensions/treegrid/bootstrap-table-treegrid.js"></script> <script src="https://cdn.bootcss.com/jquery-treegrid/0.2.0/js/jquery.treegrid.min.js"></script> <script type="text/javascript"> var $table = $('#table'); var data = [{0: undefined, id: 94710, pid: 0, mode: "系統(tǒng)管理", permissionTag: "*", permissionMark: "所有權(quán)限", } ,{0: undefined, id: 94711, pid: 94710, mode: "營銷中心管理", permissionTag: "*", permissionMark: "所有權(quán)限",} ,{0: undefined, id: 471104, pid: 94711, mode: "營銷中心管理", permissionTag: "search", permissionMark: "查詢",} ,{0: undefined, id: 94721, pid: 94710, mode: "工廠管理", permissionTag: "*", permissionMark: "所有權(quán)限", } ,{0: undefined, id: 472104, pid: 94721, mode: "工廠管理", permissionTag: "search", permissionMark: "查詢", } ,{0: undefined, id: 94731, pid: 94710, mode: "經(jīng)銷商管理", permissionTag: "*", permissionMark: "所有權(quán)限", } ,{0: undefined, id: 473104, pid: 94731, mode: "經(jīng)銷商管理", permissionTag: "search", permissionMark: "查詢", } ,{0: undefined, id: 94732, pid: 94710, mode: "產(chǎn)品管理", permissionTag: "*", permissionMark: "所有權(quán)限", } ,{0: undefined, id: 473201, pid: 94732, mode: "產(chǎn)品管理", permissionTag: "insert", permissionMark: "添加", } ,{0: undefined, id: 473202, pid: 94732, mode: "產(chǎn)品管理", permissionTag: "update", permissionMark: "修改", } , {0: undefined, id: 473203, pid: 94732, mode: "產(chǎn)品管理", permissionTag: "delete", permissionMark: "刪除", } , {0: undefined, id: 473204, pid: 94732, mode: "產(chǎn)品管理", permissionTag: "search", permissionMark: "查詢", } , {0: undefined, id: 94741, pid: 94710, mode: "用戶賬號", permissionTag: "*", permissionMark: "所有權(quán)限", } , {0: undefined, id: 474101, pid: 94741, mode: "用戶賬號", permissionTag: "insert", permissionMark: "添加", } , {0: undefined, id: 474102, pid: 94741, mode: "用戶賬號", permissionTag: "update", permissionMark: "修改", } , {0: undefined, id: 474103, pid: 94741, mode: "用戶賬號", permissionTag: "delete", permissionMark: "刪除", } , {0: undefined, id: 474104, pid: 94741, mode: "用戶賬號", permissionTag: "search", permissionMark: "查詢", } , {0: undefined, id: 474105, pid: 94741, mode: "用戶賬號", permissionTag: "resetPwd", permissionMark: "重置密碼", } , {0: undefined, id: 94742, pid: 94710, mode: "角色權(quán)限管理", permissionTag: "*", permissionMark: "所有權(quán)限", } , {0: undefined, id: 474201, pid: 94742, mode: "角色權(quán)限管理", permissionTag: "insert", permissionMark: "添加", } , {0: undefined, id: 474202, pid: 94742, mode: "角色權(quán)限管理", permissionTag: "update", permissionMark: "修改", } , {0: undefined, id: 474203, pid: 94742, mode: "角色權(quán)限管理", permissionTag: "delete", permissionMark: "刪除", } , {0: undefined, id: 474204, pid: 94742, mode: "角色權(quán)限管理", permissionTag: "search", permissionMark: "查詢", } , {0: undefined, id: 94760, pid: 0, mode: "運營中心", permissionTag: "*", permissionMark: "所有權(quán)限", } , {0: undefined, id: 94770, pid: 94760, mode: "跨區(qū)域調(diào)撥單", permissionTag: "*", permissionMark: "所有權(quán)限", } , {0: undefined, id: 476104, pid: 94770, mode: "跨區(qū)域調(diào)撥單", permissionTag: "search", permissionMark: "查詢", } , {0: undefined, id: 476105, pid: 94770, mode: "跨區(qū)域調(diào)撥單", permissionTag: "download", permissionMark: "下載", }] $(function() { //控制臺輸出一下數(shù)據(jù) console.log(data); $table.bootstrapTable({ data:data, idField: 'id', dataType:'jsonp', columns: [ { field: 'check', checkbox: true, formatter: function (value, row, index) { if (row.check == true) { // console.log(row.serverName); //設(shè)置選中 return { checked: true }; } } }, { field: 'mode', title: '名稱' }, { field: 'permissionTag', title: '狀態(tài)', }, { field: 'permissionMark', title: '權(quán)限值' }, ], // bootstrap-table-treegrid.js 插件配置 -- start //在哪一列展開樹形 treeShowField: 'mode', //指定父id列 parentIdField: 'pid', onResetView: function(data) { //console.log('load'); $table.treegrid({ // initialState: 'collapsed',// 所有節(jié)點都折疊 initialState: 'expanded',// 所有節(jié)點都展開,默認展開 treeColumn: 1, // expanderExpandedClass: 'glyphicon glyphicon-minus', //圖標樣式 // expanderCollapsedClass: 'glyphicon glyphicon-plus', onChange: function() { $table.bootstrapTable('resetWidth'); } }); //只展開樹形的第一級節(jié)點 $table.treegrid('getRootNodes').treegrid('expand'); }, onCheck:function(row){ event.preventDefault() var datas = $table.bootstrapTable('getData'); console.log(datas) row.check = true // 勾選子類 selectChilds(datas,row,"id","pid",true); // 勾選父類 selectParentChecked(datas,row,"id","pid") // 刷新數(shù)據(jù) $table.bootstrapTable('load', datas); }, onUncheck:function(row){ event.preventDefault() row.check = false var datas = $table.bootstrapTable('getData'); selectChilds(datas,row,"id","pid",false); //取消選中最后一個子元素時時查找對應父元素取消 selectParentUnchecked(datas,row,"id","pid") $table.bootstrapTable('load', datas); }, // bootstrap-table-treetreegrid.js 插件配置 -- end }); }); </script> <script> /** * 選中父項時,同時選中子項 * @param datas 所有的數(shù)據(jù) * @param row 當前數(shù)據(jù) * @param id id 字段名 * @param pid 父id字段名 */ function selectChilds(datas,row,id,pid,checked) { console.log("zi",datas,'row',row,'check',checked) for(var i in datas){ if(datas[i][pid] == row[id]){ datas[i].check=checked; selectChilds(datas,datas[i],id,pid,checked); }; } } function selectParentUnchecked(datas,row,id,pid){ console.log("fu--Uncheck",datas,'row',row,) let key = 0; for(var i in datas){ if(datas[i].pid == row.pid && datas[i].check == true){ key++ } } console.log(key) if(key == 0 ){ for(var i in datas){ if(datas[i][id] == row[pid]){ datas[i].check=false; selectParentUnchecked(datas,datas[i],id,pid); } } } } function selectParentChecked(datas,row,id,pid){ for(var i in datas){ if(datas[i][id] == row[pid]){ datas[i].check=true; selectParentChecked(datas,datas[i],id,pid); }; } } function test() { var selRows = $table.bootstrapTable("getSelections"); if(selRows.length == 0){ alert("請至少選擇一行"); return; } var postData = ""; $.each(selRows,function(i) { postData += this.id; if (i < selRows.length - 1) { postData += ", "; } }); alert("你選中行的 id 為:"+postData); } </script> </html>
實現(xiàn)的效果圖為:
Bootstrap是目前最受歡迎的前端框架,它是基于 HTML、CSS、JAVASCRIPT 的,它簡潔靈活,使得 Web 開發(fā)更加快捷,它還有一個響應最好的Grid系統(tǒng),并且能夠在手機端通用,而Bootstrap是使用許多可重用的CSS和JavaScript組件,可以幫助實現(xiàn)需要的幾乎任何類型的網(wǎng)站的功能,此外,所有這些組件都是響應式的。
感謝各位的閱讀!關(guān)于“Bootstrap中table如何實現(xiàn)樹形表格聯(lián)動選中聯(lián)動取消功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
網(wǎng)站欄目:Bootstrap中table如何實現(xiàn)樹形表格聯(lián)動選中聯(lián)動取消功能
文章URL:http://jinyejixie.com/article18/pospgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航、外貿(mào)建站、網(wǎng)站改版、電子商務、網(wǎng)站收錄、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)