基于BootstrapTable的简单应用

系统管理员 2021-09-12 07:52 550阅读 0赞

Bootstrap Table基于Bootstrap的jQuery表格插件,通过简单的设置,就可以拥有强大的单选、多选、排序、分页,以及编辑、导出、过滤(扩展)等等的功能。

本文将以一个基于BootstrapTable控件的图书列表查询功能为实例(如图1)。注意本实例使用了ASP.NET MVC。

Center

图1

页面代码:

  1. @{
  2. Layout = null;
  3. ViewBag.Title = "基于BootstrapTable的简单应用";
  4. }
  5. <!--添加相关样式引用-->
  6. <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
  7. <link href="~/Content/bootstrap-table.min.css" rel="stylesheet" />
  8. <div class="container body-content" style="padding-top:20px;">
  9. <div class="panel panel-default">
  10. <div class="panel-heading">查询条件</div>
  11. <div class="panel-body">
  12. <form class="form-inline">
  13. <div class="row">
  14. <div class="col-sm-4">
  15. <label class="control-label">图书名称:</label>
  16. <input id="txtTitle" type="text" class="form-control">
  17. </div>
  18. <div class="col-sm-4">
  19. <label class="control-label">图书作者:</label>
  20. <input id="txtAuthor" type="text" class="form-control">
  21. </div>
  22. <div class="col-sm-4">
  23. <label class="control-label">出版社:</label>
  24. <input id="txtPublish" type="text" class="form-control">
  25. </div>
  26. </div>
  27. <div class="row text-right" style="margin-top:20px;">
  28. <div class="col-sm-12">
  29. <input class="btn btn-primary" type="button" value="查询" onclick="SearchData()">
  30. <input class="btn btn-default" type="button" value="批量删除" onclick="BtchDeleteBook()">
  31. </div>
  32. </div>
  33. </form>
  34. </div>
  35. </div>
  36. <table id="table"></table>
  37. </div>
  38. <!--添加相关脚本引用-->
  39. <script src="~/Scripts/jquery-1.10.2.min.js"></script>
  40. <script src="~/Scripts/bootstrap.min.js"></script>
  41. <script src="~/Scripts/bootstrap-table.min.js"></script>
  42. <script src="~/Scripts/bootstrap-table-zh-CN.min.js"></script>
  43. <script type="text/javascript">
  44. $(document).ready(function () {
  45. $('#table').bootstrapTable({
  46. url: '@Url.Action("SearchBookInfo", "Home")',
  47. queryParamsType: '', //默认值为 'limit' ,在默认情况下 传给服务端的参数为:offset,limit,sort
  48. queryParams: queryParams,
  49. method: "post",
  50. pagination: true,
  51. pageNumber: 1,
  52. pageSize: 2,
  53. pageList: [10, 20, 50, 100],
  54. sidePagination: "server", //分页方式:client客户端分页,server服务端分页(*)
  55. striped: true, //是否显示行间隔色
  56. cache: false,
  57. uniqueId: "BookId", //每一行的唯一标识,一般为主键列
  58. height:300,
  59. paginationPreText: "上一页",
  60. paginationNextText: "下一页",
  61. columns: [
  62. { checkbox: true },
  63. { title: '序号', width: 50, align: "center", formatter: function (value, row, index) { return index + 1; } },
  64. { title: '图书名称', field: 'Title' },
  65. { title: '图书作者', field: 'Author' },
  66. { title: '销售价格', field: 'Price' },
  67. { title: '出版社', field: 'Publish' },
  68. {
  69. title: '出版时间', field: 'PublishDate', formatter: function (value, row, index) {
  70. if (value == null)
  71. return "";
  72. else {
  73. var pa = /.*\((.*)\)/;
  74. var unixtime = value.match(pa)[1].substring(0, 10);
  75. return getShortTime(unixtime);
  76. }
  77. }
  78. },
  79. {
  80. title: '操作', field: 'BookId', formatter: function (value, row, index) {
  81. var html = '<a href="javascript:EditBook('+ value + ')">编辑</a>';
  82. html += ' <a href="javascript:DeleteBook(' + value + ')">删除</a>';
  83. return html;
  84. }
  85. }
  86. ]
  87. });
  88. });
  89. //查询条件
  90. function queryParams(params) {
  91. return {
  92. pageSize: params.pageSize,
  93. pageIndex: params.pageNumber,
  94. Title: $.trim($("#txtTitle").val()),
  95. Author: $.trim($("#txtAuthor").val()),
  96. Publish: $.trim($("#txtPublish").val()),
  97. };
  98. }
  99. //查询事件
  100. function SearchData() {
  101. $('#table').bootstrapTable('refresh', { pageNumber: 1 });
  102. }
  103. //编辑操作
  104. function EditBook(bookId){
  105. alert("编辑操作,图书ID:" + bookId);
  106. }
  107. //删除操作
  108. function DeleteBook(bookId) {
  109. if (confirm("确定删除图书ID:" + bookId + "吗?"))
  110. {
  111. alert("执行删除操作");
  112. }
  113. }
  114. //批量删除
  115. function BtchDeleteBook(){
  116. var opts = $('#table').bootstrapTable('getSelections');
  117. if (opts == "") {
  118. alert("请选择要删除的数据");
  119. }
  120. else {
  121. var idArray = [];
  122. for (var i = 0; i < opts.length; i++) {
  123. idArray.push(opts[i].BookId);
  124. }
  125. if (confirm("确定删除图书ID:" + idArray + "吗?")) {
  126. alert("执行删除操作");
  127. }
  128. }
  129. }
  130. function getTime(/** timestamp=0 **/) {
  131. var ts = arguments[0] || 0;
  132. var t, y, m, d, h, i, s;
  133. t = ts ? new Date(ts * 1000) : new Date();
  134. y = t.getFullYear();
  135. m = t.getMonth() + 1;
  136. d = t.getDate();
  137. h = t.getHours();
  138. i = t.getMinutes();
  139. s = t.getSeconds();
  140. // 可根据需要在这里定义时间格式
  141. return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d) + ' ' + (h < 10 ? '0' + h : h) + ':' + (i < 10 ? '0' + i : i) + ':' + (s < 10 ? '0' + s : s);
  142. }
  143. function getShortTime(/** timestamp=0 **/) {
  144. var ts = arguments[0] || 0;
  145. var t, y, m, d, h, i, s;
  146. t = ts ? new Date(ts * 1000) : new Date();
  147. y = t.getFullYear();
  148. m = t.getMonth() + 1;
  149. d = t.getDate();
  150. // 可根据需要在这里定义时间格式
  151. return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
  152. }
  153. </script>

控制器代码:

  1. /// <summary>
  2. /// 查询图书信息
  3. /// </summary>
  4. /// <param name="param"></param>
  5. /// <returns></returns>
  6. public JsonResult SearchBookInfo(BookInfo param, int pageSize, int pageIndex)
  7. {
  8. //获取图书列表
  9. List<BookInfo> bookList = GetBookList();
  10. //根据条件筛选数据
  11. if (!String.IsNullOrEmpty(param.Title))
  12. {
  13. bookList = bookList.Where(a => a.Title.Contains(param.Title)).ToList();
  14. }
  15. if (!String.IsNullOrEmpty(param.Author))
  16. {
  17. bookList = bookList.Where(a => a.Author.Contains(param.Author)).ToList();
  18. }
  19. if (!String.IsNullOrEmpty(param.Publish))
  20. {
  21. bookList = bookList.Where(a => a.Publish.Contains(param.Publish)).ToList();
  22. }
  23. //BootstrapTable的返回结果
  24. BootstrapTableResult<BookInfo> result = new BootstrapTableResult<BookInfo>();
  25. result.total = bookList.Count;
  26. result.rows = bookList;
  27. return Json(result);
  28. }
  29. /// <summary>
  30. /// 获取图书列表
  31. /// </summary>
  32. /// <returns></returns>
  33. public List<BookInfo> GetBookList()
  34. {
  35. List<BookInfo> bookList = new List<BookInfo>();
  36. BookInfo book1 = new BookInfo()
  37. {
  38. BookId = 8,
  39. Title = "ASP.NET MVC 5高级编程",
  40. Author = "加洛韦",
  41. Publish = "清华大学出版社",
  42. PublishDate = new DateTime(2017, 08, 15),
  43. Price = 29.99
  44. };
  45. bookList.Add(book1);
  46. BookInfo book2 = new BookInfo()
  47. {
  48. BookId = 9,
  49. Title = "Java从入门到精通",
  50. Author = "明日科技",
  51. Publish = "清华大学出版社",
  52. PublishDate = new DateTime(2015, 09, 28),
  53. Price = 38.55
  54. };
  55. bookList.Add(book2);
  56. BookInfo book3 = new BookInfo()
  57. {
  58. BookId = 10,
  59. Title = "Oracle从入门到精通",
  60. Author = "秦靖",
  61. Publish = "机械工业出版社",
  62. PublishDate = new DateTime(2014, 10, 08),
  63. Price = 38.55
  64. };
  65. bookList.Add(book3);
  66. return bookList;
  67. }

其他代码:

  1. /// <summary>
  2. /// 图书信息实体类
  3. /// </summary>
  4. public class BookInfo
  5. {
  6. public int BookId { set; get; } //图书ID
  7. public string Title { set; get; } //图书名称
  8. public string Author { set; get; } //图书作者
  9. public string Publish { set; get; } //出版社
  10. public DateTime PublishDate { set; get; } //出版时间
  11. public Double Price { set; get; } //销售价格
  12. }
  13. /// <summary>
  14. /// BootstrapTable返回结果类
  15. /// </summary>
  16. public class BootstrapTableResult<T>
  17. {
  18. public int total { get; set; } //总记录数
  19. public List<T> rows { get; set; } //数据列表
  20. }

Bootstrap Table资料网站:http://bootstrap-table.wenzhixin.net.cn/zh-cn/

发表评论

表情:
评论列表 (有 0 条评论,550人围观)

还没有评论,来说两句吧...

相关阅读