值得分享的bootstrap table實(shí)例
bootstrap table 封裝了一套完善的數(shù)據(jù)表格組件,把下面的代碼復(fù)制一下估計(jì)你需要的基本功能都有了,沒有的再看看手冊(cè)對(duì)比著我給的實(shí)例也能很快的熟悉了。
客戶端
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Bootstrap-Table</title> <link rel="stylesheet" /> <link rel="stylesheet" href="assets/bootstrap-table.css"/> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> </head> <body> <div> <div> <div class="col-*-12"> <div id="toolbar"> <div class="btn btn-primary" data-toggle="modal" data-target="#addModal">添加記錄</div> </div> <table id="mytab" class="table table-hover"></table> <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> × </button> <h4 class="modal-title" id="myModalLabel">添加記錄</h4> </div> <div class="modal-body"> <form role="form" action="javascript:void(0)"> <div class="form-group"> <input type="text" class="form-control" id="name" placeholder="請(qǐng)輸入名稱"> </div> <div class="form-group"> <input type="text" class="form-control" id="age" placeholder="請(qǐng)輸入年齡"> </div> </form> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">取消</button> <button type="button" class="btn btn-primary" id="addRecord">提交</button> </div> </div> </div> </div> </div> </div> </div> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script src="http://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js"></script> <script src="assets/bootstrap-table.js"></script> <script src="assets/bootstrap-table-zh-CN.js"></script> <script type="text/javascript"> $(function() { //根據(jù)窗口調(diào)整表格高度 $(window).resize(function() { $('#mytab').bootstrapTable('resetView', { height: tableHeight() }) }) $('#mytab').bootstrapTable({ url: "index.php",//數(shù)據(jù)源 dataField: "rows",//服務(wù)端返回?cái)?shù)據(jù)鍵值 就是說記錄放的鍵值是rows,分頁(yè)時(shí)使用總記錄數(shù)的鍵值為total height: tableHeight(),//高度調(diào)整 search: true,//是否搜索 pagination: true,//是否分頁(yè) pageSize: 20,//單頁(yè)記錄數(shù) pageList: [5, 10, 20, 50],//分頁(yè)步進(jìn)值 sidePagination: "server",//服務(wù)端分頁(yè) contentType: "application/x-www-form-urlencoded",//請(qǐng)求數(shù)據(jù)內(nèi)容格式 默認(rèn)是 application/json 自己根據(jù)格式自行服務(wù)端處理 dataType: "json",//期待返回?cái)?shù)據(jù)類型 method: "post",//請(qǐng)求方式 searchAlign: "left",//查詢框?qū)R方式 queryParamsType: "limit",//查詢參數(shù)組織方式 queryParams: function getParams(params) { //params obj params.other = "otherInfo"; return params; }, searchOnEnterKey: false,//回車搜索 showRefresh: true,//刷新按鈕 showColumns: true,//列選擇按鈕 buttonsAlign: "left",//按鈕對(duì)齊方式 toolbar: "#toolbar",//指定工具欄 toolbarAlign: "right",//工具欄對(duì)齊方式 columns: [ { title: "全選", field: "select", checkbox: true, width: 20,//寬度 align: "center",//水平 valign: "middle"http://垂直 }, { title: "ID",//標(biāo)題 field: "id",//鍵名 sortable: true,//是否可排序 order: "desc"http://默認(rèn)排序方式 }, { field: "name", title: "NAME", sortable: true, titleTooltip: "this is name" }, { field: "age", title: "AGE", sortable: true, }, { field: "info", title: "INFO[using-formatter]", formatter: 'infoFormatter',//對(duì)本列數(shù)據(jù)做格式化 } ], onClickRow: function(row, $element) { //$element是當(dāng)前tr的jquery對(duì)象 $element.css("background-color", "green"); },//單擊row事件 locale: "zh-CN"http://中文支持, detailView: false, //是否顯示詳情折疊 detailFormatter: function(index, row, element) { var html = ''; $.each(row, function(key, val){ html += "<p>" + key + ":" + val + "</p>" }); return html; } }); $("#addRecord").click(function(){ alert("name:" + $("#name").val() + " age:" +$("#age").val()); }); }) function tableHeight() { return $(window).height() - 50; } /** * 列的格式化函數(shù) 在數(shù)據(jù)從服務(wù)端返回裝載前進(jìn)行處理 * @param {[type]} value [description] * @param {[type]} row [description] * @param {[type]} index [description] * @return {[type]} [description] */ function infoFormatter(value, row, index) { return "id:" + row.id + " name:" + row.name + " age:" + row.age; } </script> </body> </html>
服務(wù)端:
<?php /** * 服務(wù)端模擬數(shù)據(jù) */ //前端期望數(shù)據(jù)為json header("Content-Type:application/json;charset=utf-8"); //post 請(qǐng)求 請(qǐng)求內(nèi)容類型為 application/x-www-form-urlencoded 如果是 application/json 則需要另行處理 $_POST 數(shù)組不會(huì)被填充 //為了保持模擬的數(shù)據(jù) session_start(); if ($_SESSION['emulate_data']) { //已生成 } else { $list = []; //第一次會(huì)模擬個(gè)數(shù)據(jù) for($i = 1; $i < 50; $i ++) { $list[] = [ "id" => $i, "name" => substr(str_shuffle(implode('', range('a', 'z'))), 0, 5), "age" => mt_rand(10, 30) ]; } $_SESSION['emulate_data'] = $list; } $list_temp = []; //檢索 if (isset($_POST['search']) && !empty($_POST['search'])) { foreach ($_SESSION['emulate_data'] as $key => $row) { if (strpos($row['name'], $_POST['search']) !== false || strpos($row['age'], $_POST['search']) !== false) { $list_temp[] = $_SESSION['emulate_data'][$key]; } } } else { $list_temp = $_SESSION['emulate_data']; } //排序 if (isset($_POST['sort'])) { $temp = []; foreach ($list_temp as $row) { $temp[] = $row[$_POST['sort']]; } //php的多維排序 array_multisort($temp, $_POST['sort'] == 'name' ? SORT_STRING : SORT_NUMERIC, $_POST['order'] == 'asc' ? SORT_ASC : SORT_DESC, $list_temp ); } //分頁(yè)時(shí)需要獲取記錄總數(shù),鍵值為 total $result["total"] = count($list_temp); //根據(jù)傳遞過來的分頁(yè)偏移量和分頁(yè)量截取模擬分頁(yè) rows 可以根據(jù)前端的 dataField 來設(shè)置 $result["rows"] = array_slice($list_temp, $_POST['offset'], $_POST['limit']); echo json_encode($result);
需要注意的是
1、bootstrap table 可以前端分頁(yè)也可以后端分頁(yè),這里我們使用的是后端分頁(yè),后端分頁(yè)時(shí)需返回含有
total:總記錄數(shù) 這個(gè)鍵值好像是固定的,我看文檔沒找到可以修改成別的
rows: 記錄集合 鍵值可以修改 dataField 自己定義成自己想要的就好
{ "total":200, "rows":[ {"id":1, "name":"sallency", "age": 26}, {"id":1, "name":"sallency", "age": 26}, {"id":1, "name":"sallency", "age": 26}, {"id":1, "name":"sallency", "age": 26}, {"id":1, "name":"sallency", "age": 26}] }
如上的json數(shù)據(jù)(當(dāng)然我前臺(tái)設(shè)置的期望數(shù)據(jù)類型是json,php 直接encode一個(gè) ["total"=>200, "rows"=>[[],[],][,][,]]的數(shù)組就完事了,方便)
2、且其請(qǐng)求后端是傳遞的內(nèi)容格式默認(rèn)為 application/json 我自己習(xí)慣用方便的 x-www-form-urlencoded
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊這里進(jìn)行學(xué)習(xí),再為大家附3個(gè)精彩的專題:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Bootstrap Table 在指定列中添加下拉框控件并獲取所選值
- Bootstrap Table列寬拖動(dòng)的方法
- bootstrap table列和表頭對(duì)不齊的解決方法
- Bootstrap Table使用方法詳解
- JS組件Bootstrap Table使用方法詳解
- bootstrap table實(shí)現(xiàn)單擊單元格可編輯功能
- bootstrap table單元格新增行并編輯
- 值得分享的輕量級(jí)Bootstrap Table表格插件
- 基于BootStrap的Metronic框架實(shí)現(xiàn)頁(yè)面鏈接收藏夾功能按鈕移動(dòng)收藏記錄(使用Sortable進(jìn)行拖動(dòng)排序)
- Bootstrap?table列上下移動(dòng)效果
相關(guān)文章
pc加載更多功能和移動(dòng)端下拉刷新加載數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了pc加載更多功能和移動(dòng)端下拉刷新加載數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11JavaScript中Set和Map數(shù)據(jù)結(jié)構(gòu)使用場(chǎng)景詳解
這篇文章主要為大家介紹了JavaScript中Set和Map數(shù)據(jù)結(jié)構(gòu)使用場(chǎng)景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06解決layui中的form表單與button的點(diǎn)擊事件沖突問題
今天小編就為大家分享一篇解決layui中的form表單與button的點(diǎn)擊事件沖突問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08js點(diǎn)擊按鈕實(shí)現(xiàn)多張圖片循環(huán)切換
這篇文章主要為大家詳細(xì)介紹了js點(diǎn)擊按鈕實(shí)現(xiàn)多張圖片循環(huán)切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01JavaScript canvas實(shí)現(xiàn)俄羅斯方塊游戲
這篇文章主要為大家詳細(xì)介紹了JavaScript canvas實(shí)現(xiàn)俄羅斯方塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07微信小程序的宿主環(huán)境實(shí)現(xiàn)代碼
這篇文章主要介紹了微信小程序的宿主環(huán)境,包括scroll-view 組件的基本使用,text 組件的基本使用及rich-text 組件的基本使用,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10JS中使用textPath實(shí)現(xiàn)線條上的文字
最近項(xiàng)目經(jīng)理交給我一下新項(xiàng)目,要實(shí)現(xiàn)關(guān)系圖,需要在線條上繪制文字。下面小編把使用textPath實(shí)現(xiàn)線條上的文字功能分享到腳本之家平臺(tái)供大家參考2017-12-12