JS實(shí)現(xiàn)layui?table篩選框記憶功能
碰到表中列很多如下表
使用layui table的篩選功能.選完之后呢,關(guān)掉瀏覽器再打開或者換個(gè)頁(yè)面再打開的時(shí)候,選擇就白選了.這種情況下,客戶就要求加個(gè)記憶功能.讓她下次再打開的時(shí)候,還能記憶上次選擇的
網(wǎng)上幾乎沒有這種使用的例子.總之是沒有找到相關(guān)資料,于是我就把實(shí)現(xiàn)的過(guò)程記錄下來(lái),方便大家用到的時(shí)候做個(gè)參考.
實(shí)現(xiàn): 記憶的數(shù)據(jù)可以存到數(shù)據(jù)庫(kù),可以存到本地緩存
本案例放入本地緩存的方式
使用MutationObserver 實(shí)現(xiàn)監(jiān)控點(diǎn)擊事件.
由于篩選的跳窗是點(diǎn)開后后加的代碼,所以一般的事件onclick是觸發(fā)不到的.. 就是點(diǎn)擊篩選按鈕,此時(shí)加載跳出框代碼, 也就在此是注冊(cè)onclik 點(diǎn)擊事件是不行的. 如果早注冊(cè)事件,早于頁(yè)面元素注冊(cè),也是抓不到事件滴.頁(yè)面還沒渲染出來(lái),早早的注冊(cè)了頁(yè)面里邊的點(diǎn)擊事件,后來(lái)頁(yè)面渲染出來(lái)后,點(diǎn)擊是無(wú)法響應(yīng)的,有個(gè)先后順序.
經(jīng)過(guò)控制臺(tái)點(diǎn)擊按鈕分析頁(yè)面代碼等等操作
/////篩選框記憶功能
//1頁(yè)面打開,先讀本地緩存
//2讀入cols 設(shè)置hide true 或者false
//3渲染table
//4加入 篩選框選擇框事件獲取 并設(shè)置本地緩存
<script src="JQuery/jquery-2.2.2.min.js"></script> <script src="/layui-v2.6.4/layui.js"></script> <script> layui.use(['table','form'], function(){ var table = layui.table; var $ = layui.$;//等同于jquery var form = layui.form; //選擇 form.on('select(TableName)', function(data){ $.ajax({ type: 'get', url: '/sss', data: {OptionTableID:data.value}, async:false, dataType: 'json', success: function (data) { console.log(data); var strHtml = "<option value=''>請(qǐng)選擇</option>"; for (var i = 0; i < data.length; i++) { strHtml += "<option value='"+data[i].OptionColumnID+"'>"+data[i].ColumnDisplayName+"</option>"; } $("#OptionColumnID").html(strHtml); form.render('select'); }, error:function(e){ } }); }); /* window.localStorage.setItem('UserName',$("#UserName").val()); window.localStorage.setItem('Password',$("#Password").val());*/ /////篩選框記憶功能 //1頁(yè)面打開,先讀本地緩存 //2讀入cols 設(shè)置hide true 或者false //3渲染table //4加入 篩選框選擇框事件獲取 并設(shè)置本地緩存 var cols=[[ {checkbox: true, field:'OptionColumnValueID'}, {field: "OptionColumnValueID", hide:false, title: "ID", sort: true}, {field: "ColumnValue", hide:false, title: "字典名稱"} , {title: "操作", align: "center", fixed: "right", templet: "#barDemo"} ]]; intCols(); function intCols() { for (var i=0;i<cols[0].length;i++) { if(cols[0][i].field!=undefined) { let localfield='test'+cols[0][i].field; let hidevalue =window.localStorage.getItem(localfield); if(hidevalue==='false') { cols[0][i].hide=false; }else if(hidevalue==='true') { cols[0][i].hide=true; } } } } //方法級(jí)渲染 table.render({ elem: '#LAY_table_user'//table元素的ID ,id: 'test'//容器的ID //,toolbar: "#toolbarTpl" ,toolbar: '#toolbarDemo' //開啟頭部工具欄,并為其綁定左側(cè)模板 ,defaultToolbar: ['filter', 'exports', 'print'] ,url: '/ist' ,cols: cols /* ,done: function () { $("[data-field='OptionColumnValueID']").css('display','none'); }*/ ,page: true ,limits: [10,1000,10000] ,limit: 10 ,where: { } }); /////篩選框記憶功能 //1頁(yè)面打開,先讀本地緩存 //2讀入cols 設(shè)置hide true 或者false //3渲染table //4加入 篩選框選擇框事件獲取 并設(shè)置本地緩存 // 選擇需要觀察變動(dòng)的節(jié)點(diǎn) const targetNode =document.getElementsByClassName('layui-table-tool');//document.getElementById('some- const targetNode1 =document.getElementsByClassName('layui-table-tool-self')[0];//document.getElementById('some-id'); // console.log(targetNode); // console.log(targetNode1); // 觀察器的配置(需要觀察什么變動(dòng)) const config = { attributes: true, childList: true, subtree: true }; // 當(dāng)觀察到變動(dòng)時(shí)執(zhí)行的回調(diào)函數(shù) const callback = function(mutationsList, observer) { console.log(mutationsList); // console.log(observer); // Use traditional 'for loops' for IE 11 for(let mutation of mutationsList) { if (mutation.type === 'childList') { // console.log('A child node has been added or removed'); } else if (mutation.type === 'attributes') { console.log(mutation.target.innerText); //先根據(jù)innertext 列名稱 對(duì)cols 進(jìn)行field 查詢,查到field 可以找到本地緩存的字段,約定,本地緩存的命名規(guī)則為表名字母縮寫_加field名組成,防止沖突 var field=""; for (var i=0;i<cols[0].length;i++) { if(cols[0][i].title===mutation.target.innerText) //標(biāo)題相同 則取field { field=cols[0][i].field; break; } } if(field!=="") { // 組裝緩存key let localkey='test'+field; //判斷value值 if(mutation.target.classList[2]!=undefined) //說(shuō)明2: "layui-form-checked" 復(fù)選框是已選擇的,說(shuō)明此列是在表中顯示的 { window.localStorage.setItem(localkey,false); }else //沒被選擇,說(shuō)明此列不在table中顯示 { window.localStorage.setItem(localkey,true); } } } } }; // 創(chuàng)建一個(gè)觀察器實(shí)例并傳入回調(diào)函數(shù) const observer = new MutationObserver(callback); // 以上述配置開始觀察目標(biāo)節(jié)點(diǎn) observer.observe(targetNode1, config); //監(jiān)聽工具條 table.on('toolbar()', function(obj){ var data = obj.data; console.log(obj); }); //監(jiān)聽工具條 table.on('tool(user)', function(obj){ var data = obj.data; console.log(obj); if(obj.event === 'del'){ layer.confirm('確定要?jiǎng)h除:'+data.ColumnValue+'么?', { title: '刪除', btn: ['確定', '取消'] },function(index){ autid(data.OptionColumnValueID,table); //obj.del(); layer.close(index); }); }else if(obj.event === 'edit'){ window.location.href="/xxx/xxxx } else if(obj.event === 'LAYTABLE_COLS'){ console.log(123) ; } }); //監(jiān)聽工具條結(jié)束 //監(jiān)聽排序 table.on('sort(user)', function(obj){ //注:tool是工具條事件名,test是table原始容器的屬性 lay-filter="對(duì)應(yīng)的值" table.reload('test', {//刷新列表 initSort: obj //記錄初始排序,如果不設(shè)的話,將無(wú)法標(biāo)記表頭的排序狀態(tài)。 layui 2.1.1 新增參數(shù) ,where: { //請(qǐng)求參數(shù) field: obj.field //排序字段 ,order: obj.type //排序方式 } }); }); //監(jiān)聽排序結(jié)束 //查詢 $("#chaxun").click(function(){ table.reload('test', {//刷新列表 where: { OptionTableID:$('#TableName').val() ,OptionColumnID:$('#xxx').val() },page: { curr: 1 //重新從第 1 頁(yè)開始 } }); }) //批量 $("#allDel").click(function(){ var checkStatus = table.checkStatus('test') ,data = checkStatus.data; if (data === undefined || data.length == 0) { layer.alert("請(qǐng)勾選要操作的數(shù)據(jù)!") }else{ var itemids=''; for(var o in data){ itemids +=data[o].xxx+","; } layer.confirm('確定要?jiǎng)h除選中的數(shù)據(jù)嗎?', { title: '刪除', btn: ['確定', '取消'] },function(index){ autid(itemids,table); //obj.del(); layer.close(index); }); } }); }); function autid(itemids,table){ var indexload = layer.load(1, { shade: [0.3,'#000'], success: function(layero, indexload){ $.ajax({ url: '/xxx', type:'POST', dataType: 'json', data: { itemIds:itemids }, success: function (ret) { console.log(ret) if(ret.res=="ok"){ layer.alert('操作成功!', function(index){ layer.close(index); layer.close(indexload); table.reload('test', { where: { } }); }); }else{ layer.close(indexload); } } }); } }); } </script>
到此這篇關(guān)于實(shí)現(xiàn)layui table篩選框記憶功能的文章就介紹到這了,更多相關(guān)layui table篩選框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解如何用js實(shí)現(xiàn)一個(gè)網(wǎng)頁(yè)版節(jié)拍器
這篇文章主要為大家介紹了詳解如何用js實(shí)現(xiàn)一個(gè)網(wǎng)頁(yè)版節(jié)拍器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01一個(gè)很簡(jiǎn)單的辦法實(shí)現(xiàn)TD的加亮效果.
一個(gè)很簡(jiǎn)單的辦法實(shí)現(xiàn)TD的加亮效果....2006-06-06微信小程序 教程之wxapp視圖容器 scroll-view
這篇文章主要介紹了微信小程序 教程之wxapp視圖容器 scroll-view的相關(guān)資料,需要的朋友可以參考下2016-10-10一款功能強(qiáng)大的markdown編輯器tui.editor使用示例詳解
這篇文章主要為大家介紹了一款功能強(qiáng)大的markdown編輯器tui.editor使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02微信小程序動(dòng)態(tài)的加載數(shù)據(jù)實(shí)例代碼
這篇文章主要介紹了 微信小程序動(dòng)態(tài)的加載數(shù)據(jù)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04一篇文章教你學(xué)會(huì)js實(shí)現(xiàn)彈幕效果
彈幕效果隨著b站的越做越強(qiáng),出現(xiàn)了越來(lái)越多的仿照b站的視頻站點(diǎn)。然而這些視頻站仿照的最多的只有一點(diǎn)!那就是彈幕,現(xiàn)在也越來(lái)越多的人喜歡上了彈幕本文就教你如何制作2021-08-08微信小程序 滾動(dòng)到某個(gè)位置添加class效果實(shí)現(xiàn)代碼
這篇文章主要介紹了微信小程序 滾動(dòng)到某個(gè)位置添加class效果實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04JavaScript?Canvas實(shí)現(xiàn)噪點(diǎn)濾鏡回憶童年電視雪花屏
這篇文章主要為大家介紹了JavaScript?Canvas實(shí)現(xiàn)噪點(diǎn)濾鏡回憶童年電視雪花屏,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09