欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

值得分享的bootstrap table實(shí)例

 更新時(shí)間:2016年09月22日 15:40:42   作者:sallency  
這篇文章主要為大家詳細(xì)介紹了一個(gè)值得分享的bootstrap table實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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">
  &times;
 </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è)精彩的專題:

Bootstrap學(xué)習(xí)教程

Bootstrap實(shí)戰(zhàn)教程

Bootstrap Table使用教程

Bootstrap插件使用教程

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論