這是一個表格渲染組件,demo 地址:https://lovewebgames.com/jsmodule/table.html ,git 源碼:https://github.com/tianxiangbing/table
如演示所示,功能覆蓋了後台表格裡最常見的幾件事:分頁、搜索、刪除以及 AJAX 操作。因為是用 Handlebars 做渲染的,所以樣式非常容易控制,想加新功能也比較省事。
<div class="form"> 名稱: <input type="text" name="gname"> <a href="#" id="search">search</a> </div> <div id="tab-list" ajaxurl="list.json">loading...</div> <div id="pager"></div>
<script type="text/x-handlebars-template" id="tpl-list">
<table class="tab-list">
<thead>
<tr>
<th class="first-cell">序號</th>
<th>商品條碼</th>
<th>商品名稱</th>
<th>狀態</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{{#each data}}
<tr>
<td class="first-cell">{{@index}}</td>
<td>{{goods_bn}}</td>
<td>{{goods_name}}</td>
<td>上架</td>
<td>
<a class="js-ajax" js-ajax-param="id={{goods_id}}" href="action.json">下架</a>
<a class="js-delete" href="action.json">刪除</a>
</td>
</tr>
{{/each}}
</tbody>
</table>
</script>
<script>
var table = new Table($('#tab-list'), $('#tpl-list'), $('#pager'), {}, $('#search'));
table.init({type:'post'});
</script>
constructor(table, temp, page, param, search, callback, filterCon)
table 是存放表格的容器,可以是一個空的 div,也可以是 table 裡的某個 tbody;temp 是表格模板,也就是上面那個 script 節點的 jQuery 對象;page 用來放置分頁控件;param 是初始化參數;search 是搜索按鈕節點,它的祖先級中需要有一個 class 為 form 的節點,組件會用 query 把裡面的表單項格式化為查詢參數;callback 是加載完成後的回調;filterCon 用作篩選過濾。
init(settings)
啟動方法,目前 settings 只包含 type 一項,用來指定 ajax 請求類型,例如 {type:'get'}。
以上所述就是本文的全部內容,希望大家能夠喜歡。