使用layui 渲染table數(shù)據(jù)表格的實(shí)例代碼
先上最終效果圖:

1,引入layui的css和js文件
<link rel="stylesheet" href="lib/layui/css/layui.css" rel="external nofollow" > <script src="lib/layui/layui.js"></script>
2,在頁(yè)面放置一個(gè)table元素
<table class="layui-hide" id="test" lay-filter='test3'></table>
3,通過(guò) table.render() 方法指定該容器
layui.use('table', function(){
var table = layui.table;
// var playerName;
// if(item != undefined) {
// playerName=item;
// }
table.render({
elem: '#test' table 容器的選擇器或 DOM
,url:'http://boss.superlychee.com/rest/web/golfTourManage/getGolfTourPlayerByTourIdAndRounds'
,method:'post'
,where:{tourId:tourIds,rounds:rounds,playerName:item}
,cellMinWidth: 80 //全局定義常規(guī)單元格的最小寬度,layui 2.2.1 新增
,cols: [[
{field:'tourPlayerId', width:80, title: 'ID1', sort: true}
,{field:'playerName', width:80, title: '姓名'}
,{field:'hole1', title: '1',edit: 'text'}
,{field:'hole2', title: '2',edit: 'text'}
,{field:'hole3', title: '3',edit: 'text'} //minWidth:局部定義當(dāng)前單元格的最小寬度,layui 2.2.1 新增
,{field:'hole4', title: '4',edit: 'text'}
,{field:'hole5', title: '5',edit: 'text'}
,{field:'hole6', title: '6',edit: 'text'}
,{field:'hole7', title: '7',edit: 'text'}
,{field:'hole8', title: '8',edit: 'text'}
,{field:'hole9', title: '9',edit: 'text'}
,{field:'hole10', title: '10',edit: 'text'}
,{field:'hole11', title: '11',edit: 'text'}
,{field:'hole12', title: '12',edit: 'text'}
,{field:'hole13', title: '13',edit: 'text'}
,{field:'hole14', title: '14',edit: 'text'}
,{field:'hole15', title: '15',edit: 'text'}
,{field:'hole16', title: '16',edit: 'text'}
,{field:'hole17', title: '17',edit: 'text'}
,{field:'hole18', title: '18',edit: 'text'}
,{field:'add', title: '操作', width:177,toolbar:"#barDemo"}
]],
});
});
4,這個(gè)時(shí)候你的頁(yè)面差不多就是以下這個(gè)樣子了

5,正文到了,怎么把表格數(shù)據(jù)渲染進(jìn)去呢?咱們上面第三部就是渲染的一中方法,叫“方法渲染”,layui官網(wǎng)提供的三種渲染
方法,在這里不做陳述,方法渲染的優(yōu)點(diǎn)是:你可以脫離HTML文件,而專注于JS本身。尤其對(duì)于項(xiàng)目的頻繁改動(dòng)及發(fā)布,其便捷性會(huì)體現(xiàn)得更為明顯,
layui的url默認(rèn)是“get”請(qǐng)求,我這邊是post請(qǐng)求,所以記得加上“method”屬性為post,
6,容易碰到的問(wèn)題,按理來(lái)說(shuō)這個(gè)時(shí)候應(yīng)該已經(jīng)可以看到表格數(shù)據(jù)了,為什么大多數(shù)人的表格還是渲染不出來(lái)呢?一般是因?yàn)槟忝门渲煤笈_(tái)數(shù)據(jù)格式
response: {
statusName: 'code' //數(shù)據(jù)狀態(tài)的字段名稱,默認(rèn):code
,statusCode: 200 //成功的狀態(tài)碼,默認(rèn):0
,msgName: 'msg' //狀態(tài)信息的字段名稱,默認(rèn):msg
,countName: 'count' //數(shù)據(jù)總數(shù)的字段名稱,默認(rèn):count
,dataName: 'data' //數(shù)據(jù)列表的字段名稱,默認(rèn):data
}
這個(gè)時(shí)候表格一般已經(jīng)是出來(lái)了,記得,后臺(tái)提供的數(shù)據(jù)格式,和layui提供的一樣最好,這一點(diǎn)要切記,這一點(diǎn)大家可以去官網(wǎng)上看下,不做說(shuō)明。
以上這篇使用layui 渲染table數(shù)據(jù)表格的實(shí)例代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript 數(shù)組- Array的方法總結(jié)(推薦)
下面小編就為大家?guī)?lái)一篇JavaScript 數(shù)組- Array的方法總結(jié)(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07
echarts折線圖每段顯示不同的顏色的實(shí)現(xiàn)
本文主要介紹了echarts折線圖每段顯示不同的顏色的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09
理解JavaScript設(shè)計(jì)模式中的單例模式
這篇文章主要介紹了理解JavaScript設(shè)計(jì)模式中的單例模式,單例模式即Singleton?Pattern是最簡(jiǎn)單的設(shè)計(jì)模式之一,下文更多相關(guān)介紹感興趣的小伙伴可以參考一下2022-04-04
JS實(shí)現(xiàn)鼠標(biāo)拖拽盒子移動(dòng)及右鍵點(diǎn)擊盒子消失效果示例
這篇文章主要介紹了JS實(shí)現(xiàn)鼠標(biāo)拖拽盒子移動(dòng)及右鍵點(diǎn)擊盒子消失效果,涉及javascript事件響應(yīng)及頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-01-01

