btable.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /** BTable.js By Beginner*/
  2. layui.define(['element', 'common', 'paging', 'form'], function (exports) {
  3. "use strict";
  4. var $ = layui.jquery,
  5. layerTips = parent.layer === undefined ? layui.layer : parent.layer,
  6. layer = layui.layer,
  7. element = layui.element(),
  8. common = layui.common,
  9. paging = layui.paging(),
  10. form = layui.form();
  11. var bTable = function () {
  12. /**
  13. * 默认配置
  14. */
  15. this.config = {
  16. elem: undefined, //容器
  17. //data: undefined, //数据源
  18. columns:[],
  19. url: undefined, //数据源地址
  20. type: 'GET', //读取方式
  21. even: false, //是否开启偶数行背景
  22. skin: undefined, //风格样式 ,可选参数 line/row/nob
  23. field: 'id',//主键属性名
  24. paged: false, //是否显示分页组件
  25. singleSelect: false, //是否只能选择一行
  26. checkbox: true //显示多选
  27. };
  28. this.v = '1.0.0';
  29. };
  30. /**
  31. * 配置BTable
  32. * @param {Object} options
  33. */
  34. bTable.prototype.set = function (options) {
  35. var that = this;
  36. $.extend(true, that.config, options);
  37. return that;
  38. };
  39. /**
  40. * 渲染table
  41. */
  42. bTable.prototype.render = function () {
  43. var that = this;
  44. var _config = that.config;
  45. var columns = _config.columns;
  46. var th = '';
  47. for (var i = 0; i < columns.length; i++) {
  48. th += '<th>' + columns[i].fieldName + '</th>';
  49. }
  50. if (_config.checkbox && !_config.singleSelect) {
  51. th = '<th style="width:28px;"><input type="checkbox" lay-filter="allselector" lay-skin="primary" /></th>' + th;
  52. } else if (_config.checkbox) {
  53. th = '<th style="width:28px;">#</th>' + th;
  54. }
  55. var tpl = '<div class="btable">';
  56. if (_config.skin !== undefined && (_config.skin === 'line' || _config.skin === 'row' || _config.skin === 'nob') && _config.even) {
  57. tpl += '<table class="layui-table layui-form" lay-even lay-skin="' + _config.skin + '">';
  58. } else if (_config.skin !== undefined && (_config.skin === 'line' || _config.skin === 'row' || _config.skin === 'nob')) {
  59. tpl += '<table class="layui-table layui-form" lay-skin="' + _config.skin + '">';
  60. } else if (_config.even) {
  61. tpl += '<table class="layui-table layui-form" lay-even>';
  62. } else {
  63. tpl += '<table class="layui-table layui-form">';
  64. }
  65. tpl += '<thead><tr>' + th + '</tr></thead>';
  66. tpl += '<tbody class="btable-content"></tbody>';
  67. tpl += '</table>';
  68. if (_config.paged) {
  69. tpl += '<div data-type="paged" class="btable-paged"></div>';
  70. }
  71. tpl += '</div>';
  72. $(_config.elem).html(tpl);
  73. paging.init({
  74. url: _config.url, //地址
  75. elem: '.btable-content', //内容容器
  76. type: _config.type,
  77. tempType: 1,
  78. tempElem: getTpl({
  79. columns: _config.columns,
  80. checkbox: _config.checkbox,
  81. field: _config.field
  82. }), //模块容器
  83. paged: _config.paged,
  84. pageConfig: { //分页参数配置
  85. skip:true,
  86. elem: $(_config.elem).find('div[data-type=paged]'),//'#paged', //分页容器
  87. pageSize: _config.pageSize || 15 //分页大小
  88. },
  89. success: function () { //完成的回调
  90. //重新渲染复选框
  91. form.render('checkbox');
  92. form.on('checkbox(allselector)', function (data) {
  93. var elem = data.elem;
  94. $(_config.elem).find('tbody.btable-content').children('tr').each(function () {
  95. var $that = $(this);
  96. //全选或反选
  97. $that.children('td').eq(0).children('input[type=checkbox]')[0].checked = elem.checked;
  98. form.render('checkbox');
  99. });
  100. });
  101. //绑定选择行事件
  102. $(_config.elem).find('tbody.btable-content').children('tr').each(function (e) {
  103. //e.preventDefault();
  104. //e.stopPropagation();
  105. var $that = $(this);
  106. $that.on('click', function () {
  107. //只允许选择一行
  108. if (_config.singleSelect) {
  109. $that.siblings().each(function () {
  110. $(this).children('td').eq(0).children('input[type=checkbox]')[0].checked = false
  111. });
  112. $that.children('td').eq(0).children('input[type=checkbox]')[0].checked = true;
  113. } else {
  114. //获取当前的状态
  115. var currState = $that.children('td').eq(0).children('input[type=checkbox]')[0].checked;
  116. $that.children('td').eq(0).children('input[type=checkbox]')[0].checked = !currState;
  117. //当前已选择的总行数
  118. var cbxCount = 0;
  119. $that.parent('tbody').children('tr').each(function () {
  120. var $that = $(this);
  121. if ($that.children('td:first-child').children('input')[0].checked) {
  122. cbxCount++;
  123. }
  124. });
  125. $(_config.elem).find('thead').children('tr').children('th:first-child').children('input[type=checkbox]')[0].checked =
  126. $that.parent('tbody').children('tr').length === cbxCount;
  127. }
  128. form.render('checkbox');
  129. });
  130. });
  131. }
  132. });
  133. return that;
  134. };
  135. /**
  136. * 获取选择的行。
  137. */
  138. bTable.prototype.getSelected = function (callback) {
  139. var that = this;
  140. var _config = that.config;
  141. if (!_config.singleSelect)
  142. return callback({});
  143. var $tbody = $(_config.elem).find('tbody.btable-content');
  144. $tbody.children('tr').each(function () {
  145. var $that = $(this);
  146. var $input = $that.children('td:first-child').children('input')
  147. if ($input[0].checked) {
  148. callback({
  149. elem: $that,
  150. id: $input.data('id')
  151. });
  152. }
  153. });
  154. return that;
  155. };
  156. /**
  157. * 获取选择的所有行数据
  158. */
  159. bTable.prototype.getSelections = function (callback) {
  160. var that = this;
  161. var _config = that.config;
  162. var $tbody = $(_config.elem).find('tbody.btable-content');
  163. var dom = [];
  164. var ids = [];
  165. var index = 0;
  166. $tbody.children('tr').each(function () {
  167. var $that = $(this);
  168. var $input = $that.children('td:first-child').children('input')
  169. if ($input[0].checked) {
  170. dom[index] = $that;
  171. ids[index] = $input.data('id');
  172. index++;
  173. }
  174. });
  175. return callback({
  176. elem: dom,
  177. ids: ids,
  178. count: dom.length
  179. });
  180. };
  181. /**
  182. * 获取模板
  183. * @param {Object} options
  184. */
  185. function getTpl(options) {
  186. var columns = options.columns;
  187. var tpl = '{{# if(d.list.length>0 && d.list!=undefined){ }}';
  188. tpl += '{{# layui.each(d.list, function(index, item){ }}';
  189. var tds = '';
  190. for (var i = 0; i < columns.length; i++) {
  191. tds += '<td>{{ item.' + columns[i].field + ' }}</td>';
  192. }
  193. if (options.checkbox) {
  194. tds = '<td><input type="checkbox" data-id="{{ item.' + options.field + ' }}" lay-skin="primary" /></td>' + tds;
  195. } else {
  196. tds = '<td style="display:none;"><input type="hidden" data-id="{{ item.id }}" name="id" /></td>' + tds;
  197. }
  198. tpl += '<tr>' + tds + '</tr>'
  199. tpl += '{{# }); }}';
  200. tpl += '{{# }else{ }}';
  201. var colLength = options.checkbox && !options.singleSelect ? columns.length + 1 : columns.length;
  202. tpl += '<tr col="' + colLength + '">暂无数据.</tr>';
  203. tpl += '{{# } }}';
  204. return tpl;
  205. }
  206. var btable = new bTable();
  207. exports('btable', function (options) {
  208. return btable.set(options);
  209. });
  210. });