新的一年悄然到来,生活依旧。最近一周大热的赵雷风,一首《成都》,一首《理想》再次把民谣展现在国人面前。歌词着实写的不错。css
理想,你今年几岁html
你老是诱惑着年轻的朋友前端
你老是谢了又开 给我惊喜jquery
又让我沉入失望的生活里git
。。。。github
我已不是无悔的那个青年bootstrap
青春被时光抛弃后端
已经是当父亲的年纪浏览器
理想永远都年轻缓存
你让我倔强的反抗着命运
你让我变得苍白
却依然天真的相信
花儿会再次的怒放
等等,这和咱们今天的主题貌似没半毛钱关系。好了,再扯就扯远了,今天和你们分享的是 Bootstrap Table 点我查看效果
或者直接看简版效果以下(一个简单的后台管理,实现了增删查改):
既然说是bootstrap table 那确定是bootstrap实现的(这是句废话)。
关于bottstrap相关文档以下:
Bootstrap中文网:http://www.bootcss.com/
Bootstrap Table Demo:http://issues.wenzhixin.net.cn/bootstrap-table/index.html
Bootstrap Table API:http://bootstrap-table.wenzhixin.net.cn/zh-cn/documentation/
Bootstrap Table源码:https://github.com/wenzhixin/bootstrap-table
Bootstrap DataPicker:http://www.bootcss.com/p/bootstrap-datetimepicker/
Boostrap Table 扩展API:http://bootstrap-table.wenzhixin.net.cn/extensions/
关于引入有两种方式,一种是直接下载源码,而后放到项目中,bottstrap包能够到http://v3.bootcss.com/ 中找到。
第二种方法就是使用打开Nuget,搜索这两个包
而Bootstrap Table的版本居然是0.4,这也太坑爹了。因此博主建议Bootstrap Table的包就直接在源码里面去下载吧。Bootstrap Table最新的版本好像是1.9.0。
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>BootStrap Table使用</title> @*一、Jquery组件引用*@ <script src="~/Scripts/jquery-1.10.2.js"></script> @*二、bootstrap组件引用*@ <script src="~/Content/bootstrap/bootstrap.js"></script> <link href="~/Content/bootstrap/bootstrap.css" rel="stylesheet" /> @*三、bootstrap table组件以及中文包的引用*@ <script src="~/Content/bootstrap-table/bootstrap-table.js"></script> <link href="~/Content/bootstrap-table/bootstrap-table.css" rel="stylesheet" /> <script src="~/Content/bootstrap-table/locale/bootstrap-table-zh-CN.js"></script> @*四、页面Js文件的引用*@ <script src="~/Scripts/Home/Index.js"></script> </head> <body> <div class="panel-body" style="padding-bottom:0px;"> <div class="panel panel-default"> <div class="panel-heading">查询条件</div> <div class="panel-body"> <form id="formSearch" class="form-horizontal"> <div class="form-group" style="margin-top:15px"> <label class="control-label col-sm-1" for="txt_search_departmentname">部门名称</label> <div class="col-sm-3"> <input type="text" class="form-control" id="txt_search_departmentname"> </div> <label class="control-label col-sm-1" for="txt_search_statu">状态</label> <div class="col-sm-3"> <input type="text" class="form-control" id="txt_search_statu"> </div> <div class="col-sm-4" style="text-align:left;"> <button type="button" style="margin-left:50px" id="btn_query" class="btn btn-primary">查询</button> </div> </div> </form> </div> </div> <div id="toolbar" class="btn-group"> <button id="btn_add" type="button" class="btn btn-default"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增 </button> <button id="btn_edit" type="button" class="btn btn-default"> <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改 </button> <button id="btn_delete" type="button" class="btn btn-default"> <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除 </button> </div> <table id="tb_departments"></table> </div> </body> </html>
引入须要的文件以后,咱们最重要的就是定义一个空的table,如上的 <table id="tb_departments"></table> 。固然Bootstrap table还提供了一种简介的用法,直接在table标签里面定义相似“data-...”等相关属性,就不用再js里面注册了,但博主以为这种用法虽然简单,但不太灵活,遇到父子表等这些高级用法的时候就不太好处理了,因此我们仍是统一使用在js里面初始化的方式来使用table组件。
$(function () { //1.初始化Table var oTable = new TableInit(); oTable.Init(); //2.初始化Button的点击事件 var oButtonInit = new ButtonInit(); oButtonInit.Init(); }); var TableInit = function () { var oTableInit = new Object(); //初始化Table oTableInit.Init = function () { $('#tb_departments').bootstrapTable({ url: '/Home/GetDepartment', //请求后台的URL(*) method: 'get', //请求方式(*) toolbar: '#toolbar', //工具按钮用哪一个容器 striped: true, //是否显示行间隔色 cache: false, //是否使用缓存,默认为true,因此通常状况下须要设置一下这个属性(*) pagination: true, //是否显示分页(*) sortable: false, //是否启用排序 sortOrder: "asc", //排序方式 queryParams: oTableInit.queryParams,//传递参数(*) sidePagination: "server", //分页方式:client客户端分页,server服务端分页(*) pageNumber:1, //初始化加载第一页,默认第一页 pageSize: 10, //每页的记录行数(*) pageList: [10, 25, 50, 100], //可供选择的每页的行数(*) search: true, //是否显示表格搜索,此搜索是客户端搜索,不会进服务端,因此,我的感受意义不大 strictSearch: true, showColumns: true, //是否显示全部的列 showRefresh: true, //是否显示刷新按钮 minimumCountColumns: 2, //最少容许的列数 clickToSelect: true, //是否启用点击选中行 height: 500, //行高,若是没有设置height属性,表格自动根据记录条数以为表格高度 uniqueId: "ID", //每一行的惟一标识,通常为主键列 showToggle:true, //是否显示详细视图和列表视图的切换按钮 cardView: false, //是否显示详细视图 detailView: false, //是否显示父子表
columns: [{
checkbox: true
}
, {
field: 'MethodName',
title: '方法名'
}, {
field: 'OperType',
title: '日志类型'
}, {
field: 'LogFormat',
title: '内容格式'
}, {
field: 'ParamterValues',
title: '格式参数'
}, {
field: 'LogSql',
title: '数值'
}, {
field: 'SQLParamter',
title: '数值定义'
}, {
field: 'CreateDate',
title: '建立时间'
}
]
}); }; //获得查询的参数 oTableInit.queryParams = function (params) { var temp = { //这里的键的名字和控制器的变量名必须一直,这边改动,控制器也须要改为同样的 limit: params.limit, //页面大小 offset: params.offset, //页码 departmentname: $("#txt_search_departmentname").val(), statu: $("#txt_search_statu").val() }; return temp; }; return oTableInit; }; var ButtonInit = function () { var oInit = new Object(); var postdata = {}; oInit.Init = function () { //初始化页面上面的按钮事件 }; return oInit; };
表格的初始化也很简单,定义相关的参数便可。上面一些博主以为重要的参数都加了注释,而且初始化Table必须的几个参数博主也用(*)作了标记,若是你的表格也有太多的页面需求,直接用必须的参数就能解决。一样,在columns参数里面其实也有不少的参数须要设置,好比列的排序,对齐,宽度等等。这些博主以为比较简单,不会涉及表格的功能,看看API就能搞定。
注:若是须要隐藏某列,只须要便可
$('#tb_departments').bootstrapTable('hideColumn', '列名');
显示某列
$('#tableOrderRealItems').bootstrapTable('showColumn', '列名');
public JsonResult GetDepartment(int limit, int offset, string departmentname, string statu) { var lstRes = new List<Department>(); for (var i = 0; i < 50; i++) { var oModel = new Department(); oModel.ID = Guid.NewGuid().ToString(); oModel.Name = "销售部" + i ; oModel.Level = i.ToString(); oModel.Desc = "暂无描述信息"; lstRes.Add(oModel); } var total = lstRes.Count; var rows = lstRes.Skip(offset).Take(limit).ToList(); return Json(new { total = total, rows = rows }, JsonRequestBehavior.AllowGet); }
这里有一点须要注意:若是是服务端分页,返回的结果必须包含total、rows两个参数。漏写或错写都会致使表格没法显示数据。相反,若是是客户端分页,这里要返回一个集合对象到前端。
效果及说明
第一个问题上面说过,若是在js里面初始化的参数 sidePagination: "server" 设置为在服务端分页,那么咱们的返回值必须告诉前端总记录的条数和当前页的记录数,而后前端才知道如何分页。而且最重要的一点,这两个参数的名字必须为total和rows。最开始博主也不知道这个,写成了total和row,结果是请求能够进到后台的GetDepartment方法,返回值total和row也都有值,但是前端就是显示以下:
Content文件夹中存放文件以下所示:
oTableInit.queryParams = function (params) { var temp = { //这里的键的名字和控制器的变量名必须一直,这边改动,控制器也须要改为同样的 limit: params.limit, //页面大小 offset: params.offset, //页码 departmentname: $("#txt_search_departmentname").val(), statu: $("#txt_search_statu").val(), search:params.search }; return temp; };
而后后端(后端能够根据这个参数值是否有值来判断作什么查询)
public JsonResult GetDepartment(int limit, int offset, string departmentname, string statu, string search) { var lstRes = new List<Department>(); for (var i = 0; i < 50; i++) { var oModel = new Department(); oModel.ID = Guid.NewGuid().ToString(); oModel.Name = "销售部" + i ; oModel.Level = i.ToString(); oModel.Desc = "暂无描述信息"; lstRes.Add(oModel); } var total = lstRes.Count; var rows = lstRes.Skip(offset).Take(limit).ToList(); return Json(new { total = total, rows = rows }, JsonRequestBehavior.AllowGet); }
第四点:关于Bootstrap Table的排序,因为通常这种BS系统确定会采用服务端分页,咱们若是仅仅在js里面设置sortable和sortOrder等属性信息,表格是不会有效排序的。缘由很简单,服务端分页的方式,排序本页数据意义不大。因此,通常的排序须要将排序方式和排序字段发送到后台,在后台排序比较合适。好比咱们这里能够再参数里面增长两个:
oTableInit.queryParams = function (params) { var temp = { //这里的键的名字和控制器的变量名必须一直,这边改动,控制器也须要改为同样的 limit: params.limit, //页面大小 offset: params.offset, //页码 order: params.order, ordername: params.sort, departmentname: $("#txt_search_departmentname").val(), statu: $("#txt_search_statu").val() }; return temp; };
百度网盘:点我下载 密码:knct
git地址:https://github.com/XiaoYong666/bootstrap-table
补充:搭配一种后台风格效果更佳,以下图所示:
而后提示框各类提示框推荐使用layer,效果以下:
后台布局是使用的bootstrap ,下面也放出布局Demo,有须要的小伙伴能够参考下:
连接:点我下载 密码:w7g0
关于上图中的提示框,是使用的layer弹出组件,使用方法很是简单,官方下载出来获得以下所示文件,须要将整个文件都拷贝到项目目录下, 而后只须要引用layer.js便可(须要引入jquery1.8及以上版本才支持layer)
更多关于layer的请各位小伙伴移步layer大本营:我要飞过去
本文原创地址:http://www.cnblogs.com/landeanfen/p/4976838.html