首頁  >  文章  >  php教程  >  BootStrap 可編輯表格Table格

BootStrap 可編輯表格Table格

高洛峰
高洛峰原創
2016-12-05 14:15:432454瀏覽

一、 顯示資料(基礎功能)

在html頁面中定義表格以及表格的列名,最後把從資料庫中查詢出來的數據,循環顯示到頁面中。這個系統用的是PHP語言,裡邊用到了PHP中的語法,如果是Java語言,把php換成jsp中對應的語法就行

<div class="containe">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr class="success">
<th>序号</th>
<th style="display: none">ActionID</th>
<th>Category</th>
<th>SubProcess Name</th>
<th>Description</th>
<th>Do Action</th>
</tr>
</thead>
<tbody>
<?php
//遍历传递过来的变量$subprocess_info
$i=1;
foreach($subprocess_info as $_v){
?>
<tr id="">
<td><?php echo $i; ?></td>
<td style="display: none"><?php echo $_v->ActionID; ?></td>
<td><?php echo $_v->Category; ?></td>
<td><a href="#"><?php echo $_v->ActionName; ?></a></td>
<td><?php echo $_v -> Description; ?></td>
<td>
<a href="./index.php?r=subprocess/update&id=<?php echo $_v->ActionID; ?>">修改</a>
<a href="./index.php?r=subprocess/del&id=<?php echo $_v->ActionID; ?>">删除</a>
</td>
</tr>
<?php $i++; }?>
</tbody>
</table>
</div>

   

二、表格編輯(高級功能)

在html在頁面中,先定義一個表格,然後到js中初始化。這個功能引用了一個第三方插件,可以到這裡下載http://bootstrap-table.wenzhixin.net.cn/zh-cn/,這個插件是修改了http://bootstrap-table.wenzhixin.net.cn /zh-cn/ 裡邊的一些功能後​​形成的。在使用過程中,我做了一些小的改動,大家用的時候可以根據情況來

1. 效果展示

表格初始化後

BootStrap 可編輯表格Table格

添加新行

BootStrap 可編輯表格Table格

添加新行

添加新行

添加新行

添加新行

,首先需要引入它的js,我是統一引用到入口文件中的

<!--表格编辑-->
<link href="./assets/tableEdit/css/bootstrap-table.min.css" rel="stylesheet" />
<script src="./assets/tableEdit/js/bootstrap-table.js"></script>
<script src="./assets/tableEdit/js/bootstrap-table-edit.js"></script>
<script src="./assets/tableEdit/js/bootstrap-select.js"></script>
<script src="./assets/tableEdit/js/bootstrap-datetimepicker.min.js"></script>
<link href="./assets/tableEdit/css/bootstrap-datetimepicker.min.css" rel="stylesheet" />

   

BootStrap 可編輯表格Table格在頁面中定義表格,可添加自訂按鈕

<script src="./js/subprocess/subprocess.js"></script>
<div class="col-md-12">
<div style="float:right;margin:10px 0px 10px 5px">
<a title="Add" href="./index.php?r=subprocess/add">
<button type="button" class="btn btn-default" id="addData"<span style="color:#008000;background-color:#efefef;font-weight:bold;"></span>>
<span class="glyphicon glyphicon-plus"></span>
</button>
</a>
</div>
<table class="table table-striped table-bordered table-hover" id="subprocessTable"></table>
</div>

 

需要用下拉列表的,在定義列的時候這樣定義

$(function(){
//初始化表格
$(&#39;#subprocessTable&#39;).bootstrapTable({
method: &#39;get&#39;,
url:"./index.php?r=subprocess/subprocessInfo",
editable:true,//开启编辑模式
clickToSelect: true,
cache: false,
showToggle:true, //显示切换按钮来切换表/卡片视图。
showPaginationSwitch:true, //显示分页切换按钮
pagination: true,
pageList: [10,25,50,100],
pageSize:10,
pageNumber:1,
uniqueId: &#39;index&#39;, //将index列设为唯一索引
striped: true,
search: true,
showRefresh: true,
minimumCountColumns: 2,
smartDisplay:true,
columns: [
[
{field:"index",title:"ID",align:"center",edit:false,formatter:function(value, row, index){
return row.index=index ; //返回行号
}},
{field:"actionName",title:"ActionName",align:"center",order:"asc",sortable:"true",formatter:function(value,row,index){
var strHtml =&#39;<a href="./index.php?r=subprocess/modify&id=&#39;+ row.actionId +&#39;">&#39;+ row.actionName +&#39;</a>&#39;;
return strHtml;
}},
{field:"category",title:"Category",align:"center",sortable:"true"},
{field:"description",title:"Description",align:"center"},
{field:"action",title:"Action",align:"center",formatter:function(value,row,index){
var strHtml =&#39;<a href="./index.php?r=subprocess/modify&id=&#39;+ row.actionId +&#39;"><li class="glyphicon glyphicon-pencil"></li></a>&#39;+
&#39;<a href="javascript:void(0);" onclick="removeData(&#39;+ index +&#39;)" style="margin-left:5px;"><li class="glyphicon glyphicon-remove"></li></a>&#39;;
return strHtml;
},edit:false},
{field:"actionId",title:"ActionID",align:"center",edit:false,visible:false,searchable:false}
]
]
});
 
/**
* add a new row
*/
$(&#39;#addData&#39;).click(function(){
$(&#39;#subprocessTable&#39;).bootstrapTable(&#39;selectPage&#39;, 1); //Jump to the first page
var data = {actionId: &#39;&#39;, actionName: &#39;&#39;,category:&#39;&#39;, description: &#39;&#39;}; //define a new row data,certainly it&#39;s empty
 
$(&#39;#subprocessTable&#39;).bootstrapTable(&#39;prepend&#39;, data); //the method of prepend must defined all fields,but append needn&#39;t
//$(&#39;#dataTable&#39;).bootstrapTable(&#39;append&#39;,data);
 
$("#dataTable tr:eq(1) td:eq(0)").trigger("dblclick");
$("#dataTable input")[0].focus();
});
});

   

效果如下

BootStrap 可編輯表格Table格

🎜效果如下🎜🎜🎜🎜js🎜🎜效果如下🎜🎜🎜🎜js🎜 🎜🎜🎜三、動態表頭🎜🎜動態表頭,說到底就是每次的列資料是不固定的,根據前提條件查詢資料庫,再根據查詢結果載入表頭。有了上邊的修改,實作這個功能已經不在話下,只要把初始化表格的columns替換成我們自訂的資料就可以了,做了個簡單的小demo,具體的可以看【EasyUi DataGrid】動態載入列這篇文章🎜
{field:"toRun",title:"Run Flag",align:"center",edit:{
type:&#39;select&#39;,//下拉框
url:&#39;./index.php?r=dictionary/dictionaryInfo&type=&#39;+"run",
//data:[{id:1,text:&#39;hello&#39;},{id:2,text:&#39;hi&#39;}],
valueField:&#39;id&#39;,
textField:&#39;text&#39;,
editable : false,
onSelect:function(val,rec){
//console.log(val,rec);
}
},sortable:true}
🎜   🎜🎜🎜🎜效果如下:🎜🎜🎜🎜
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn