值得分享的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,分頁時(shí)使用總記錄數(shù)的鍵值為total
height: tableHeight(),//高度調(diào)整
search: true,//是否搜索
pagination: true,//是否分頁
pageSize: 20,//單頁記錄數(shù)
pageList: [5, 10, 20, 50],//分頁步進(jìn)值
sidePagination: "server",//服務(wù)端分頁
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
);
}
//分頁時(shí)需要獲取記錄總數(shù),鍵值為 total
$result["total"] = count($list_temp);
//根據(jù)傳遞過來的分頁偏移量和分頁量截取模擬分頁 rows 可以根據(jù)前端的 dataField 來設(shè)置
$result["rows"] = array_slice($list_temp, $_POST['offset'], $_POST['limit']);
echo json_encode($result);
需要注意的是
1、bootstrap table 可以前端分頁也可以后端分頁,這里我們使用的是后端分頁,后端分頁時(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è)精彩的專題:
以上就是本文的全部內(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)頁面鏈接收藏夾功能按鈕移動(dòng)收藏記錄(使用Sortable進(jìn)行拖動(dòng)排序)
- Bootstrap?table列上下移動(dòng)效果
相關(guān)文章
如何正確使用javascript 來進(jìn)行我們的程序開發(fā)
Javascript 正確使用方法,下面為大家介紹的是一個(gè)關(guān)于如何正確使用javascript 來進(jìn)行我們的程序開發(fā),需要的朋友可以參考下2014-06-06
uniapp開發(fā)安卓App實(shí)現(xiàn)高德地圖路線規(guī)劃導(dǎo)航功能的全過程
最近項(xiàng)目需要在APP內(nèi)實(shí)現(xiàn)路線規(guī)劃導(dǎo)航功能,直接打開高德地圖進(jìn)行導(dǎo)航,下面這篇文章主要給大家介紹了關(guān)于利用uniapp開發(fā)安卓App實(shí)現(xiàn)高德地圖路線規(guī)劃導(dǎo)航功能的相關(guān)資料,需要的朋友可以參考下2022-08-08
javascript學(xué)習(xí)總結(jié)之js使用技巧
本篇文章給大家分享javascript學(xué)習(xí)總結(jié)之js使用技巧,都是精華喔~小伙伴快來學(xué)習(xí)吧。2015-09-09
js 聲明數(shù)組和向數(shù)組中添加對(duì)象變量的簡單實(shí)例
下面小編就為大家?guī)硪黄猨s 聲明數(shù)組和向數(shù)組中添加對(duì)象變量的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07
JavaScript如何實(shí)現(xiàn)監(jiān)聽鍵盤輸入和鼠標(biāo)監(jiān)點(diǎn)擊
這篇文章主要介紹了JavaScript如何實(shí)現(xiàn)監(jiān)聽鍵盤輸入和鼠標(biāo)監(jiān)點(diǎn)擊,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

