bootstrap-table-object.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * 初始化 BootStrap Table 的封装
  3. *
  4. * 约定:toolbar的id为 (bstableId + "Toolbar")
  5. *
  6. * @author fengshuonan
  7. */
  8. (function () {
  9. var BSTable = function (bstableId, url, columns) {
  10. this.btInstance = null; //jquery和BootStrapTable绑定的对象
  11. this.bstableId = bstableId;
  12. this.url = Feng.ctxPath + url;
  13. this.method = "post";
  14. this.paginationType = "server"; //默认分页方式是服务器分页,可选项"client"
  15. this.toolbarId = bstableId + "Toolbar";
  16. this.columns = columns;
  17. // this.height = 665; //默认表格高度665
  18. this.height = 600;
  19. this.data = {};
  20. this.singleSelect = true;
  21. this.queryParams = {}; // 向后台传递的自定义参数
  22. this.onLoadSuccess = function(){};
  23. this.onDblClickRow = function () {};
  24. this.headerStyle = function(){};
  25. this.detailView = false;
  26. this.rowStyle=function () {
  27. return {
  28. }
  29. };
  30. };
  31. BSTable.prototype = {
  32. /**
  33. * 初始化bootstrap table
  34. */
  35. init: function () {
  36. var tableId = this.bstableId;
  37. var me = this;
  38. this.btInstance =
  39. $('#' + tableId).bootstrapTable({
  40. contentType: "application/x-www-form-urlencoded",
  41. url: this.url, //请求地址
  42. method: this.method, //ajax方式,post还是get
  43. ajaxOptions: { //ajax请求的附带参数
  44. data: this.data
  45. },
  46. toolbar: "#" + this.toolbarId,//顶部工具条
  47. striped: true, //是否显示行间隔色
  48. cache: false, //是否使用缓存,默认为true
  49. pagination: true, //是否显示分页(*)
  50. sortable: true, //是否启用排序
  51. sortOrder: "desc", //排序方式
  52. pageNumber: 1, //初始化加载第一页,默认第一页
  53. pageSize: 10, //每页的记录行数(*)
  54. pageList: [10,20,50,100], //可供选择的每页的行数(*)
  55. showJumpto:true, //分页跳转
  56. queryParamsType: 'limit', //默认值为 'limit' ,在默认情况下 传给服务端的参数为:offset,limit,sort
  57. queryParams: function (param) {
  58. return $.extend(me.queryParams, param);
  59. }, // 向后台传递的自定义参数
  60. sidePagination: this.paginationType, //分页方式:client客户端分页,server服务端分页(*)
  61. search: false, //是否显示表格搜索,此搜索是客户端搜索,不会进服务端
  62. strictSearch: true, //设置为 true启用 全匹配搜索,否则为模糊搜索
  63. showColumns: true, //是否显示所有的列
  64. showRefresh: true, //是否显示刷新按钮
  65. singleSelect:this.singleSelect,
  66. minimumCountColumns: 2, //最少允许的列数
  67. clickToSelect: true, //是否启用点击选中行
  68. searchOnEnterKey: true, //设置为 true时,按回车触发搜索方法,否则自动触发搜索方法
  69. columns: this.columns, //列数组
  70. height: this.height,
  71. detailView:this.detailView,
  72. icons: {
  73. refresh: 'glyphicon-repeat',
  74. toggle: 'glyphicon-list-alt',
  75. columns: 'glyphicon-list'
  76. },
  77. iconSize: 'sm',
  78. onLoadSuccess : this.onLoadSuccess,
  79. onDblClickRow : this.onDblClickRow,
  80. rowStyle:this.rowStyle,
  81. headerStyle:this.headerStyle,
  82. classes: "table table-bordered table-hover table-sm",
  83. onPostBody: function(){
  84. $("td.uitd_showTip").bind("mouseover", function(){
  85. var htm = $(this).html();
  86. if (htm!=null && htm.length>1){
  87. $(this).webuiPopover({content:htm,trigger:'hover'}).webuiPopover('show');
  88. }
  89. });
  90. }
  91. });
  92. return this;
  93. },
  94. /**headerStyle
  95. * 向后台传递的自定义参数
  96. * @param param
  97. */
  98. setQueryParams: function (param) {
  99. this.queryParams = param;
  100. },
  101. /**
  102. * 设置分页方式:server 或者 client
  103. */
  104. setPaginationType: function (type) {
  105. this.paginationType = type;
  106. },
  107. setOnLoadSuccess: function (fun) {
  108. this.onLoadSuccess = fun;
  109. },
  110. setOnDblClickRow :function(fun){
  111. this.onDblClickRow = fun;
  112. },
  113. setDetailView : function(b){
  114. this.detailView = b;
  115. },
  116. setRowStyle:function(fun){
  117. this.rowStyle=fun;
  118. },
  119. setHeight : function(h){
  120. this.height = h;
  121. },
  122. setSingleSelect:function(b){
  123. this.singleSelect = b;
  124. },
  125. /**
  126. * 设置ajax post请求时候附带的参数
  127. */
  128. set: function (key, value) {
  129. if (typeof key == "object") {
  130. for (var i in key) {
  131. if (typeof i == "function")
  132. continue;
  133. this.data[i] = key[i];
  134. }
  135. } else {
  136. this.data[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
  137. }
  138. return this;
  139. },
  140. /**
  141. * 设置ajax post请求时候附带的参数
  142. */
  143. setData: function (data) {
  144. this.data = data;
  145. return this;
  146. },
  147. /**
  148. * 清空ajax post请求参数
  149. */
  150. clear: function () {
  151. this.data = {};
  152. return this;
  153. },
  154. setHeaderStyle:function(style){
  155. this.headerStyle = style;
  156. },
  157. /**
  158. * 刷新 bootstrap 表格
  159. * Refresh the remote server data,
  160. * you can set {silent: true} to refresh the data silently,
  161. * and set {url: newUrl} to change the url.
  162. * To supply query params specific to this request, set {query: {foo: 'bar'}}
  163. */
  164. refresh: function (parms) {
  165. if (typeof parms != "undefined") {
  166. this.btInstance.bootstrapTable('refresh', parms);
  167. } else {
  168. this.btInstance.bootstrapTable('refresh');
  169. }
  170. }
  171. };
  172. window.BSTable = BSTable;
  173. }());