在使用layui做數(shù)據(jù)表格的時(shí)候,插入的數(shù)據(jù)的id是不是一組連續(xù)的數(shù)字,那么就需要用到layui的cols的type屬性。
table.render({ elem: '#test' ,url:'${pageContext.request.contextPath}/findcustomers' ,cols: [[ {align:'center', title: '編號(hào)', sort: true,type:'numbers',width:100} ]] ,page: true });
我們就會(huì)獲得一列有序的數(shù)列(ps:這里的必須加width屬性,使用minWidth沒有用,如果沒用width,那么會(huì)使用layui的表格默認(rèn)寬度40)
那么問題來了,我們的id怎么獲取呢,難道id再開一列嗎,如果id有意義,那么再開一列是可以的,如果沒有意義但有需要用的話,那么把id隱藏起來更為美觀。在layui2.4版本以前我們可以用layui的數(shù)據(jù)表格的done參數(shù)。
table.render({ elem: '#test' ,url:'${pageContext.request.contextPath}/findcustomers' ,cols: [[ {align:'center', title: '編號(hào)', sort: true,type:'numbers',width:100} ,{field:'cust_id', title: 'ID'} ]] ,done:function(res,curr,count){ // 隱藏列 $(".layui-table-box").find("[data-field='cust_id']").css("display","none"); } ,page: true });
可以將id隱藏,但有那么零點(diǎn)幾秒,id的列是加載出來了,最后隱藏的。那么有沒有更有效的辦法呢,只能layui自己在加載的時(shí)候,實(shí)現(xiàn)隱藏。所以在layui2.4版本的時(shí)候,layui的cols的參數(shù)新添了hide(隱藏)屬性。
table.render({ elem: '#test' ,url:'${pageContext.request.contextPath}/findcustomers' ,cols: [[ {align:'center', title: '編號(hào)', sort: true,type:'numbers',width:100} ,{field:'cust_id', title: 'ID', hide:true} ]] ,page: true });