您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)使用layui怎么實(shí)現(xiàn)一個(gè)點(diǎn)擊按鈕添加一行功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
解決方案:
方案1、table 是用轉(zhuǎn)換靜態(tài)表格的方式創(chuàng)建的,寫一個(gè)button,每次點(diǎn)擊按鈕,就添加一個(gè)<tr> 標(biāo)簽;
方案2、table 是用方法渲染的方式創(chuàng)建的,寫一個(gè)button,每次點(diǎn)擊按鈕,加載數(shù)據(jù)時(shí)添加一個(gè)Object;
之前我試過用方案1 來實(shí)現(xiàn)該功能,發(fā)現(xiàn)這個(gè)方案,代碼量極大,最困難的地方就是在<tr>中加樣式,特別是table中有很多種控件時(shí),樣式的添加非常麻煩 ,可參見點(diǎn)擊打開鏈接
后選擇使用方案2 ,但是遇到了,添加的對(duì)象無法通過加載url來渲染t
原來的數(shù)據(jù)表格使用方法渲染的方法傳的值,其數(shù)據(jù)是從url中取出,
發(fā)現(xiàn)layui的url傳值方式,數(shù)據(jù)格式必須為:
{"code":0,"msg":"","count":4,"data":[{"colName":"ID","colNo":0,"collator":"","comments":"","dbId":1,"defVal":"","deleted":"","dispersion":0,"domainId":0,"histogram":"","isHide":"","isSerial":"F","isVirtual":"","maxVal":"","minVal":"","notNull":"T","repetRate":0,"scale":-1,"serialId":0,"tableId":1048586,"timestampT":" ","typeName":"INTEGER","varying":"F","vcolNo":0,"vertNo":0}]}
而我在reload方法中存放的data數(shù)據(jù)格式為
[{"colName":"ID1","colNo":0,"collator":"","comments":"","dbId":1,"defVal":"","deleted":"","dispersion":0,"domainId":0,"histogram":"","isHide":"","isSerial":"F","isVirtual":"","maxVal":"","minVal":"","notNull":"T","repetRate":0,"scale":-1,"serialId":0,"tableId":1048586,"timestampT":" ","typeName":"INTEGER","varying":"F","vcolNo":0,"vertNo":0}]
因此,解決方案:
1、將新增的data格式修改為url返回的格式,失??;
2、將url的返回值,以data的array格式返回,并作為變量傳給data;成功。
全部方法如下:
首先:用ajax請(qǐng)求將數(shù)據(jù)取出存于變量,將變量賦值給table的data
var tableData=new Array(); // 用于存放表格數(shù)據(jù) $.ajax({ url: "${ctp}/TableOperate/GetColsInfo?tabId=1048586&dbId=1" ,type:"get" ,async:false ,dataType:"json" , success: function(result){ tableData=result; console.log(result); } }); table.render({ elem: '#baseInfo' ,data:tableData ,cols: [[ {title : '序號(hào)',type:'numbers',Width: 20} /* ,{field:'tableId' , title:'tableId' } ,{field:'dbId' , title:'dbId' } */ ,{field:'colNo' , title:'colNo' , sort: true} ,{field:'domainId' , title:'domainId', minWidth: 120, templet: '#switchTpl', unresize: true } ,{field:'colName' , title:'colName' , minWidth: 120, sort: true , edit: 'text'} ,{field:'typeName' , title:'typeName', minWidth: 120, sort: true ,templet: '#selectTpl'} ,{field:'scale' , title:'scale' , minWidth: 80, edit: 'text'} ,{field:'notNull' , title:'notNull' , minWidth: 100, templet: '#switchNullTpl', unresize: true} ]] ,page: { //支持傳入 laypage 組件的所有參數(shù)(某些參數(shù)除外,如:jump/elem) - 詳見文檔 layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定義分頁(yè)布局 ,curr: 1 //設(shè)定初始在第 1 頁(yè) ,groups: 1 //只顯示 1 個(gè)連續(xù)頁(yè)碼 ,first: false //不顯示首頁(yè) ,last: false //不顯示尾頁(yè) } , done: function(res, curr, count){ } });
最后,調(diào)用按鈕的點(diǎn)擊方法
//點(diǎn)擊加號(hào)按鈕時(shí),新添一行 $("#addTable").click(function(){ var oldData = table.cache["baseInfo"]; var data1={"colName":"ID2","colNo":0,"collator":"","comments":"","dbId":1,"defVal":"","deleted":"","dispersion":0,"domainId":0,"histogram":"","isHide":"","isSerial":"F","isVirtual":"","maxVal":"","minVal":"","notNull":"T","repetRate":0,"scale":-1,"serialId":0,"tableId":1048586,"timestampT":" ","typeName":"INTEGER","varying":"F","vcolNo":0,"vertNo":0}; oldData.push(data1); table.reload('baseInfo',{ data : oldData }); });
上述就是小編為大家分享的使用layui怎么實(shí)現(xiàn)一個(gè)點(diǎn)擊按鈕添加一行功能了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。