require-table.js 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table', 'bootstrap-table-lang', 'bootstrap-table-export', 'bootstrap-table-commonsearch', 'bootstrap-table-template', 'bootstrap-table-jumpto', 'bootstrap-table-fixed-columns'], function ($, undefined, Moment) {
  2. var Table = {
  3. list: {},
  4. // Bootstrap-table 基础配置
  5. defaults: {
  6. url: '',
  7. sidePagination: 'server',
  8. method: 'get', //请求方法
  9. toolbar: ".toolbar", //工具栏
  10. search: true, //是否启用快速搜索
  11. cache: false,
  12. commonSearch: true, //是否启用通用搜索
  13. searchFormVisible: false, //是否始终显示搜索表单
  14. titleForm: '', //为空则不显示标题,不定义默认显示:普通搜索
  15. idTable: 'commonTable',
  16. showExport: true,
  17. exportDataType: "auto",
  18. exportTypes: ['json', 'xml', 'csv', 'txt', 'doc', 'excel'],
  19. exportOptions: {
  20. fileName: 'export_' + Moment().format("YYYY-MM-DD"),
  21. preventInjection: false,
  22. mso: {
  23. onMsoNumberFormat: function (cell, row, col) {
  24. return !isNaN($(cell).text()) ? '\\@' : '';
  25. },
  26. },
  27. ignoreColumn: [0, 'operate'] //默认不导出第一列(checkbox)与操作(operate)列
  28. },
  29. pageSize: Config.pagesize || localStorage.getItem("pagesize") || 10,
  30. pageList: [10, 15, 20, 25, 50, 'All'],
  31. pagination: true,
  32. clickToSelect: true, //是否启用点击选中
  33. dblClickToEdit: true, //是否启用双击编辑
  34. singleSelect: false, //是否启用单选
  35. showRefresh: false,
  36. showJumpto: true,
  37. locale: Config.language == 'zh-cn' ? 'zh-CN' : 'en-US',
  38. showToggle: true,
  39. showColumns: true,
  40. pk: 'id',
  41. sortName: 'id',
  42. sortOrder: 'desc',
  43. paginationFirstText: __("First"),
  44. paginationPreText: __("Previous"),
  45. paginationNextText: __("Next"),
  46. paginationLastText: __("Last"),
  47. cardView: false, //卡片视图
  48. iosCardView: true, //ios卡片视图
  49. checkOnInit: true, //是否在初始化时判断
  50. escape: true, //是否对内容进行转义
  51. fixDropdownPosition: true, //是否修复下拉的定位
  52. dragCheckboxMultiselect: true, //拖拽时复选框是否多选模式
  53. selectedIds: [],
  54. selectedData: [],
  55. extend: {
  56. index_url: '',
  57. add_url: '',
  58. edit_url: '',
  59. del_url: '',
  60. import_url: '',
  61. multi_url: '',
  62. dragsort_url: 'ajax/weigh',
  63. }
  64. },
  65. // Bootstrap-table 列配置
  66. columnDefaults: {
  67. align: 'center',
  68. valign: 'middle',
  69. },
  70. config: {
  71. checkboxtd: 'tbody>tr>td.bs-checkbox',
  72. toolbar: '.toolbar',
  73. refreshbtn: '.btn-refresh',
  74. addbtn: '.btn-add',
  75. editbtn: '.btn-edit',
  76. delbtn: '.btn-del',
  77. importbtn: '.btn-import',
  78. multibtn: '.btn-multi',
  79. disabledbtn: '.btn-disabled',
  80. editonebtn: '.btn-editone',
  81. restoreonebtn: '.btn-restoreone',
  82. destroyonebtn: '.btn-destroyone',
  83. restoreallbtn: '.btn-restoreall',
  84. destroyallbtn: '.btn-destroyall',
  85. dragsortfield: 'weigh',
  86. },
  87. button: {
  88. edit: {
  89. name: 'edit',
  90. icon: 'fa fa-pencil',
  91. title: __('Edit'),
  92. extend: 'data-toggle="tooltip" data-container="body"',
  93. classname: 'btn btn-xs btn-success btn-editone'
  94. },
  95. del: {
  96. name: 'del',
  97. icon: 'fa fa-trash',
  98. title: __('Del'),
  99. extend: 'data-toggle="tooltip" data-container="body"',
  100. classname: 'btn btn-xs btn-danger btn-delone'
  101. },
  102. dragsort: {
  103. name: 'dragsort',
  104. icon: 'fa fa-arrows',
  105. title: __('Drag to sort'),
  106. extend: 'data-toggle="tooltip"',
  107. classname: 'btn btn-xs btn-primary btn-dragsort'
  108. }
  109. },
  110. api: {
  111. init: function (defaults, columnDefaults, locales) {
  112. defaults = defaults ? defaults : {};
  113. columnDefaults = columnDefaults ? columnDefaults : {};
  114. locales = locales ? locales : {};
  115. $.fn.bootstrapTable.Constructor.prototype.getSelectItem = function () {
  116. return this.$selectItem;
  117. };
  118. var _onPageListChange = $.fn.bootstrapTable.Constructor.prototype.onPageListChange;
  119. $.fn.bootstrapTable.Constructor.prototype.onPageListChange = function () {
  120. _onPageListChange.apply(this, Array.prototype.slice.apply(arguments));
  121. localStorage.setItem('pagesize', this.options.pageSize);
  122. return false;
  123. };
  124. // 写入bootstrap-table默认配置
  125. $.extend(true, $.fn.bootstrapTable.defaults, Table.defaults, defaults);
  126. // 写入bootstrap-table column配置
  127. $.extend($.fn.bootstrapTable.columnDefaults, Table.columnDefaults, columnDefaults);
  128. // 写入bootstrap-table locale配置
  129. $.extend($.fn.bootstrapTable.locales[Table.defaults.locale], {
  130. formatCommonSearch: function () {
  131. return __('Common search');
  132. },
  133. formatCommonSubmitButton: function () {
  134. return __('Submit');
  135. },
  136. formatCommonResetButton: function () {
  137. return __('Reset');
  138. },
  139. formatCommonCloseButton: function () {
  140. return __('Close');
  141. },
  142. formatCommonChoose: function () {
  143. return __('Choose');
  144. },
  145. formatJumpto: function () {
  146. return __('Go');
  147. }
  148. }, locales);
  149. // 如果是iOS设备则判断是否启用卡片视图
  150. if ($.fn.bootstrapTable.defaults.iosCardView && navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
  151. Table.defaults.cardView = true;
  152. $.fn.bootstrapTable.defaults.cardView = true;
  153. }
  154. if (typeof defaults.exportTypes != 'undefined') {
  155. $.fn.bootstrapTable.defaults.exportTypes = defaults.exportTypes;
  156. }
  157. },
  158. // 绑定事件
  159. bindevent: function (table) {
  160. //Bootstrap-table的父元素,包含table,toolbar,pagnation
  161. var parenttable = table.closest('.bootstrap-table');
  162. //Bootstrap-table配置
  163. var options = table.bootstrapTable('getOptions');
  164. //Bootstrap操作区
  165. var toolbar = $(options.toolbar, parenttable);
  166. //跨页提示按钮
  167. var tipsBtn = $(".btn-selected-tips", parenttable);
  168. if (tipsBtn.length === 0) {
  169. tipsBtn = $('<a href="javascript:" class="btn btn-warning-light btn-selected-tips hide" data-animation="false" data-toggle="tooltip" data-title="' + __("Click to uncheck all") + '"><i class="fa fa-info-circle"></i> ' + __("Multiple selection mode: %s checked", "<b>0</b>") + '</a>').appendTo(toolbar);
  170. }
  171. //点击提示按钮
  172. tipsBtn.off("click").on("click", function (e) {
  173. table.trigger("uncheckbox");
  174. table.bootstrapTable("refresh");
  175. });
  176. //当刷新表格时
  177. table.on('uncheckbox', function (status, res, e) {
  178. options.selectedIds = [];
  179. options.selectedData = [];
  180. tipsBtn.tooltip('hide');
  181. tipsBtn.addClass('hide');
  182. });
  183. //表格加载出错时
  184. table.on('load-error.bs.table', function (status, res, e) {
  185. if (e.status === 0) {
  186. return;
  187. }
  188. Toastr.error(__('Unknown data format'));
  189. });
  190. //当加载数据成功时
  191. table.on('load-success.bs.table', function (e, data) {
  192. if (typeof data.rows === 'undefined' && typeof data.code != 'undefined') {
  193. Toastr.error(data.msg);
  194. }
  195. });
  196. //当刷新表格时
  197. table.on('refresh.bs.table', function (e, settings, data) {
  198. $(Table.config.refreshbtn, toolbar).find(".fa").addClass("fa-spin");
  199. //移除指定浮动弹窗
  200. $(".layui-layer-autocontent").remove();
  201. });
  202. //当执行搜索时
  203. table.on('search.bs.table common-search.bs.table', function (e, settings, data) {
  204. table.trigger("uncheckbox");
  205. });
  206. if (options.dblClickToEdit) {
  207. //当双击单元格时
  208. table.on('dbl-click-row.bs.table', function (e, row, element, field) {
  209. $(Table.config.editonebtn, element).trigger("click");
  210. });
  211. }
  212. //渲染内容前
  213. table.on('pre-body.bs.table', function (e, data) {
  214. if (options.maintainSelected) {
  215. $.each(data, function (i, row) {
  216. row[options.stateField] = $.inArray(row[options.pk], options.selectedIds) > -1;
  217. });
  218. }
  219. });
  220. //当内容渲染完成后
  221. table.on('post-body.bs.table', function (e, data) {
  222. $(Table.config.refreshbtn, toolbar).find(".fa").removeClass("fa-spin");
  223. if ($(Table.config.checkboxtd + ":first", table).find("input[type='checkbox'][data-index]").length > 0) {
  224. //拖拽选择复选框
  225. var posx, posy, dragdiv, drag = false, prepare = false;
  226. var mousemove = function (e) {
  227. if (drag) {
  228. var left = Math.min(e.pageX, posx);
  229. var top = Math.min(e.pageY, posy);
  230. var width = Math.abs(posx - e.pageX);
  231. var height = Math.abs(posy - e.pageY);
  232. dragdiv.css({left: left + "px", top: top + "px", width: width + "px", height: height + "px"});
  233. var dragrect = {x: left, y: top, width: width, height: height};
  234. $(Table.config.checkboxtd, table).each(function () {
  235. var checkbox = $("input:checkbox", this);
  236. var tdrect = this.getBoundingClientRect();
  237. tdrect.x += document.documentElement.scrollLeft;
  238. tdrect.y += document.documentElement.scrollTop;
  239. var td_min_x = tdrect.x;
  240. var td_min_y = tdrect.y;
  241. var td_max_x = tdrect.x + tdrect.width;
  242. var td_max_y = tdrect.y + tdrect.height;
  243. var drag_min_x = dragrect.x;
  244. var drag_min_y = dragrect.y;
  245. var drag_max_x = dragrect.x + dragrect.width;
  246. var drag_max_y = dragrect.y + dragrect.height;
  247. var overlapped = td_min_x <= drag_max_x && td_max_x >= drag_min_x && td_min_y <= drag_max_y && td_max_y >= drag_min_y;
  248. if (overlapped) {
  249. if (!$(this).hasClass("overlaped")) {
  250. $(this).addClass("overlaped");
  251. checkbox.trigger("click");
  252. }
  253. } else {
  254. if ($(this).hasClass("overlaped")) {
  255. $(this).removeClass("overlaped");
  256. checkbox.trigger("click");
  257. }
  258. }
  259. });
  260. }
  261. };
  262. var selectstart = function () {
  263. return false;
  264. };
  265. var mouseup = function () {
  266. if (drag) {
  267. $(document).off("mousemove", mousemove);
  268. $(document).off("selectstart", selectstart);
  269. dragdiv.remove();
  270. }
  271. drag = false;
  272. prepare = false;
  273. $(document.body).css({'MozUserSelect': '', 'webkitUserSelect': ''}).attr('unselectable', 'off');
  274. };
  275. $(Table.config.checkboxtd, table).on("mousedown", function (e) {
  276. //禁止鼠标右键事件和文本框
  277. if (e.button === 2 || $(e.target).is("input")) {
  278. return false;
  279. }
  280. posx = e.pageX;
  281. posy = e.pageY;
  282. prepare = true;
  283. }).on("mousemove", function (e) {
  284. if (prepare && !drag) {
  285. drag = true;
  286. dragdiv = $("<div />");
  287. dragdiv.css({position: 'absolute', width: 0, height: 0, border: "1px dashed blue", background: "#0029ff", left: e.pageX + "px", top: e.pageY + "px", opacity: .1});
  288. dragdiv.appendTo(document.body);
  289. $(document.body).css({'MozUserSelect': 'none', 'webkitUserSelect': 'none'}).attr('unselectable', 'on');
  290. $(document).on("mousemove", mousemove).on("mouseup", mouseup).on("selectstart", selectstart);
  291. if (options.dragCheckboxMultiselect) {
  292. $(Table.config.checkboxtd, table).removeClass("overlaped");
  293. }
  294. }
  295. });
  296. }
  297. });
  298. var exportDataType = options.exportDataType;
  299. // 处理选中筛选框后按钮的状态统一变更
  300. table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table post-body.bs.table', function (e) {
  301. var allIds = [];
  302. $.each(table.bootstrapTable("getData"), function (i, item) {
  303. allIds.push(typeof item[options.pk] != 'undefined' ? item[options.pk] : '');
  304. });
  305. var selectedIds = Table.api.selectedids(table, true),
  306. selectedData = Table.api.selecteddata(table, true);
  307. //开启分页checkbox分页记忆
  308. if (options.maintainSelected) {
  309. options.selectedIds = options.selectedIds.filter(function (element, index, self) {
  310. return $.inArray(element, allIds) === -1;
  311. }).concat(selectedIds);
  312. options.selectedData = options.selectedData.filter(function (element, index, self) {
  313. return $.inArray(element[options.pk], allIds) === -1;
  314. }).concat(selectedData);
  315. if (options.selectedIds.length > selectedIds.length) {
  316. $("b", tipsBtn).text(options.selectedIds.length);
  317. tipsBtn.removeClass('hide');
  318. } else {
  319. tipsBtn.addClass('hide');
  320. }
  321. } else {
  322. options.selectedIds = selectedIds;
  323. options.selectedData = selectedData;
  324. }
  325. //如果导出类型为auto时则自动判断
  326. if (exportDataType === 'auto') {
  327. options.exportDataType = selectedIds.length > 0 ? 'selected' : 'all';
  328. }
  329. $(Table.config.disabledbtn, toolbar).toggleClass('disabled', !options.selectedIds.length);
  330. });
  331. // 提交通用搜索时判断是否和Tabs筛选一致
  332. table.on('common-search.bs.table', function (e, setting, query) {
  333. var tabs = $('.panel-heading [data-field]', table.closest(".panel-intro"));
  334. var field = tabs.data("field");
  335. var value = $("li.active > a", tabs).data("value");
  336. if (query.filter && typeof query.filter[field] !== 'undefined' && query.filter[field] != value) {
  337. $("li", tabs).removeClass("active");
  338. $("li > a[data-value='" + query.filter[field] + "']", tabs).parent().addClass("active");
  339. }
  340. });
  341. // 绑定TAB事件
  342. $('.panel-heading [data-field] a[data-toggle="tab"]', table.closest(".panel-intro")).on('shown.bs.tab', function (e) {
  343. var field = $(this).closest("[data-field]").data("field");
  344. var value = $(this).data("value");
  345. var object = $("[name='" + field + "']", table.closest(".bootstrap-table").find(".commonsearch-table"));
  346. if (object.prop('tagName') == "SELECT") {
  347. $("option[value='" + value + "']", object).prop("selected", true);
  348. } else {
  349. object.val(value);
  350. }
  351. table.trigger("uncheckbox");
  352. table.bootstrapTable('refresh', {pageNumber: 1});
  353. return false;
  354. });
  355. // 修复重置事件
  356. $("form", table.closest(".bootstrap-table").find(".commonsearch-table")).on('reset', function () {
  357. setTimeout(function () {
  358. // $('.panel-heading [data-field] li.active a[data-toggle="tab"]').trigger('shown.bs.tab');
  359. }, 0);
  360. $('.panel-heading [data-field] li', table.closest(".panel-intro")).removeClass('active');
  361. $('.panel-heading [data-field] li:first', table.closest(".panel-intro")).addClass('active');
  362. });
  363. // 刷新按钮事件
  364. toolbar.on('click', Table.config.refreshbtn, function () {
  365. table.bootstrapTable('refresh');
  366. });
  367. // 添加按钮事件
  368. toolbar.on('click', Table.config.addbtn, function () {
  369. var ids = Table.api.selectedids(table);
  370. var url = options.extend.add_url;
  371. if (url.indexOf("{ids}") !== -1) {
  372. url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
  373. }
  374. Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('Add'), $(this).data() || {});
  375. });
  376. // 导入按钮事件
  377. if ($(Table.config.importbtn, toolbar).length > 0) {
  378. require(['upload'], function (Upload) {
  379. Upload.api.upload($(Table.config.importbtn, toolbar), function (data, ret) {
  380. Fast.api.ajax({
  381. url: options.extend.import_url,
  382. data: {file: data.url},
  383. }, function (data, ret) {
  384. table.trigger("uncheckbox");
  385. table.bootstrapTable('refresh');
  386. });
  387. });
  388. });
  389. }
  390. // 批量编辑按钮事件
  391. toolbar.on('click', Table.config.editbtn, function () {
  392. var that = this;
  393. var ids = Table.api.selectedids(table);
  394. if (ids.length > 10) {
  395. return;
  396. }
  397. var title = $(that).data('title') || $(that).attr("title") || __('Edit');
  398. var data = $(that).data() || {};
  399. delete data.title;
  400. //循环弹出多个编辑框
  401. $.each(Table.api.selecteddata(table), function (index, row) {
  402. var url = options.extend.edit_url;
  403. row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
  404. url = Table.api.replaceurl(url, row, table);
  405. Fast.api.open(url, typeof title === 'function' ? title.call(table, row) : title, data);
  406. });
  407. });
  408. //清空回收站
  409. $(document).on('click', Table.config.destroyallbtn, function () {
  410. var that = this;
  411. Layer.confirm(__('Are you sure you want to truncate?'), function () {
  412. var url = $(that).data("url") ? $(that).data("url") : $(that).attr("href");
  413. Fast.api.ajax(url, function () {
  414. Layer.closeAll();
  415. table.trigger("uncheckbox");
  416. table.bootstrapTable('refresh');
  417. }, function () {
  418. Layer.closeAll();
  419. });
  420. });
  421. return false;
  422. });
  423. //全部还原
  424. $(document).on('click', Table.config.restoreallbtn, function () {
  425. var that = this;
  426. var url = $(that).data("url") ? $(that).data("url") : $(that).attr("href");
  427. Fast.api.ajax(url, function () {
  428. Layer.closeAll();
  429. table.trigger("uncheckbox");
  430. table.bootstrapTable('refresh');
  431. }, function () {
  432. Layer.closeAll();
  433. });
  434. return false;
  435. });
  436. //销毁或删除
  437. $(document).on('click', Table.config.restoreonebtn + ',' + Table.config.destroyonebtn, function () {
  438. var that = this;
  439. var url = $(that).data("url") ? $(that).data("url") : $(that).attr("href");
  440. var row = Fast.api.getrowbyindex(table, $(that).data("row-index"));
  441. Fast.api.ajax({
  442. url: url,
  443. data: {ids: row[options.pk]}
  444. }, function () {
  445. table.trigger("uncheckbox");
  446. table.bootstrapTable('refresh');
  447. });
  448. return false;
  449. });
  450. // 批量操作按钮事件
  451. toolbar.on('click', Table.config.multibtn, function () {
  452. var ids = Table.api.selectedids(table);
  453. Table.api.multi($(this).data("action"), ids, table, this);
  454. });
  455. // 批量删除按钮事件
  456. toolbar.on('click', Table.config.delbtn, function () {
  457. var that = this;
  458. var ids = Table.api.selectedids(table);
  459. Layer.confirm(
  460. __('Are you sure you want to delete the %s selected item?', ids.length),
  461. {icon: 3, title: __('Warning'), offset: 0, shadeClose: true, btn: [__('OK'), __('Cancel')]},
  462. function (index) {
  463. Table.api.multi("del", ids, table, that);
  464. Layer.close(index);
  465. }
  466. );
  467. });
  468. // 拖拽排序
  469. require(['dragsort'], function () {
  470. //绑定拖动排序
  471. $("tbody", table).dragsort({
  472. itemSelector: 'tr:visible',
  473. dragSelector: "a.btn-dragsort",
  474. dragEnd: function (a, b) {
  475. var element = $("a.btn-dragsort", this);
  476. var data = table.bootstrapTable('getData');
  477. var current = data[parseInt($(this).data("index"))];
  478. var options = table.bootstrapTable('getOptions');
  479. //改变的值和改变的ID集合
  480. var ids = $.map($("tbody tr:visible", table), function (tr) {
  481. return data[parseInt($(tr).data("index"))][options.pk];
  482. });
  483. var changeid = current[options.pk];
  484. var pid = typeof current.pid != 'undefined' ? current.pid : '';
  485. var params = {
  486. url: table.bootstrapTable('getOptions').extend.dragsort_url,
  487. data: {
  488. ids: ids.join(','),
  489. changeid: changeid,
  490. pid: pid,
  491. field: Table.config.dragsortfield,
  492. orderway: options.sortOrder,
  493. table: options.extend.table,
  494. pk: options.pk
  495. }
  496. };
  497. Fast.api.ajax(params, function (data, ret) {
  498. var success = $(element).data("success") || $.noop;
  499. if (typeof success === 'function') {
  500. if (false === success.call(element, data, ret)) {
  501. return false;
  502. }
  503. }
  504. table.bootstrapTable('refresh');
  505. }, function (data, ret) {
  506. var error = $(element).data("error") || $.noop;
  507. if (typeof error === 'function') {
  508. if (false === error.call(element, data, ret)) {
  509. return false;
  510. }
  511. }
  512. table.bootstrapTable('refresh');
  513. });
  514. },
  515. placeHolderTemplate: ""
  516. });
  517. });
  518. table.on("click", "input[data-id][name='checkbox']", function (e) {
  519. var ids = $(this).data("id");
  520. table.bootstrapTable($(this).prop("checked") ? 'checkBy' : 'uncheckBy', {field: options.pk, values: [ids]});
  521. });
  522. table.on("click", "[data-id].btn-change", function (e) {
  523. e.preventDefault();
  524. var changer = $.proxy(function () {
  525. Table.api.multi($(this).data("action") ? $(this).data("action") : '', [$(this).data("id")], table, this);
  526. }, this);
  527. if (typeof $(this).data("confirm") !== 'undefined') {
  528. Layer.confirm($(this).data("confirm"), function (index) {
  529. changer();
  530. Layer.close(index);
  531. });
  532. } else {
  533. changer();
  534. }
  535. });
  536. table.on("click", "[data-id].btn-edit", function (e) {
  537. e.preventDefault();
  538. var ids = $(this).data("id");
  539. var row = Table.api.getrowbyid(table, ids);
  540. row.ids = ids;
  541. var url = Table.api.replaceurl(options.extend.edit_url, row, table);
  542. Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('Edit'), $(this).data() || {});
  543. });
  544. table.on("click", "[data-id].btn-del", function (e) {
  545. e.preventDefault();
  546. var id = $(this).data("id");
  547. var that = this;
  548. Layer.confirm(
  549. __('Are you sure you want to delete this item?'),
  550. {icon: 3, title: __('Warning'), shadeClose: true, btn: [__('OK'), __('Cancel')]},
  551. function (index) {
  552. Table.api.multi("del", id, table, that);
  553. Layer.close(index);
  554. }
  555. );
  556. });
  557. table.on("mouseenter mouseleave", ".autocontent", function (e) {
  558. var target = $(".autocontent-item", this).get(0);
  559. if (!target) return;
  560. if (e.type === 'mouseenter') {
  561. if (target.scrollWidth > target.offsetWidth) {
  562. $(this).append("<div class='autocontent-caret'><i class='fa fa-chevron-down'></div>");
  563. }
  564. } else {
  565. $(".autocontent-caret", this).remove();
  566. }
  567. });
  568. table.on("click mouseenter", ".autocontent-caret", function (e) {
  569. var hover = $(this).prev().hasClass("autocontent-hover");
  570. if (!hover && e.type === 'mouseenter') {
  571. return;
  572. }
  573. var text = $(this).prev().text();
  574. var tdrect = $(this).parent().get(0).getBoundingClientRect();
  575. var index = Layer.open({id: 'autocontent', skin: 'layui-layer-fast layui-layer-autocontent', title: false, content: text, btn: false, anim: false, shade: 0, isOutAnim: false, area: 'auto', maxWidth: 450, maxHeight: 350, offset: [tdrect.y, tdrect.x]});
  576. if (hover) {
  577. $(document).one("mouseleave", "#layui-layer" + index, function () {
  578. Layer.close(index);
  579. });
  580. }
  581. var mousedown = function (e) {
  582. if ($(e.target).closest(".layui-layer").length === 0) {
  583. Layer.close(index);
  584. $(document).off("mousedown", mousedown);
  585. }
  586. };
  587. $(document).off("mousedown", mousedown).on("mousedown", mousedown);
  588. });
  589. //修复dropdown定位溢出的情况
  590. if (options.fixDropdownPosition) {
  591. var tableBody = table.closest(".fixed-table-body");
  592. table.on('show.bs.dropdown fa.event.refreshdropdown', ".btn-group", function (e) {
  593. var dropdownMenu = $(".dropdown-menu", this);
  594. var btnGroup = $(this);
  595. var isPullRight = dropdownMenu.hasClass("pull-right") || dropdownMenu.hasClass("dropdown-menu-right");
  596. var left, top, position;
  597. if (true || dropdownMenu.outerHeight() + btnGroup.outerHeight() > tableBody.outerHeight() - 41) {
  598. position = 'fixed';
  599. top = btnGroup.offset().top - $(window).scrollTop() + btnGroup.outerHeight();
  600. if ((top + dropdownMenu.outerHeight()) > $(window).height()) {
  601. top = btnGroup.offset().top - dropdownMenu.outerHeight() - 5;
  602. }
  603. left = isPullRight ? btnGroup.offset().left + btnGroup.outerWidth() - dropdownMenu.outerWidth() : btnGroup.offset().left;
  604. }
  605. if (left || top) {
  606. dropdownMenu.css({
  607. position: position, left: left, top: top, right: 'inherit'
  608. });
  609. }
  610. });
  611. var checkdropdown = function () {
  612. if ($(".btn-group.open", table).length > 0 && $(".btn-group.open .dropdown-menu", table).css("position") == 'fixed') {
  613. $(".btn-group.open", table).trigger("fa.event.refreshdropdown");
  614. }
  615. };
  616. $(window).on("scroll", function () {
  617. checkdropdown();
  618. });
  619. tableBody.on("scroll", function () {
  620. checkdropdown();
  621. });
  622. }
  623. var id = table.attr("id");
  624. Table.list[id] = table;
  625. return table;
  626. },
  627. // 批量操作请求
  628. multi: function (action, ids, table, element) {
  629. var options = table.bootstrapTable('getOptions');
  630. var data = element ? $(element).data() : {};
  631. ids = ($.isArray(ids) ? ids.join(",") : ids);
  632. var url = typeof data.url !== "undefined" ? data.url : (action == "del" ? options.extend.del_url : options.extend.multi_url);
  633. var params = typeof data.params !== "undefined" ? (typeof data.params == 'object' ? $.param(data.params) : data.params) : '';
  634. options = {url: url, data: {action: action, ids: ids, params: params}};
  635. Fast.api.ajax(options, function (data, ret) {
  636. table.trigger("uncheckbox");
  637. var success = $(element).data("success") || $.noop;
  638. if (typeof success === 'function') {
  639. if (false === success.call(element, data, ret)) {
  640. return false;
  641. }
  642. }
  643. table.bootstrapTable('refresh');
  644. }, function (data, ret) {
  645. var error = $(element).data("error") || $.noop;
  646. if (typeof error === 'function') {
  647. if (false === error.call(element, data, ret)) {
  648. return false;
  649. }
  650. }
  651. });
  652. },
  653. // 单元格元素事件
  654. events: {
  655. operate: {
  656. 'click .btn-editone': function (e, value, row, index) {
  657. e.stopPropagation();
  658. e.preventDefault();
  659. var table = $(this).closest('table');
  660. var options = table.bootstrapTable('getOptions');
  661. var ids = row[options.pk];
  662. row = $.extend({}, row ? row : {}, {ids: ids});
  663. var url = options.extend.edit_url;
  664. Fast.api.open(Table.api.replaceurl(url, row, table), $(this).data("original-title") || $(this).attr("title") || __('Edit'), $(this).data() || {});
  665. },
  666. 'click .btn-delone': function (e, value, row, index) {
  667. e.stopPropagation();
  668. e.preventDefault();
  669. var that = this;
  670. var top = $(that).offset().top - $(window).scrollTop();
  671. var left = $(that).offset().left - $(window).scrollLeft() - 260;
  672. if (top + 154 > $(window).height()) {
  673. top = top - 154;
  674. }
  675. if ($(window).width() < 480) {
  676. top = left = undefined;
  677. }
  678. Layer.confirm(
  679. __('Are you sure you want to delete this item?'),
  680. {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true, btn: [__('OK'), __('Cancel')]},
  681. function (index) {
  682. var table = $(that).closest('table');
  683. var options = table.bootstrapTable('getOptions');
  684. Table.api.multi("del", row[options.pk], table, that);
  685. Layer.close(index);
  686. }
  687. );
  688. }
  689. },//单元格图片预览
  690. image: {
  691. 'click .img-center': function (e, value, row, index) {
  692. var data = [];
  693. value = value === null ? '' : value.toString();
  694. var arr = value != '' ? value.split(",") : [];
  695. var url;
  696. $.each(arr, function (index, value) {
  697. url = Fast.api.cdnurl(value);
  698. data.push({
  699. src: url,
  700. thumb: url.match(/^(\/|data:image\\)/) ? url : url + Config.upload.thumbstyle
  701. });
  702. });
  703. Layer.photos({
  704. photos: {
  705. "start": $(this).parent().index(),
  706. "data": data
  707. },
  708. anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
  709. });
  710. },
  711. }
  712. },
  713. // 单元格数据格式化
  714. formatter: {
  715. icon: function (value, row, index) {
  716. value = value === null ? '' : value.toString();
  717. value = value.indexOf(" ") > -1 ? value : "fa fa-" + value;
  718. //渲染fontawesome图标
  719. return '<i class="' + value + '"></i> ' + value;
  720. },
  721. image: function (value, row, index) {
  722. value = value == null || value.length === 0 ? '' : value.toString();
  723. value = value ? value : '/assets/img/blank.gif';
  724. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  725. var url = Fast.api.cdnurl(value, true);
  726. url = url.match(/^(\/|data:image\\)/) ? url : url + Config.upload.thumbstyle;
  727. return '<a href="javascript:"><img class="' + classname + '" src="' + url + '" /></a>';
  728. },
  729. images: function (value, row, index) {
  730. value = value == null || value.length === 0 ? '' : value.toString();
  731. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  732. var arr = value != '' ? value.split(',') : [];
  733. var html = [];
  734. var url;
  735. $.each(arr, function (i, value) {
  736. value = value ? value : '/assets/img/blank.gif';
  737. url = Fast.api.cdnurl(value, true);
  738. url = url.match(/^(\/|data:image\\)/) ? url : url + Config.upload.thumbstyle;
  739. html.push('<a href="javascript:"><img class="' + classname + '" src="' + url + '" /></a>');
  740. });
  741. return html.join(' ');
  742. },
  743. file: function (value, row, index) {
  744. value = value == null || value.length === 0 ? '' : value.toString();
  745. value = Fast.api.cdnurl(value, true);
  746. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  747. var suffix = /[\.]?([a-zA-Z0-9]+)$/.exec(value);
  748. suffix = suffix ? suffix[1] : 'file';
  749. var url = Fast.api.fixurl("ajax/icon?suffix=" + suffix);
  750. return '<a href="' + value + '" target="_blank"><img src="' + url + '" class="' + classname + '"></a>';
  751. },
  752. files: function (value, row, index) {
  753. value = value == null || value.length === 0 ? '' : value.toString();
  754. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  755. var arr = value != '' ? value.split(',') : [];
  756. var html = [];
  757. var suffix, url;
  758. $.each(arr, function (i, value) {
  759. value = Fast.api.cdnurl(value, true);
  760. suffix = /[\.]?([a-zA-Z0-9]+)$/.exec(value);
  761. suffix = suffix ? suffix[1] : 'file';
  762. url = Fast.api.fixurl("ajax/icon?suffix=" + suffix);
  763. html.push('<a href="' + value + '" target="_blank"><img src="' + url + '" class="' + classname + '"></a>');
  764. });
  765. return html.join(' ');
  766. },
  767. content: function (value, row, index) {
  768. var width = this.width != undefined ? (this.width.toString().match(/^\d+$/) ? this.width + "px" : this.width) : "250px";
  769. var hover = this.hover != undefined && this.hover ? "autocontent-hover" : "";
  770. return "<div class='autocontent-item " + hover + "' style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + ";'>" + value + "</div>";
  771. },
  772. status: function (value, row, index) {
  773. var custom = {normal: 'success', hidden: 'gray', deleted: 'danger', locked: 'info'};
  774. if (typeof this.custom !== 'undefined') {
  775. custom = $.extend(custom, this.custom);
  776. }
  777. this.custom = custom;
  778. this.icon = 'fa fa-circle';
  779. return Table.api.formatter.normal.call(this, value, row, index);
  780. },
  781. normal: function (value, row, index) {
  782. var colorArr = ["primary", "success", "danger", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
  783. var custom = {};
  784. if (typeof this.custom !== 'undefined') {
  785. custom = $.extend(custom, this.custom);
  786. }
  787. value = value == null || value.length === 0 ? '' : value.toString();
  788. var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
  789. var index = keys.indexOf(value);
  790. var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
  791. var display = index > -1 ? this.searchList[value] : null;
  792. var icon = typeof this.icon !== 'undefined' ? this.icon : null;
  793. if (!color) {
  794. color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
  795. }
  796. if (!display) {
  797. display = __(value.charAt(0).toUpperCase() + value.slice(1));
  798. }
  799. var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
  800. if (this.operate != false) {
  801. html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
  802. }
  803. return html;
  804. },
  805. toggle: function (value, row, index) {
  806. var table = this.table;
  807. var options = table ? table.bootstrapTable('getOptions') : {};
  808. var pk = options.pk || "id";
  809. var color = typeof this.color !== 'undefined' ? this.color : 'success';
  810. var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
  811. var no = typeof this.no !== 'undefined' ? this.no : 0;
  812. var url = typeof this.url !== 'undefined' ? this.url : '';
  813. var confirm = '';
  814. var disable = false;
  815. if (typeof this.confirm !== "undefined") {
  816. confirm = typeof this.confirm === "function" ? this.confirm.call(this, value, row, index) : this.confirm;
  817. }
  818. if (typeof this.disable !== "undefined") {
  819. disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable;
  820. }
  821. return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change " + (disable ? 'btn disabled no-padding' : '') + "' data-index='" + index + "' data-id='"
  822. + row[pk] + "' " + (url ? "data-url='" + url + "'" : "") + (confirm ? "data-confirm='" + confirm + "'" : "") + " data-params='" + this.field + "=" + (value == yes ? no : yes) + "'><i class='fa fa-toggle-on text-success text-" + color + " " + (value == yes ? '' : 'fa-flip-horizontal text-gray') + " fa-2x'></i></a>";
  823. },
  824. url: function (value, row, index) {
  825. value = value == null || value.length === 0 ? '' : value.toString();
  826. return '<div class="input-group input-group-sm" style="width:250px;margin:0 auto;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
  827. },
  828. search: function (value, row, index) {
  829. var field = this.field;
  830. if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
  831. value = row[this.customField];
  832. field = this.customField;
  833. }
  834. return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', value) + '" data-field="' + field + '" data-value="' + value + '">' + value + '</a>';
  835. },
  836. addtabs: function (value, row, index) {
  837. var url = Table.api.replaceurl(this.url || '', row, this.table);
  838. var title = this.atitle ? this.atitle : __("Search %s", value);
  839. return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  840. },
  841. dialog: function (value, row, index) {
  842. var url = Table.api.replaceurl(this.url || '', row, this.table);
  843. var title = this.atitle ? this.atitle : __("View %s", value);
  844. return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  845. },
  846. flag: function (value, row, index) {
  847. var that = this;
  848. value = value == null || value.length === 0 ? '' : value.toString();
  849. var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
  850. //如果字段列有定义custom
  851. if (typeof this.custom !== 'undefined') {
  852. colorArr = $.extend(colorArr, this.custom);
  853. }
  854. var field = this.field;
  855. if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
  856. value = row[this.customField];
  857. field = this.customField;
  858. }
  859. if (typeof that.searchList === 'object' && typeof that.custom === 'undefined') {
  860. var i = 0;
  861. var searchValues = Object.values(colorArr);
  862. $.each(that.searchList, function (key, val) {
  863. if (typeof colorArr[key] == 'undefined') {
  864. colorArr[key] = searchValues[i];
  865. i = typeof searchValues[i + 1] === 'undefined' ? 0 : i + 1;
  866. }
  867. });
  868. }
  869. //渲染Flag
  870. var html = [];
  871. var arr = value != '' ? value.split(',') : [];
  872. var color, display, label;
  873. $.each(arr, function (i, value) {
  874. value = value == null || value.length === 0 ? '' : value.toString();
  875. if (value == '')
  876. return true;
  877. color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
  878. display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
  879. label = '<span class="label label-' + color + '">' + display + '</span>';
  880. if (that.operate) {
  881. html.push('<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + field + '" data-value="' + value + '">' + label + '</a>');
  882. } else {
  883. html.push(label);
  884. }
  885. });
  886. return html.join(' ');
  887. },
  888. label: function (value, row, index) {
  889. return Table.api.formatter.flag.call(this, value, row, index);
  890. },
  891. datetime: function (value, row, index) {
  892. var datetimeFormat = typeof this.datetimeFormat === 'undefined' ? 'YYYY-MM-DD HH:mm:ss' : this.datetimeFormat;
  893. if (isNaN(value)) {
  894. return value ? Moment(value).format(datetimeFormat) : __('None');
  895. } else {
  896. return value ? Moment(parseInt(value) * 1000).format(datetimeFormat) : __('None');
  897. }
  898. },
  899. operate: function (value, row, index) {
  900. var table = this.table;
  901. // 操作配置
  902. var options = table ? table.bootstrapTable('getOptions') : {};
  903. // 默认按钮组
  904. var buttons = $.extend([], this.buttons || []);
  905. // 所有按钮名称
  906. var names = [];
  907. buttons.forEach(function (item) {
  908. names.push(item.name);
  909. });
  910. if (options.extend.dragsort_url !== '' && names.indexOf('dragsort') === -1) {
  911. buttons.push(Table.button.dragsort);
  912. }
  913. if (options.extend.edit_url !== '' && names.indexOf('edit') === -1) {
  914. Table.button.edit.url = options.extend.edit_url;
  915. buttons.push(Table.button.edit);
  916. }
  917. if (options.extend.del_url !== '' && names.indexOf('del') === -1) {
  918. buttons.push(Table.button.del);
  919. }
  920. return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
  921. }
  922. ,
  923. buttons: function (value, row, index) {
  924. // 默认按钮组
  925. var buttons = $.extend([], this.buttons || []);
  926. return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
  927. }
  928. },
  929. buttonlink: function (column, buttons, value, row, index, type) {
  930. var table = column.table;
  931. column.clickToSelect = false;
  932. type = typeof type === 'undefined' ? 'buttons' : type;
  933. var options = table ? table.bootstrapTable('getOptions') : {};
  934. var html = [];
  935. var hidden, visible, disable, url, classname, icon, text, title, refresh, confirm, extend,
  936. dropdown, link;
  937. var fieldIndex = column.fieldIndex;
  938. var dropdowns = {};
  939. $.each(buttons, function (i, j) {
  940. if (type === 'operate') {
  941. if (j.name === 'dragsort' && typeof row[Table.config.dragsortfield] === 'undefined') {
  942. return true;
  943. }
  944. if (['add', 'edit', 'del', 'multi', 'dragsort'].indexOf(j.name) > -1 && !options.extend[j.name + "_url"]) {
  945. return true;
  946. }
  947. }
  948. var attr = table.data(type + "-" + j.name);
  949. if (typeof attr === 'undefined' || attr) {
  950. hidden = typeof j.hidden === 'function' ? j.hidden.call(table, row, j) : (typeof j.hidden !== 'undefined' ? j.hidden : false);
  951. if (hidden) {
  952. return true;
  953. }
  954. visible = typeof j.visible === 'function' ? j.visible.call(table, row, j) : (typeof j.visible !== 'undefined' ? j.visible : true);
  955. if (!visible) {
  956. return true;
  957. }
  958. dropdown = j.dropdown ? j.dropdown : '';
  959. url = j.url ? j.url : '';
  960. url = typeof url === 'function' ? url.call(table, row, j) : (url ? Fast.api.fixurl(Table.api.replaceurl(url, row, table)) : 'javascript:;');
  961. classname = j.classname ? j.classname : (dropdown ? 'btn-' + name + 'one' : 'btn-primary btn-' + name + 'one');
  962. icon = j.icon ? j.icon : '';
  963. text = typeof j.text === 'function' ? j.text.call(table, row, j) : j.text ? j.text : '';
  964. title = typeof j.title === 'function' ? j.title.call(table, row, j) : j.title ? j.title : text;
  965. refresh = j.refresh ? 'data-refresh="' + j.refresh + '"' : '';
  966. confirm = typeof j.confirm === 'function' ? j.confirm.call(table, row, j) : (typeof j.confirm !== 'undefined' ? j.confirm : false);
  967. confirm = confirm ? 'data-confirm="' + confirm + '"' : '';
  968. extend = typeof j.extend === 'function' ? j.extend.call(table, row, j) : (typeof j.extend !== 'undefined' ? j.extend : '');
  969. disable = typeof j.disable === 'function' ? j.disable.call(table, row, j) : (typeof j.disable !== 'undefined' ? j.disable : false);
  970. if (disable) {
  971. classname = classname + ' disabled';
  972. }
  973. link = '<a href="' + url + '" class="' + classname + '" ' + (confirm ? confirm + ' ' : '') + (refresh ? refresh + ' ' : '') + extend + ' title="' + title + '" data-table-id="' + (table ? table.attr("id") : '') + '" data-field-index="' + fieldIndex + '" data-row-index="' + index + '" data-button-index="' + i + '"><i class="' + icon + '"></i>' + (text ? ' ' + text : '') + '</a>';
  974. if (dropdown) {
  975. if (typeof dropdowns[dropdown] == 'undefined') {
  976. dropdowns[dropdown] = [];
  977. }
  978. dropdowns[dropdown].push(link);
  979. } else {
  980. html.push(link);
  981. }
  982. }
  983. });
  984. if (!$.isEmptyObject(dropdowns)) {
  985. var dropdownHtml = [];
  986. $.each(dropdowns, function (i, j) {
  987. dropdownHtml.push('<div class="btn-group"><button type="button" class="btn btn-primary dropdown-toggle btn-xs" data-toggle="dropdown">' + i + '</button><button type="button" class="btn btn-primary dropdown-toggle btn-xs" data-toggle="dropdown"><span class="caret"></span></button><ul class="dropdown-menu dropdown-menu-right"><li>' + j.join('</li><li>') + '</li></ul></div>');
  988. });
  989. html.unshift(dropdownHtml);
  990. }
  991. return html.join(' ');
  992. },
  993. //替换URL中的数据
  994. replaceurl: function (url, row, table) {
  995. var options = table ? table.bootstrapTable('getOptions') : null;
  996. var ids = options ? row[options.pk] : 0;
  997. row.ids = ids ? ids : (typeof row.ids !== 'undefined' ? row.ids : 0);
  998. url = url == null || url.length === 0 ? '' : url.toString();
  999. //自动添加ids参数
  1000. url = !url.match(/(?=([?&]ids=)|(\/ids\/)|(\{ids}))/i) ?
  1001. url + (url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + '{ids}' : url;
  1002. url = url.replace(/\{(.*?)\}/gi, function (matched) {
  1003. matched = matched.substring(1, matched.length - 1);
  1004. if (matched.indexOf(".") !== -1) {
  1005. var temp = row;
  1006. var arr = matched.split(/\./);
  1007. for (var i = 0; i < arr.length; i++) {
  1008. if (typeof temp[arr[i]] !== 'undefined') {
  1009. temp = temp[arr[i]];
  1010. }
  1011. }
  1012. return typeof temp === 'object' ? '' : temp;
  1013. }
  1014. return row[matched];
  1015. });
  1016. return url;
  1017. },
  1018. // 获取选中的条目ID集合
  1019. selectedids: function (table, current) {
  1020. var options = table.bootstrapTable('getOptions');
  1021. //如果有设置翻页记忆模式
  1022. if (!current && options.maintainSelected) {
  1023. return options.selectedIds;
  1024. }
  1025. return $.map(table.bootstrapTable('getSelections'), function (row) {
  1026. return row[options.pk];
  1027. });
  1028. },
  1029. //获取选中的数据
  1030. selecteddata: function (table, current) {
  1031. var options = table.bootstrapTable('getOptions');
  1032. //如果有设置翻页记忆模式
  1033. if (!current && options.maintainSelected) {
  1034. return options.selectedData;
  1035. }
  1036. return table.bootstrapTable('getSelections');
  1037. },
  1038. // 切换复选框状态
  1039. toggleattr: function (table) {
  1040. $("input[type='checkbox']", table).trigger('click');
  1041. },
  1042. // 根据行索引获取行数据
  1043. getrowdata: function (table, index) {
  1044. index = parseInt(index);
  1045. var data = table.bootstrapTable('getData');
  1046. return typeof data[index] !== 'undefined' ? data[index] : null;
  1047. },
  1048. // 根据行索引获取行数据
  1049. getrowbyindex: function (table, index) {
  1050. return Table.api.getrowdata(table, index);
  1051. },
  1052. // 根据主键ID获取行数据
  1053. getrowbyid: function (table, id) {
  1054. var row = {};
  1055. var options = table.bootstrapTable("getOptions");
  1056. $.each(Table.api.selecteddata(table), function (i, j) {
  1057. if (j[options.pk] == id) {
  1058. row = j;
  1059. return false;
  1060. }
  1061. });
  1062. return row;
  1063. }
  1064. },
  1065. };
  1066. return Table;
  1067. });