BootStrap 可編輯表Table格
一、 顯示數(shù)據(jù)(基礎(chǔ)功能)
在html頁面中定義表格以及表格的列名,最后把從數(shù)據(jù)庫中查詢出來的數(shù)據(jù),循環(huán)顯示到頁面中。這個(gè)系統(tǒng)用的是PHP語言,里邊用到了PHP中的語法,如果是Java語言,把php換成jsp中對(duì)應(yīng)的語法就行
<div class="containe"> <table class="table table-striped table-bordered table-hover"> <thead> <tr class="success"> <th>序號(hào)</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>
二、表格編輯(高級(jí)功能)
在html頁面中,先定義一個(gè)表格,然后到j(luò)s中初始化。這個(gè)功能引用了一個(gè)第三方插件,可以到這里下載 http://bootstrap-table.wenzhixin.net.cn/zh-cn/,這個(gè)插件是修改了 http://bootstrap-table.wenzhixin.net.cn/zh-cn/ 里邊的一些功能后形成的。在使用過程中,我做了一些小的改動(dòng),大家用的時(shí)候可以根據(jù)情況來
1. 效果展示
表格初始化后
添加新行
2. 在使用時(shí),首先需要引入它的js,我是統(tǒng)一引用到入口文件中的
<!--表格編輯--> <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" />
在頁面中定義表格,可添加自定義按鈕
<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>
3. js初始化表格
$(function(){ //初始化表格 $('#subprocessTable').bootstrapTable({ method: 'get', 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: 'index', //將index列設(shè)為唯一索引 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 ; //返回行號(hào) }}, {field:"actionName",title:"ActionName",align:"center",order:"asc",sortable:"true",formatter:function(value,row,index){ var strHtml ='<a href="./index.php?r=subprocess/modify&id='+ row.actionId +'">'+ row.actionName +'</a>'; 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 ='<a href="./index.php?r=subprocess/modify&id='+ row.actionId +'"><li class="glyphicon glyphicon-pencil"></li></a>'+ '<a href="javascript:void(0);" onclick="removeData('+ index +')" style="margin-left:5px;"><li class="glyphicon glyphicon-remove"></li></a>'; return strHtml; },edit:false}, {field:"actionId",title:"ActionID",align:"center",edit:false,visible:false,searchable:false} ] ] }); /** * add a new row */ $('#addData').click(function(){ $('#subprocessTable').bootstrapTable('selectPage', 1); //Jump to the first page var data = {actionId: '', actionName: '',category:'', description: ''}; //define a new row data,certainly it's empty $('#subprocessTable').bootstrapTable('prepend', data); //the method of prepend must defined all fields,but append needn't //$('#dataTable').bootstrapTable('append',data); $("#dataTable tr:eq(1) td:eq(0)").trigger("dblclick"); $("#dataTable input")[0].focus(); }); });
需要用下拉列表的,在定義列的時(shí)候這樣定義
{field:"toRun",title:"Run Flag",align:"center",edit:{ type:'select',//下拉框 url:'./index.php?r=dictionary/dictionaryInfo&type='+"run", //data:[{id:1,text:'hello'},{id:2,text:'hi'}], valueField:'id', textField:'text', editable : false, onSelect:function(val,rec){ //console.log(val,rec); } },sortable:true}
效果如下
其它的操作,大家可以到這個(gè)插件的網(wǎng)站上查閱文檔,或者看js源碼
三、動(dòng)態(tài)表頭
動(dòng)態(tài)表頭,說到底就是每次的列數(shù)據(jù)是不固定的,根據(jù)前提條件查詢數(shù)據(jù)庫,再根據(jù)查詢結(jié)果加載表頭。有了上邊的修改,實(shí)現(xiàn)這個(gè)功能已經(jīng)不在話下,只要把初始化表格的columns替換成我們自定義的數(shù)據(jù)就可以了,做了個(gè)簡單的小demo,具體的可以看【EasyUi DataGrid】動(dòng)態(tài)加載列這篇文章
$(function(){ var columnsAll = new Array(); //定義一個(gè)新的列集合,用來保存返回的數(shù)據(jù) //把列數(shù)據(jù)封裝到一個(gè)對(duì)象中 var col = {}; col["field"] = "index"; col["title"] = "ID"; col["align"] = 'center'; col["formatter"] = function(value, row, index){ return row.index=index ; //返回行號(hào) }; col["edit"] = false; columnsAll.push(col); //把這個(gè)對(duì)象添加到列集合中 var col2 = {}; col2["field"] = "scenarioId"; col2["title"] = "haha"; col2["align"] = 'center'; col2["edit"] = false; columnsAll.push(col2); //把這個(gè)對(duì)象添加到列集合中 //表格數(shù)據(jù) $('#detailTable').bootstrapTable({ method: 'get', url:"./index.php?r=session/sessionInfo", editable:true,//開啟編輯模式 clickToSelect: true, cache: false, uniqueId: 'index', //將index列設(shè)為唯一索引 striped: true, minimumCountColumns: 2, smartDisplay:true, columns: [ columnsAll ] }); });
效果如下:
以上所述是小編給大家介紹的BootStrap 可編輯表Table格,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- JS表格組件神器bootstrap table詳解(基礎(chǔ)版)
- Bootstrap Table使用方法詳解
- JS組件Bootstrap Table使用方法詳解
- BootStrap table表格插件自適應(yīng)固定表頭(超好用)
- bootstrap Table的一些小操作
- bootstrap table 服務(wù)器端分頁例子分享
- Bootstrap Table的使用總結(jié)
- Bootstrap Table從服務(wù)器加載數(shù)據(jù)進(jìn)行顯示的實(shí)現(xiàn)方法
- Bootstrap table分頁問題匯總
- bootstrap Table的使用方法總結(jié)
相關(guān)文章
使用Browserify配合jQuery進(jìn)行編程的超級(jí)指南
這篇文章主要介紹了使用Browserify配合jQuery進(jìn)行編程的超級(jí)指南,Browserify 可以讓你使用類似于node的require()的方式來組織瀏覽器端的JavaScript代碼,需要的朋友可以參考下2015-07-07jQuery Selectors(選擇器)的使用(二、層次篇)
本系列文章主要講述jQuery框架的選擇器(Selectors)使用方法,我將以實(shí)例方式進(jìn)行講述,以簡單,全面為基礎(chǔ),不會(huì)涉及很深,我的學(xué)習(xí)方法:先入門,后進(jìn)階!2009-12-12基于JQuery實(shí)現(xiàn)的Select級(jí)聯(lián)
Select級(jí)聯(lián),想必大并不陌生吧,本文為大家介紹下使用jquery是如何快速實(shí)現(xiàn)的,希望對(duì)大家有所幫助2014-01-01