搜尋
首頁web前端Layui教程使用layui渲染table資料表(實例)

使用layui渲染table資料表(實例)

Nov 29, 2019 pm 02:00 PM
layuitable

使用layui渲染table資料表(實例)

table 模組是layui框架最核心的組成之一。它用於對表格進行一些列功能和動態化資料操作,涵蓋了日常業務所涉及的幾乎全部需求。

支援固定表頭、固定行、固定列左/列右,支援拖曳改變列寬度,支援排序,支援多層表頭,支援儲存格的自訂模板,支援對表格重載(如搜尋、條件篩選等),支援複選框,支援分頁,支援儲存格編輯等等一些列功能。

儘管如此,我們仍將對其進行完善,在控製程式碼量和效能的前提下,不定期增加更多人性化功能。 table 模組也將是 layui 重點維護的項目之一。

layui渲染table資料表範例:

1、引入layui的css和js檔案

link rel="stylesheet" href="lib/layui/css/layui.css">
<script src="lib/layui/layui.js"></script>

2、在頁面放置一個table元素

<table class="layui-hide" id="test" lay-filter=&#39;test3&#39;></table>

3.透過 table.render() 方法指定該容器

layui.use(&#39;table&#39;, function(){
            var table = layui.table;
//            var playerName;
//            if(item != undefined) {
//                playerName=item;
//            }
            table.render({
                elem: &#39;#test&#39;  table 容器的选择器或 DOM
                ,url:&#39;http://boss.superlychee.com/rest/web/golfTourManage/getGolfTourPlayerByTourIdAndRounds&#39;
                ,method:&#39;post&#39;
                ,where:{tourId:tourIds,rounds:rounds,playerName:item}
                ,cellMinWidth: 80 //全局定义常规单元格的最小宽度,layui 2.2.1 新增
                ,cols: [[
                    {field:&#39;tourPlayerId&#39;, width:80, title: &#39;ID1&#39;, sort: true}
                    ,{field:&#39;playerName&#39;, width:80, title: &#39;姓名&#39;}
                    ,{field:&#39;hole1&#39;,  title: &#39;1&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole2&#39;, title: &#39;2&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole3&#39;, title: &#39;3&#39;,edit: &#39;text&#39;} //minWidth:局部定义当前单元格的最小宽度,layui 2.2.1 新增
                    ,{field:&#39;hole4&#39;, title: &#39;4&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole5&#39;, title: &#39;5&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole6&#39;, title: &#39;6&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole7&#39;, title: &#39;7&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole8&#39;, title: &#39;8&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole9&#39;, title: &#39;9&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole10&#39;, title: &#39;10&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole11&#39;, title: &#39;11&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole12&#39;, title: &#39;12&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole13&#39;, title: &#39;13&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole14&#39;, title: &#39;14&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole15&#39;, title: &#39;15&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole16&#39;, title: &#39;16&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole17&#39;, title: &#39;17&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;hole18&#39;, title: &#39;18&#39;,edit: &#39;text&#39;}
                    ,{field:&#39;add&#39;, title: &#39;操作&#39;, width:177,toolbar:"#barDemo"}

4、這時候你的頁面差不多就是以下這個樣子了

使用layui渲染table資料表(實例)

5、正文到了,怎麼把表格資料渲染進去呢?咱們上面第三部就是渲染的一中方法,叫“方法渲染”,layui官網提供的三種渲染

方法,在這裡不做陳述,方法渲染的優點是:你可以脫離HTML文件,而專注於JS本身。尤其對於專案的頻繁改動及發布,其便利性會體現得更為明顯,

layui的url預設是「get」請求,我這邊是post請求,所以記得加上「method」屬性為post,

6、容易碰到的問題,照理來說這個時候應該已經可以看到表格資料了,為什麼大多數人的表格還是渲染不出來呢?一般是因為你妹妹配置後台資料格式

response: {
    statusName: &#39;code&#39; //数据状态的字段名称,默认:code
    ,statusCode: 200 //成功的状态码,默认:0
    ,msgName: &#39;msg&#39; //状态信息的字段名称,默认:msg
    ,countName: &#39;count&#39; //数据总数的字段名称,默认:count
    ,dataName: &#39;data&#39; //数据列表的字段名称,默认:data

效果如下:

使用layui渲染table資料表(實例)

以上是使用layui渲染table資料表(實例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:CSDN。如有侵權,請聯絡admin@php.cn刪除

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器