Home > Article > Web Front-end > How to implement layui to click a button to add a row to the table (manually added)
The content of this article is about how to implement layui to click the button to add a row to the table (manually add). It has certain reference value. Friends in need can refer to it. I hope it will help You helped.
Problem description: I want to implement the function of clicking a button to add a row to the table, but I found that layuii does not integrate the toolbar, so I need to add this function manually;
Originally I The written table is implemented like this:
$("#addTable").click(function(){ var tr=" <tr>"+ " <td>11</td>"+ " <td>22</td>"+ " <td>33</td>"+ " <td>44</td>"+ " <td>55</td>"+ " </tr>"; $(".layui-table").append(tr); });
cannot achieve the effect of adding; after querying, it is found that this is based on the fact that the table is written in a static way, that is:
<table class="layui-table" lay-data="{height:315, url:'/demo/table/user/', page:true, id:'tableInfo'}" lay-filter="test"> <thead> <tr> <th lay-data="{field:'id', width:80, sort: true}">ID</th> <th lay-data="{field:'username', width:80}">用户名</th> <th lay-data="{field:'sex', width:80, sort: true}">性别</th> <th lay-data="{field:'city'}">城市</th> <th lay-data="{field:'sign'}">签名</th> <th lay-data="{field:'experience', sort: true}">积分</th> <th lay-data="{field:'score', sort: true}">评分</th> <th lay-data="{field:'classify'}">职业</th> <th lay-data="{field:'wealth', sort: true}">财富</th> </tr> </thead> </table>
And I use this method:
<table class="layui-hide" id="baseInfo" lay-filter="demo" lay-data="{height: 'full-200', cellMinWidth: 80, page: true, limit:30}"></table>
The definition of column names is written in the table.render({}) method
Solution: Rewrite the table as static The method is enough, that is, the second part of the code
At this time, a new problem appears: two pieces of data will appear every time you click, and the following modifications should be made:
$(".layui-table-body .layui-table").append(tr);
Finally, click the button to create a new The effect of adding a piece of data.
Related recommendations:
JQuery implements dynamic tables. Click the button table to add a row_jquery
The above is the detailed content of How to implement layui to click a button to add a row to the table (manually added). For more information, please follow other related articles on the PHP Chinese website!