require-table.js 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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", //支持auto,selected,all 当设定为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 __('搜索');
  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. if ($(".export .exporttips").length === 0) {
  329. $(".export .dropdown-menu").prepend("<li class='exporttips alert alert-warning-light mb-0 no-border p-2'></li>")
  330. }
  331. $(".export .exporttips").html("导出记录:" + (selectedIds.length > 0 ? "选中" : "全部"));
  332. }
  333. $(Table.config.disabledbtn, toolbar).toggleClass('disabled', !options.selectedIds.length);
  334. });
  335. // 提交通用搜索时判断是否和Tabs筛选一致
  336. table.on('common-search.bs.table', function (e, setting, query) {
  337. var tabs = $('.panel-heading [data-field]', table.closest(".panel-intro"));
  338. var field = tabs.data("field");
  339. var value = $("li.active > a", tabs).data("value");
  340. if (query.filter && typeof query.filter[field] !== 'undefined' && query.filter[field] != value) {
  341. $("li", tabs).removeClass("active");
  342. $("li > a[data-value='" + query.filter[field] + "']", tabs).parent().addClass("active");
  343. }
  344. });
  345. // 绑定TAB事件
  346. $('.panel-heading [data-field] a[data-toggle="tab"]', table.closest(".panel-intro")).on('shown.bs.tab', function (e) {
  347. var field = $(this).closest("[data-field]").data("field");
  348. var value = $(this).data("value");
  349. var object = $("[name='" + field + "']", table.closest(".bootstrap-table").find(".commonsearch-table"));
  350. if (object.prop('tagName') === "SELECT") {
  351. $("option[value='" + value + "']", object).prop("selected", true);
  352. } else {
  353. object.val(value);
  354. }
  355. table.trigger("uncheckbox");
  356. table.bootstrapTable('getOptions').totalRows = 0;
  357. table.bootstrapTable('refresh', {pageNumber: 1});
  358. return false;
  359. });
  360. // 修复重置事件
  361. $("form", table.closest(".bootstrap-table").find(".commonsearch-table")).on('reset', function () {
  362. setTimeout(function () {
  363. // $('.panel-heading [data-field] li.active a[data-toggle="tab"]').trigger('shown.bs.tab');
  364. }, 0);
  365. $('.panel-heading [data-field] li', table.closest(".panel-intro")).removeClass('active');
  366. $('.panel-heading [data-field] li:first', table.closest(".panel-intro")).addClass('active');
  367. });
  368. // 刷新按钮事件
  369. toolbar.on('click', Table.config.refreshbtn, function () {
  370. table.bootstrapTable('refresh');
  371. });
  372. // 添加按钮事件
  373. toolbar.on('click', Table.config.addbtn, function () {
  374. var ids = Table.api.selectedids(table);
  375. var url = options.extend.add_url;
  376. if (url.indexOf("{ids}") !== -1) {
  377. url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
  378. }
  379. Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('Add'), $(this).data() || {});
  380. });
  381. // 导入按钮事件
  382. if ($(Table.config.importbtn, toolbar).length > 0) {
  383. require(['upload'], function (Upload) {
  384. Upload.api.upload($(Table.config.importbtn, toolbar), function (data, ret) {
  385. Fast.api.ajax({
  386. url: options.extend.import_url,
  387. data: {file: data.url},
  388. }, function (data, ret) {
  389. table.trigger("uncheckbox");
  390. table.bootstrapTable('refresh');
  391. });
  392. });
  393. });
  394. }
  395. // 批量编辑按钮事件
  396. toolbar.on('click', Table.config.editbtn, function () {
  397. var that = this;
  398. var ids = Table.api.selectedids(table);
  399. if (ids.length > 10) {
  400. return;
  401. }
  402. var title = $(that).data('title') || $(that).attr("title") || __('Edit');
  403. var data = $(that).data() || {};
  404. delete data.title;
  405. //循环弹出多个编辑框
  406. $.each(Table.api.selecteddata(table), function (index, row) {
  407. var url = options.extend.edit_url;
  408. row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
  409. url = Table.api.replaceurl(url, row, table);
  410. Fast.api.open(url, typeof title === 'function' ? title.call(table, row) : title, data);
  411. });
  412. });
  413. //清空回收站
  414. $(document).on('click', Table.config.destroyallbtn, function () {
  415. var that = this;
  416. Layer.confirm(__('Are you sure you want to truncate?'), function () {
  417. var url = $(that).data("url") ? $(that).data("url") : $(that).attr("href");
  418. Fast.api.ajax(url, function () {
  419. Layer.closeAll();
  420. table.trigger("uncheckbox");
  421. table.bootstrapTable('refresh');
  422. }, function () {
  423. Layer.closeAll();
  424. });
  425. });
  426. return false;
  427. });
  428. //全部还原
  429. $(document).on('click', Table.config.restoreallbtn, function () {
  430. var that = this;
  431. var url = $(that).data("url") ? $(that).data("url") : $(that).attr("href");
  432. Fast.api.ajax(url, function () {
  433. Layer.closeAll();
  434. table.trigger("uncheckbox");
  435. table.bootstrapTable('refresh');
  436. }, function () {
  437. Layer.closeAll();
  438. });
  439. return false;
  440. });
  441. //销毁或删除
  442. $(document).on('click', Table.config.restoreonebtn + ',' + Table.config.destroyonebtn, function () {
  443. var that = this;
  444. var url = $(that).data("url") ? $(that).data("url") : $(that).attr("href");
  445. var row = Table.api.getrowbyindex(table, $(that).data("row-index"));
  446. Fast.api.ajax({
  447. url: url,
  448. data: {ids: row[options.pk]}
  449. }, function () {
  450. table.trigger("uncheckbox");
  451. table.bootstrapTable('refresh');
  452. });
  453. return false;
  454. });
  455. // 批量操作按钮事件
  456. toolbar.on('click', Table.config.multibtn, function () {
  457. var ids = Table.api.selectedids(table);
  458. Table.api.multi($(this).data("action"), ids, table, this);
  459. });
  460. // 批量删除按钮事件
  461. toolbar.on('click', Table.config.delbtn, function () {
  462. var that = this;
  463. var ids = Table.api.selectedids(table);
  464. Layer.confirm(
  465. __('Are you sure you want to delete the %s selected item?', ids.length),
  466. {icon: 3, title: __('Warning'), offset: 0, shadeClose: true, btn: [__('OK'), __('Cancel')]},
  467. function (index) {
  468. Table.api.multi("del", ids, table, that);
  469. Layer.close(index);
  470. }
  471. );
  472. });
  473. // 拖拽排序
  474. require(['dragsort'], function () {
  475. //绑定拖动排序
  476. $("tbody", table).dragsort({
  477. itemSelector: 'tr:visible',
  478. dragSelector: "a.btn-dragsort",
  479. dragEnd: function (a, b) {
  480. var element = $("a.btn-dragsort", this);
  481. var data = table.bootstrapTable('getData');
  482. var current = data[parseInt($(this).data("index"))];
  483. var options = table.bootstrapTable('getOptions');
  484. //改变的值和改变的ID集合
  485. var ids = $.map($("tbody tr:visible", table), function (tr) {
  486. return data[parseInt($(tr).data("index"))][options.pk];
  487. });
  488. var changeid = current[options.pk];
  489. var pid = typeof current.pid != 'undefined' ? current.pid : '';
  490. var params = {
  491. url: table.bootstrapTable('getOptions').extend.dragsort_url,
  492. data: {
  493. ids: ids.join(','),
  494. changeid: changeid,
  495. pid: pid,
  496. field: Table.config.dragsortfield,
  497. orderway: options.sortOrder,
  498. table: options.extend.table,
  499. pk: options.pk
  500. }
  501. };
  502. Fast.api.ajax(params, function (data, ret) {
  503. var success = $(element).data("success") || $.noop;
  504. if (typeof success === 'function') {
  505. if (false === success.call(element, data, ret)) {
  506. return false;
  507. }
  508. }
  509. table.bootstrapTable('refresh');
  510. }, function (data, ret) {
  511. var error = $(element).data("error") || $.noop;
  512. if (typeof error === 'function') {
  513. if (false === error.call(element, data, ret)) {
  514. return false;
  515. }
  516. }
  517. table.bootstrapTable('refresh');
  518. });
  519. },
  520. placeHolderTemplate: ""
  521. });
  522. });
  523. table.on("click", "input[data-id][name='checkbox']", function (e) {
  524. var ids = $(this).data("id");
  525. table.bootstrapTable($(this).prop("checked") ? 'checkBy' : 'uncheckBy', {field: options.pk, values: [ids]});
  526. });
  527. table.on("click", "[data-id].btn-change", function (e) {
  528. e.preventDefault();
  529. var changer = $.proxy(function () {
  530. Table.api.multi($(this).data("action") ? $(this).data("action") : '', [$(this).data("id")], table, this);
  531. }, this);
  532. if (typeof $(this).data("confirm") !== 'undefined') {
  533. Layer.confirm($(this).data("confirm"), function (index) {
  534. changer();
  535. Layer.close(index);
  536. });
  537. } else {
  538. changer();
  539. }
  540. });
  541. table.on("click", "[data-id].btn-edit", function (e) {
  542. e.preventDefault();
  543. var ids = $(this).data("id");
  544. var row = Table.api.getrowbyid(table, ids);
  545. row.ids = ids;
  546. var url = Table.api.replaceurl(options.extend.edit_url, row, table);
  547. Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('Edit'), $(this).data() || {});
  548. });
  549. table.on("click", "[data-id].btn-del", function (e) {
  550. e.preventDefault();
  551. var id = $(this).data("id");
  552. var that = this;
  553. Layer.confirm(
  554. __('Are you sure you want to delete this item?'),
  555. {icon: 3, title: __('Warning'), shadeClose: true, btn: [__('OK'), __('Cancel')]},
  556. function (index) {
  557. Table.api.multi("del", id, table, that);
  558. Layer.close(index);
  559. }
  560. );
  561. });
  562. table.on("mouseenter mouseleave", ".autocontent", function (e) {
  563. var target = $(".autocontent-item", this).get(0);
  564. if (!target) return;
  565. if (e.type === 'mouseenter') {
  566. if (target.scrollWidth > target.offsetWidth && $(".autocontent-caret", this).length === 0) {
  567. $(this).append("<div class='autocontent-caret'><i class='fa fa-chevron-down'></div>");
  568. }
  569. } else {
  570. $(".autocontent-caret", this).remove();
  571. }
  572. });
  573. table.on("click mouseenter", ".autocontent-caret", function (e) {
  574. var hover = $(this).prev().hasClass("autocontent-hover");
  575. if (!hover && e.type === 'mouseenter') {
  576. return;
  577. }
  578. var text = $(this).prev().text();
  579. var tdrect = $(this).parent().get(0).getBoundingClientRect();
  580. 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]});
  581. if (hover) {
  582. $(document).one("mouseleave", "#layui-layer" + index, function () {
  583. Layer.close(index);
  584. });
  585. }
  586. var mousedown = function (e) {
  587. if ($(e.target).closest(".layui-layer").length === 0) {
  588. Layer.close(index);
  589. $(document).off("mousedown", mousedown);
  590. }
  591. };
  592. $(document).off("mousedown", mousedown).on("mousedown", mousedown);
  593. });
  594. //修复dropdown定位溢出的情况
  595. if (options.fixDropdownPosition) {
  596. var tableBody = table.closest(".fixed-table-body");
  597. table.on('show.bs.dropdown fa.event.refreshdropdown', ".btn-group", function (e) {
  598. var dropdownMenu = $(".dropdown-menu", this);
  599. var btnGroup = $(this);
  600. var isPullRight = dropdownMenu.hasClass("pull-right") || dropdownMenu.hasClass("dropdown-menu-right");
  601. var left, top, position;
  602. if (true || dropdownMenu.outerHeight() + btnGroup.outerHeight() > tableBody.outerHeight() - 41) {
  603. position = 'fixed';
  604. top = btnGroup.offset().top - $(window).scrollTop() + btnGroup.outerHeight();
  605. if ((top + dropdownMenu.outerHeight()) > $(window).height()) {
  606. top = btnGroup.offset().top - dropdownMenu.outerHeight() - 5;
  607. }
  608. left = isPullRight ? btnGroup.offset().left + btnGroup.outerWidth() - dropdownMenu.outerWidth() : btnGroup.offset().left;
  609. }
  610. if (left || top) {
  611. dropdownMenu.css({
  612. position: position, left: left, top: top, right: 'inherit'
  613. });
  614. }
  615. });
  616. var checkdropdown = function () {
  617. if ($(".btn-group.open", table).length > 0 && $(".btn-group.open .dropdown-menu", table).css("position") == 'fixed') {
  618. $(".btn-group.open", table).trigger("fa.event.refreshdropdown");
  619. }
  620. };
  621. $(window).on("scroll", function () {
  622. checkdropdown();
  623. });
  624. tableBody.on("scroll", function () {
  625. checkdropdown();
  626. });
  627. }
  628. var id = table.attr("id");
  629. Table.list[id] = table;
  630. return table;
  631. },
  632. // 批量操作请求
  633. multi: function (action, ids, table, element) {
  634. var options = table.bootstrapTable('getOptions');
  635. var data = element ? $(element).data() : {};
  636. ids = ($.isArray(ids) ? ids.join(",") : ids);
  637. var url = typeof data.url !== "undefined" ? data.url : (action == "del" ? options.extend.del_url : options.extend.multi_url);
  638. var params = typeof data.params !== "undefined" ? (typeof data.params == 'object' ? $.param(data.params) : data.params) : '';
  639. options = {url: url, data: {action: action, ids: ids, params: params}};
  640. Fast.api.ajax(options, function (data, ret) {
  641. table.trigger("uncheckbox");
  642. var success = $(element).data("success") || $.noop;
  643. if (typeof success === 'function') {
  644. if (false === success.call(element, data, ret)) {
  645. return false;
  646. }
  647. }
  648. table.bootstrapTable('refresh');
  649. }, function (data, ret) {
  650. var error = $(element).data("error") || $.noop;
  651. if (typeof error === 'function') {
  652. if (false === error.call(element, data, ret)) {
  653. return false;
  654. }
  655. }
  656. });
  657. },
  658. // 单元格元素事件
  659. events: {
  660. operate: {
  661. 'click .btn-editone': function (e, value, row, index) {
  662. e.stopPropagation();
  663. e.preventDefault();
  664. var table = $(this).closest('table');
  665. var options = table.bootstrapTable('getOptions');
  666. var ids = row[options.pk];
  667. row = $.extend({}, row ? row : {}, {ids: ids});
  668. var url = options.extend.edit_url;
  669. Fast.api.open(Table.api.replaceurl(url, row, table), $(this).data("original-title") || $(this).attr("title") || __('Edit'), $(this).data() || {});
  670. },
  671. 'click .btn-delone': function (e, value, row, index) {
  672. e.stopPropagation();
  673. e.preventDefault();
  674. var that = this;
  675. var top = $(that).offset().top - $(window).scrollTop();
  676. var left = $(that).offset().left - $(window).scrollLeft() - 260;
  677. if (top + 154 > $(window).height()) {
  678. top = top - 154;
  679. }
  680. if ($(window).width() < 480) {
  681. top = left = undefined;
  682. }
  683. Layer.confirm(
  684. __('Are you sure you want to delete this item?'),
  685. {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true, btn: [__('OK'), __('Cancel')]},
  686. function (index) {
  687. var table = $(that).closest('table');
  688. var options = table.bootstrapTable('getOptions');
  689. Table.api.multi("del", row[options.pk], table, that);
  690. Layer.close(index);
  691. }
  692. );
  693. }
  694. },//单元格图片预览
  695. image: {
  696. 'click .img-center': function (e, value, row, index) {
  697. var data = [];
  698. value = value === null ? '' : value.toString();
  699. var arr = value != '' ? value.split(",") : [];
  700. var url;
  701. $.each(arr, function (index, value) {
  702. url = Fast.api.cdnurl(value);
  703. data.push({
  704. src: url,
  705. thumb: url.match(/^(\/|data:image\\)/) ? url : url + Config.upload.thumbstyle
  706. });
  707. });
  708. Layer.photos({
  709. photos: {
  710. "start": $(this).parent().index(),
  711. "data": data
  712. },
  713. anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
  714. });
  715. },
  716. }
  717. },
  718. // 单元格数据格式化
  719. formatter: {
  720. icon: function (value, row, index) {
  721. value = value === null ? '' : value.toString();
  722. value = value.indexOf(" ") > -1 ? value : "fa fa-" + value;
  723. //渲染fontawesome图标
  724. return '<i class="' + value + '"></i> ' + value;
  725. },
  726. image: function (value, row, index) {
  727. return Table.api.formatter.images.call(this, value, row, index);
  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.indexOf('data:image/') === -1 ? value.split(',') : [value]) : [];
  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. //匹配本地、data:image、或已包含标识符首字符
  739. url = !Config.upload.thumbstyle || url.match(/^(\/|data:image\/)/) || url.indexOf(Config.upload.thumbstyle.substring(0, 1)) > -1 ? url : url + Config.upload.thumbstyle;
  740. html.push('<a href="javascript:"><img class="' + classname + '" src="' + url + '" /></a>');
  741. });
  742. return html.join(' ');
  743. },
  744. file: function (value, row, index) {
  745. return Table.api.formatter.files.call(this, value, row, index);
  746. },
  747. files: function (value, row, index) {
  748. value = value == null || value.length === 0 ? '' : value.toString();
  749. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  750. var arr = value !== '' ? (value.indexOf('data:image/') === -1 ? value.split(',') : [value]) : [];
  751. var html = [];
  752. var suffix, url;
  753. $.each(arr, function (i, value) {
  754. value = Fast.api.cdnurl(value, true);
  755. suffix = /[\.]?([a-zA-Z0-9]+)$/.exec(value);
  756. suffix = suffix ? suffix[1] : 'file';
  757. url = Fast.api.fixurl("ajax/icon?suffix=" + suffix);
  758. html.push('<a href="' + value + '" target="_blank"><img src="' + url + '" class="' + classname + '"></a>');
  759. });
  760. return html.join(' ');
  761. },
  762. content: function (value, row, index) {
  763. var width = this.width != undefined ? (this.width.toString().match(/^\d+$/) ? this.width + "px" : this.width) : "250px";
  764. var hover = this.hover != undefined && this.hover ? "autocontent-hover" : "";
  765. return "<div class='autocontent-item " + hover + "' style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + ";'>" + value + "</div>";
  766. },
  767. status: function (value, row, index) {
  768. var custom = {normal: 'success', hidden: 'gray', deleted: 'danger', locked: 'info'};
  769. if (typeof this.custom !== 'undefined') {
  770. custom = $.extend(custom, this.custom);
  771. }
  772. this.custom = custom;
  773. this.icon = 'fa fa-circle';
  774. return Table.api.formatter.normal.call(this, value, row, index);
  775. },
  776. simtype: function (value, row, index) {
  777. var custom = {'0001': 'success', '0002': 'info', '0003': 'warning'};
  778. if (typeof this.custom !== 'undefined') {
  779. custom = $.extend(custom, this.custom);
  780. }
  781. this.custom = custom;
  782. return Table.api.formatter.normal.call(this, value, row, index);
  783. },
  784. // ["primary", "success", "danger", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
  785. simexamstatus: function (value, row, index) {
  786. var custom = {'0': 'gray', '1': 'info', '2': 'danger', '3': 'success','4': 'warning','5': 'aqua','6': 'navy','7': 'blue','80': 'red','81': 'yellow','90': 'maroon'};
  787. if (typeof this.custom !== 'undefined') {
  788. custom = $.extend(custom, this.custom);
  789. }
  790. this.custom = custom;
  791. this.icon = 'fa fa-circle';
  792. return Table.api.formatter.normal.call(this, value, row, index);
  793. },
  794. simmethod: function (value, row, index) {
  795. var custom = {'1': 'danger', '2': 'info', '3': 'success'};
  796. if (typeof this.custom !== 'undefined') {
  797. custom = $.extend(custom, this.custom);
  798. }
  799. this.custom = custom;
  800. return Table.api.formatter.normal.call(this, value, row, index);
  801. },
  802. answerName:function(value, row, index){
  803. var text = '';
  804. for (var i = 0;i<value.length;i++){
  805. text +='<span class="btn btn-xs m-1 btn-danger">'+value[i].cx_type+':'+value[i].cx_name+'</span>';
  806. }
  807. return text;
  808. },
  809. faultname:function(value, row, index){
  810. if (!value || value.trim() === '-') {
  811. // 当值为空或为 '-' 时,显示红色文本
  812. return `<span class="text-danger"></span>`;
  813. }
  814. // 生成随机渐变颜色
  815. function randomGradient() {
  816. var color1 = `#${Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0')}`;
  817. var color2 = `#${Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0')}`;
  818. return '#444c69';//`linear-gradient(45deg, ${color1}, ${color2})`;
  819. }
  820. // 将逗号分隔的内容转换为带渐变背景的按钮
  821. var items = value.split(',').map(function (item) {
  822. var gradient = randomGradient();
  823. return `<span class="btn btn-xs m-1"
  824. style="
  825. display: inline-block;
  826. background: ${gradient};
  827. color: white;
  828. border: none;
  829. /*padding: 5px 10px;*/
  830. border-radius: 4px;
  831. white-space: nowrap;
  832. font-size: 12px;
  833. ">
  834. ${item.trim()}
  835. </span>`;
  836. });
  837. return items.join(' ');
  838. },
  839. collectonstate: function (value, row, index) {
  840. var table = this.table;
  841. var options = table ? table.bootstrapTable('getOptions') : {};
  842. var pk = options.pk || "id";
  843. var color = typeof this.color !== 'undefined' ? this.color : 'success';
  844. var yes = typeof this.yes !== 'undefined' ? this.yes : 2;
  845. var no = typeof this.no !== 'undefined' ? this.no : 3;
  846. var url = typeof this.url !== 'undefined' ? this.url : '';
  847. var confirm = '';
  848. var disable = false;
  849. if (typeof this.confirm !== "undefined") {
  850. confirm = typeof this.confirm === "function" ? this.confirm.call(this, value, row, index) : this.confirm;
  851. }
  852. if (typeof this.disable !== "undefined") {
  853. disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable;
  854. }
  855. return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change " + (disable ? 'btn disabled no-padding' : '') + "' data-index='" + index + "' data-id='"
  856. + 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>";
  857. },
  858. normal: function (value, row, index) {
  859. var colorArr = ["primary", "success", "danger", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
  860. var custom = {};
  861. if (typeof this.custom !== 'undefined') {
  862. custom = $.extend(custom, this.custom);
  863. }
  864. value = value == null || value.length === 0 ? '' : value.toString();
  865. var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
  866. var index = keys.indexOf(value);
  867. var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
  868. var display = index > -1 ? this.searchList[value] : null;
  869. var icon = typeof this.icon !== 'undefined' ? this.icon : null;
  870. if (!color) {
  871. color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
  872. }
  873. if (!display) {
  874. display = __(value.charAt(0).toUpperCase() + value.slice(1));
  875. }
  876. var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
  877. if (this.operate != false) {
  878. html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
  879. }
  880. return html;
  881. },
  882. toggle: function (value, row, index) {
  883. var table = this.table;
  884. var options = table ? table.bootstrapTable('getOptions') : {};
  885. var pk = options.pk || "id";
  886. var color = typeof this.color !== 'undefined' ? this.color : 'success';
  887. var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
  888. var no = typeof this.no !== 'undefined' ? this.no : 0;
  889. var url = typeof this.url !== 'undefined' ? this.url : '';
  890. var confirm = '';
  891. var disable = false;
  892. if (typeof this.confirm !== "undefined") {
  893. confirm = typeof this.confirm === "function" ? this.confirm.call(this, value, row, index) : this.confirm;
  894. }
  895. if (typeof this.disable !== "undefined") {
  896. disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable;
  897. }
  898. return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change " + (disable ? 'btn disabled no-padding' : '') + "' data-index='" + index + "' data-id='"
  899. + 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>";
  900. },
  901. url: function (value, row, index) {
  902. value = value == null || value.length === 0 ? '' : value.toString();
  903. 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>';
  904. },
  905. search: function (value, row, index) {
  906. var field = this.field;
  907. if (typeof this.customField !== 'undefined') {
  908. var customValue = this.customField.split('.').reduce(function (obj, key) {
  909. return obj === null || obj === undefined ? '' : obj[key];
  910. }, row);
  911. value = Fast.api.escape(customValue);
  912. field = this.customField;
  913. }
  914. return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', value) + '" data-field="' + field + '" data-value="' + value + '">' + value + '</a>';
  915. },
  916. addtabs: function (value, row, index) {
  917. var url = Table.api.replaceurl(this.url || '', row, this.table);
  918. var title = this.atitle ? this.atitle : __("Search %s", value);
  919. return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  920. },
  921. dialog: function (value, row, index) {
  922. var url = Table.api.replaceurl(this.url || '', row, this.table);
  923. var title = this.atitle ? this.atitle : __("View %s", value);
  924. return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  925. },
  926. flag: function (value, row, index) {
  927. var that = this;
  928. value = value == null || value.length === 0 ? '' : value.toString();
  929. var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
  930. //如果字段列有定义custom
  931. if (typeof this.custom !== 'undefined') {
  932. colorArr = $.extend(colorArr, this.custom);
  933. }
  934. var field = this.field;
  935. if (typeof this.customField !== 'undefined') {
  936. var customValue = this.customField.split('.').reduce(function (obj, key) {
  937. return obj === null || obj === undefined ? '' : obj[key];
  938. }, row);
  939. value = Fast.api.escape(customValue);
  940. field = this.customField;
  941. }
  942. if (typeof that.searchList === 'object' && typeof that.custom === 'undefined') {
  943. var i = 0;
  944. var searchValues = Object.values(colorArr);
  945. $.each(that.searchList, function (key, val) {
  946. if (typeof colorArr[key] == 'undefined') {
  947. colorArr[key] = searchValues[i];
  948. i = typeof searchValues[i + 1] === 'undefined' ? 0 : i + 1;
  949. }
  950. });
  951. }
  952. //渲染Flag
  953. var html = [];
  954. var arr = value != '' ? value.split(',') : [];
  955. var color, display, label;
  956. $.each(arr, function (i, value) {
  957. value = value == null || value.length === 0 ? '' : value.toString();
  958. if (value == '')
  959. return true;
  960. color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
  961. display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
  962. label = '<span class="label label-' + color + '">' + display + '</span>';
  963. if (that.operate) {
  964. html.push('<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + field + '" data-value="' + value + '">' + label + '</a>');
  965. } else {
  966. html.push(label);
  967. }
  968. });
  969. return html.join(' ');
  970. },
  971. label: function (value, row, index) {
  972. return Table.api.formatter.flag.call(this, value, row, index);
  973. },
  974. datetime: function (value, row, index) {
  975. var datetimeFormat = typeof this.datetimeFormat === 'undefined' ? 'YYYY-MM-DD HH:mm:ss' : this.datetimeFormat;
  976. if (isNaN(value)) {
  977. return value ? Moment(value).format(datetimeFormat) : __('None');
  978. } else {
  979. return value ? Moment(parseInt(value) * 1000).format(datetimeFormat) : __('None');
  980. }
  981. },
  982. operate: function (value, row, index) {
  983. var table = this.table;
  984. // 操作配置
  985. var options = table ? table.bootstrapTable('getOptions') : {};
  986. // 默认按钮组
  987. var buttons = $.extend([], this.buttons || []);
  988. // 所有按钮名称
  989. var names = [];
  990. buttons.forEach(function (item) {
  991. names.push(item.name);
  992. });
  993. if (options.extend.dragsort_url !== '' && names.indexOf('dragsort') === -1) {
  994. buttons.push(Table.button.dragsort);
  995. }
  996. if (options.extend.edit_url !== '' && names.indexOf('edit') === -1) {
  997. Table.button.edit.url = options.extend.edit_url;
  998. buttons.push(Table.button.edit);
  999. }
  1000. if (options.extend.del_url !== '' && names.indexOf('del') === -1) {
  1001. buttons.push(Table.button.del);
  1002. }
  1003. return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
  1004. }
  1005. ,
  1006. buttons: function (value, row, index) {
  1007. // 默认按钮组
  1008. var buttons = $.extend([], this.buttons || []);
  1009. return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
  1010. }
  1011. },
  1012. buttonlink: function (column, buttons, value, row, index, type) {
  1013. var table = column.table;
  1014. column.clickToSelect = false;
  1015. type = typeof type === 'undefined' ? 'buttons' : type;
  1016. var options = table ? table.bootstrapTable('getOptions') : {};
  1017. var html = [];
  1018. var hidden, visible, disable, url, classname, icon, text, title, refresh, confirm, extend,
  1019. dropdown, link;
  1020. var fieldIndex = column.fieldIndex;
  1021. var dropdowns = {};
  1022. $.each(buttons, function (i, j) {
  1023. if (type === 'operate') {
  1024. if (j.name === 'dragsort' && typeof row[Table.config.dragsortfield] === 'undefined') {
  1025. return true;
  1026. }
  1027. if (['add', 'edit', 'del', 'multi', 'dragsort'].indexOf(j.name) > -1 && !options.extend[j.name + "_url"]) {
  1028. return true;
  1029. }
  1030. }
  1031. var attr = table.data(type + "-" + j.name);
  1032. if (typeof attr === 'undefined' || attr) {
  1033. hidden = typeof j.hidden === 'function' ? j.hidden.call(table, row, j) : (typeof j.hidden !== 'undefined' ? j.hidden : false);
  1034. if (hidden) {
  1035. return true;
  1036. }
  1037. visible = typeof j.visible === 'function' ? j.visible.call(table, row, j) : (typeof j.visible !== 'undefined' ? j.visible : true);
  1038. if (!visible) {
  1039. return true;
  1040. }
  1041. dropdown = j.dropdown ? j.dropdown : '';
  1042. url = j.url ? j.url : '';
  1043. url = typeof url === 'function' ? url.call(table, row, j) : (url ? Fast.api.fixurl(Table.api.replaceurl(url, row, table)) : 'javascript:;');
  1044. classname = j.classname ? j.classname : (dropdown ? 'btn-' + name + 'one' : 'btn-primary btn-' + name + 'one');
  1045. icon = j.icon ? j.icon : '';
  1046. text = typeof j.text === 'function' ? j.text.call(table, row, j) : j.text ? j.text : '';
  1047. title = typeof j.title === 'function' ? j.title.call(table, row, j) : j.title ? j.title : text;
  1048. refresh = j.refresh ? 'data-refresh="' + j.refresh + '"' : '';
  1049. confirm = typeof j.confirm === 'function' ? j.confirm.call(table, row, j) : (typeof j.confirm !== 'undefined' ? j.confirm : false);
  1050. confirm = confirm ? 'data-confirm="' + confirm + '"' : '';
  1051. extend = typeof j.extend === 'function' ? j.extend.call(table, row, j) : (typeof j.extend !== 'undefined' ? j.extend : '');
  1052. disable = typeof j.disable === 'function' ? j.disable.call(table, row, j) : (typeof j.disable !== 'undefined' ? j.disable : false);
  1053. if (disable) {
  1054. classname = classname + ' disabled';
  1055. }
  1056. 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>';
  1057. if (dropdown) {
  1058. if (typeof dropdowns[dropdown] == 'undefined') {
  1059. dropdowns[dropdown] = [];
  1060. }
  1061. dropdowns[dropdown].push(link);
  1062. } else {
  1063. html.push(link);
  1064. }
  1065. }
  1066. });
  1067. if (!$.isEmptyObject(dropdowns)) {
  1068. var dropdownHtml = [];
  1069. $.each(dropdowns, function (i, j) {
  1070. 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>');
  1071. });
  1072. html.unshift(dropdownHtml.join(' '));
  1073. }
  1074. return html.join(' ');
  1075. },
  1076. //替换URL中的数据
  1077. replaceurl: function (url, row, table) {
  1078. var options = table ? table.bootstrapTable('getOptions') : null;
  1079. var ids = options ? row[options.pk] : 0;
  1080. row.ids = ids ? ids : (typeof row.ids !== 'undefined' ? row.ids : 0);
  1081. url = url == null || url.length === 0 ? '' : url.toString();
  1082. //自动添加ids参数
  1083. url = !url.match(/(?=([?&]ids=)|(\/ids\/)|(\{ids}))/i) ?
  1084. url + (url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + '{ids}' : url;
  1085. url = url.replace(/\{(.*?)\}/gi, function (matched) {
  1086. matched = matched.substring(1, matched.length - 1);
  1087. var temp = matched.split('.').reduce(function (obj, key) {
  1088. return obj === null || obj === undefined ? '' : obj[key];
  1089. }, row);
  1090. temp = Fast.api.escape(temp);
  1091. return temp;
  1092. });
  1093. return url;
  1094. },
  1095. // 获取选中的条目ID集合
  1096. selectedids: function (table, current) {
  1097. var options = table.bootstrapTable('getOptions');
  1098. //如果有设置翻页记忆模式
  1099. if (!current && options.maintainSelected) {
  1100. return options.selectedIds;
  1101. }
  1102. return $.map(table.bootstrapTable('getSelections'), function (row) {
  1103. return row[options.pk];
  1104. });
  1105. },
  1106. //获取选中的数据
  1107. selecteddata: function (table, current) {
  1108. var options = table.bootstrapTable('getOptions');
  1109. //如果有设置翻页记忆模式
  1110. if (!current && options.maintainSelected) {
  1111. return options.selectedData;
  1112. }
  1113. return table.bootstrapTable('getSelections');
  1114. },
  1115. // 切换复选框状态
  1116. toggleattr: function (table) {
  1117. $("input[type='checkbox']", table).trigger('click');
  1118. },
  1119. // 根据行索引获取行数据
  1120. getrowdata: function (table, index) {
  1121. index = parseInt(index);
  1122. var data = table.bootstrapTable('getData');
  1123. return typeof data[index] !== 'undefined' ? data[index] : null;
  1124. },
  1125. // 根据行索引获取行数据
  1126. getrowbyindex: function (table, index) {
  1127. return Table.api.getrowdata(table, index);
  1128. },
  1129. // 根据主键ID获取行数据
  1130. getrowbyid: function (table, id) {
  1131. var row = {};
  1132. var options = table.bootstrapTable("getOptions");
  1133. $.each(Table.api.selecteddata(table), function (i, j) {
  1134. if (j[options.pk] == id) {
  1135. row = j;
  1136. return false;
  1137. }
  1138. });
  1139. return row;
  1140. }
  1141. },
  1142. };
  1143. return Table;
  1144. });