require-table.js 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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 __('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. 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. faultname:function(value, row, index){
  803. if (!value || value.trim() === '-') {
  804. // 当值为空或为 '-' 时,显示红色文本
  805. return `<span class="text-danger"></span>`;
  806. }
  807. // 生成随机渐变颜色
  808. function randomGradient() {
  809. var color1 = `#${Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0')}`;
  810. var color2 = `#${Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0')}`;
  811. return `linear-gradient(45deg, ${color1}, ${color2})`;
  812. }
  813. // 将逗号分隔的内容转换为带渐变背景的按钮
  814. var items = value.split(',').map(function (item) {
  815. var gradient = randomGradient();
  816. return `<span class="btn btn-xs m-1"
  817. style="
  818. display: inline-block;
  819. background: ${gradient};
  820. color: white;
  821. border: none;
  822. padding: 5px 10px;
  823. border-radius: 4px;
  824. white-space: nowrap;
  825. font-size: 12px;
  826. ">
  827. ${item.trim()}
  828. </span>`;
  829. });
  830. return items.join(' ');
  831. },
  832. collectonstate: function (value, row, index) {
  833. var table = this.table;
  834. var options = table ? table.bootstrapTable('getOptions') : {};
  835. var pk = options.pk || "id";
  836. var color = typeof this.color !== 'undefined' ? this.color : 'success';
  837. var yes = typeof this.yes !== 'undefined' ? this.yes : 2;
  838. var no = typeof this.no !== 'undefined' ? this.no : 3;
  839. var url = typeof this.url !== 'undefined' ? this.url : '';
  840. var confirm = '';
  841. var disable = false;
  842. if (typeof this.confirm !== "undefined") {
  843. confirm = typeof this.confirm === "function" ? this.confirm.call(this, value, row, index) : this.confirm;
  844. }
  845. if (typeof this.disable !== "undefined") {
  846. disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable;
  847. }
  848. return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change " + (disable ? 'btn disabled no-padding' : '') + "' data-index='" + index + "' data-id='"
  849. + 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>";
  850. },
  851. normal: function (value, row, index) {
  852. var colorArr = ["primary", "success", "danger", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
  853. var custom = {};
  854. if (typeof this.custom !== 'undefined') {
  855. custom = $.extend(custom, this.custom);
  856. }
  857. value = value == null || value.length === 0 ? '' : value.toString();
  858. var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
  859. var index = keys.indexOf(value);
  860. var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
  861. var display = index > -1 ? this.searchList[value] : null;
  862. var icon = typeof this.icon !== 'undefined' ? this.icon : null;
  863. if (!color) {
  864. color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
  865. }
  866. if (!display) {
  867. display = __(value.charAt(0).toUpperCase() + value.slice(1));
  868. }
  869. var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
  870. if (this.operate != false) {
  871. html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
  872. }
  873. return html;
  874. },
  875. toggle: function (value, row, index) {
  876. var table = this.table;
  877. var options = table ? table.bootstrapTable('getOptions') : {};
  878. var pk = options.pk || "id";
  879. var color = typeof this.color !== 'undefined' ? this.color : 'success';
  880. var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
  881. var no = typeof this.no !== 'undefined' ? this.no : 0;
  882. var url = typeof this.url !== 'undefined' ? this.url : '';
  883. var confirm = '';
  884. var disable = false;
  885. if (typeof this.confirm !== "undefined") {
  886. confirm = typeof this.confirm === "function" ? this.confirm.call(this, value, row, index) : this.confirm;
  887. }
  888. if (typeof this.disable !== "undefined") {
  889. disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable;
  890. }
  891. return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change " + (disable ? 'btn disabled no-padding' : '') + "' data-index='" + index + "' data-id='"
  892. + 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>";
  893. },
  894. url: function (value, row, index) {
  895. value = value == null || value.length === 0 ? '' : value.toString();
  896. 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>';
  897. },
  898. search: function (value, row, index) {
  899. var field = this.field;
  900. if (typeof this.customField !== 'undefined') {
  901. var customValue = this.customField.split('.').reduce(function (obj, key) {
  902. return obj === null || obj === undefined ? '' : obj[key];
  903. }, row);
  904. value = Fast.api.escape(customValue);
  905. field = this.customField;
  906. }
  907. return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', value) + '" data-field="' + field + '" data-value="' + value + '">' + value + '</a>';
  908. },
  909. addtabs: function (value, row, index) {
  910. var url = Table.api.replaceurl(this.url || '', row, this.table);
  911. var title = this.atitle ? this.atitle : __("Search %s", value);
  912. return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  913. },
  914. dialog: function (value, row, index) {
  915. var url = Table.api.replaceurl(this.url || '', row, this.table);
  916. var title = this.atitle ? this.atitle : __("View %s", value);
  917. return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  918. },
  919. flag: function (value, row, index) {
  920. var that = this;
  921. value = value == null || value.length === 0 ? '' : value.toString();
  922. var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
  923. //如果字段列有定义custom
  924. if (typeof this.custom !== 'undefined') {
  925. colorArr = $.extend(colorArr, this.custom);
  926. }
  927. var field = this.field;
  928. if (typeof this.customField !== 'undefined') {
  929. var customValue = this.customField.split('.').reduce(function (obj, key) {
  930. return obj === null || obj === undefined ? '' : obj[key];
  931. }, row);
  932. value = Fast.api.escape(customValue);
  933. field = this.customField;
  934. }
  935. if (typeof that.searchList === 'object' && typeof that.custom === 'undefined') {
  936. var i = 0;
  937. var searchValues = Object.values(colorArr);
  938. $.each(that.searchList, function (key, val) {
  939. if (typeof colorArr[key] == 'undefined') {
  940. colorArr[key] = searchValues[i];
  941. i = typeof searchValues[i + 1] === 'undefined' ? 0 : i + 1;
  942. }
  943. });
  944. }
  945. //渲染Flag
  946. var html = [];
  947. var arr = value != '' ? value.split(',') : [];
  948. var color, display, label;
  949. $.each(arr, function (i, value) {
  950. value = value == null || value.length === 0 ? '' : value.toString();
  951. if (value == '')
  952. return true;
  953. color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
  954. display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
  955. label = '<span class="label label-' + color + '">' + display + '</span>';
  956. if (that.operate) {
  957. html.push('<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + field + '" data-value="' + value + '">' + label + '</a>');
  958. } else {
  959. html.push(label);
  960. }
  961. });
  962. return html.join(' ');
  963. },
  964. label: function (value, row, index) {
  965. return Table.api.formatter.flag.call(this, value, row, index);
  966. },
  967. datetime: function (value, row, index) {
  968. var datetimeFormat = typeof this.datetimeFormat === 'undefined' ? 'YYYY-MM-DD HH:mm:ss' : this.datetimeFormat;
  969. if (isNaN(value)) {
  970. return value ? Moment(value).format(datetimeFormat) : __('None');
  971. } else {
  972. return value ? Moment(parseInt(value) * 1000).format(datetimeFormat) : __('None');
  973. }
  974. },
  975. operate: function (value, row, index) {
  976. var table = this.table;
  977. // 操作配置
  978. var options = table ? table.bootstrapTable('getOptions') : {};
  979. // 默认按钮组
  980. var buttons = $.extend([], this.buttons || []);
  981. // 所有按钮名称
  982. var names = [];
  983. buttons.forEach(function (item) {
  984. names.push(item.name);
  985. });
  986. if (options.extend.dragsort_url !== '' && names.indexOf('dragsort') === -1) {
  987. buttons.push(Table.button.dragsort);
  988. }
  989. if (options.extend.edit_url !== '' && names.indexOf('edit') === -1) {
  990. Table.button.edit.url = options.extend.edit_url;
  991. buttons.push(Table.button.edit);
  992. }
  993. if (options.extend.del_url !== '' && names.indexOf('del') === -1) {
  994. buttons.push(Table.button.del);
  995. }
  996. return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
  997. }
  998. ,
  999. buttons: function (value, row, index) {
  1000. // 默认按钮组
  1001. var buttons = $.extend([], this.buttons || []);
  1002. return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
  1003. }
  1004. },
  1005. buttonlink: function (column, buttons, value, row, index, type) {
  1006. var table = column.table;
  1007. column.clickToSelect = false;
  1008. type = typeof type === 'undefined' ? 'buttons' : type;
  1009. var options = table ? table.bootstrapTable('getOptions') : {};
  1010. var html = [];
  1011. var hidden, visible, disable, url, classname, icon, text, title, refresh, confirm, extend,
  1012. dropdown, link;
  1013. var fieldIndex = column.fieldIndex;
  1014. var dropdowns = {};
  1015. $.each(buttons, function (i, j) {
  1016. if (type === 'operate') {
  1017. if (j.name === 'dragsort' && typeof row[Table.config.dragsortfield] === 'undefined') {
  1018. return true;
  1019. }
  1020. if (['add', 'edit', 'del', 'multi', 'dragsort'].indexOf(j.name) > -1 && !options.extend[j.name + "_url"]) {
  1021. return true;
  1022. }
  1023. }
  1024. var attr = table.data(type + "-" + j.name);
  1025. if (typeof attr === 'undefined' || attr) {
  1026. hidden = typeof j.hidden === 'function' ? j.hidden.call(table, row, j) : (typeof j.hidden !== 'undefined' ? j.hidden : false);
  1027. if (hidden) {
  1028. return true;
  1029. }
  1030. visible = typeof j.visible === 'function' ? j.visible.call(table, row, j) : (typeof j.visible !== 'undefined' ? j.visible : true);
  1031. if (!visible) {
  1032. return true;
  1033. }
  1034. dropdown = j.dropdown ? j.dropdown : '';
  1035. url = j.url ? j.url : '';
  1036. url = typeof url === 'function' ? url.call(table, row, j) : (url ? Fast.api.fixurl(Table.api.replaceurl(url, row, table)) : 'javascript:;');
  1037. classname = j.classname ? j.classname : (dropdown ? 'btn-' + name + 'one' : 'btn-primary btn-' + name + 'one');
  1038. icon = j.icon ? j.icon : '';
  1039. text = typeof j.text === 'function' ? j.text.call(table, row, j) : j.text ? j.text : '';
  1040. title = typeof j.title === 'function' ? j.title.call(table, row, j) : j.title ? j.title : text;
  1041. refresh = j.refresh ? 'data-refresh="' + j.refresh + '"' : '';
  1042. confirm = typeof j.confirm === 'function' ? j.confirm.call(table, row, j) : (typeof j.confirm !== 'undefined' ? j.confirm : false);
  1043. confirm = confirm ? 'data-confirm="' + confirm + '"' : '';
  1044. extend = typeof j.extend === 'function' ? j.extend.call(table, row, j) : (typeof j.extend !== 'undefined' ? j.extend : '');
  1045. disable = typeof j.disable === 'function' ? j.disable.call(table, row, j) : (typeof j.disable !== 'undefined' ? j.disable : false);
  1046. if (disable) {
  1047. classname = classname + ' disabled';
  1048. }
  1049. 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>';
  1050. if (dropdown) {
  1051. if (typeof dropdowns[dropdown] == 'undefined') {
  1052. dropdowns[dropdown] = [];
  1053. }
  1054. dropdowns[dropdown].push(link);
  1055. } else {
  1056. html.push(link);
  1057. }
  1058. }
  1059. });
  1060. if (!$.isEmptyObject(dropdowns)) {
  1061. var dropdownHtml = [];
  1062. $.each(dropdowns, function (i, j) {
  1063. 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>');
  1064. });
  1065. html.unshift(dropdownHtml.join(' '));
  1066. }
  1067. return html.join(' ');
  1068. },
  1069. //替换URL中的数据
  1070. replaceurl: function (url, row, table) {
  1071. var options = table ? table.bootstrapTable('getOptions') : null;
  1072. var ids = options ? row[options.pk] : 0;
  1073. row.ids = ids ? ids : (typeof row.ids !== 'undefined' ? row.ids : 0);
  1074. url = url == null || url.length === 0 ? '' : url.toString();
  1075. //自动添加ids参数
  1076. url = !url.match(/(?=([?&]ids=)|(\/ids\/)|(\{ids}))/i) ?
  1077. url + (url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + '{ids}' : url;
  1078. url = url.replace(/\{(.*?)\}/gi, function (matched) {
  1079. matched = matched.substring(1, matched.length - 1);
  1080. var temp = matched.split('.').reduce(function (obj, key) {
  1081. return obj === null || obj === undefined ? '' : obj[key];
  1082. }, row);
  1083. temp = Fast.api.escape(temp);
  1084. return temp;
  1085. });
  1086. return url;
  1087. },
  1088. // 获取选中的条目ID集合
  1089. selectedids: function (table, current) {
  1090. var options = table.bootstrapTable('getOptions');
  1091. //如果有设置翻页记忆模式
  1092. if (!current && options.maintainSelected) {
  1093. return options.selectedIds;
  1094. }
  1095. return $.map(table.bootstrapTable('getSelections'), function (row) {
  1096. return row[options.pk];
  1097. });
  1098. },
  1099. //获取选中的数据
  1100. selecteddata: function (table, current) {
  1101. var options = table.bootstrapTable('getOptions');
  1102. //如果有设置翻页记忆模式
  1103. if (!current && options.maintainSelected) {
  1104. return options.selectedData;
  1105. }
  1106. return table.bootstrapTable('getSelections');
  1107. },
  1108. // 切换复选框状态
  1109. toggleattr: function (table) {
  1110. $("input[type='checkbox']", table).trigger('click');
  1111. },
  1112. // 根据行索引获取行数据
  1113. getrowdata: function (table, index) {
  1114. index = parseInt(index);
  1115. var data = table.bootstrapTable('getData');
  1116. return typeof data[index] !== 'undefined' ? data[index] : null;
  1117. },
  1118. // 根据行索引获取行数据
  1119. getrowbyindex: function (table, index) {
  1120. return Table.api.getrowdata(table, index);
  1121. },
  1122. // 根据主键ID获取行数据
  1123. getrowbyid: function (table, id) {
  1124. var row = {};
  1125. var options = table.bootstrapTable("getOptions");
  1126. $.each(Table.api.selecteddata(table), function (i, j) {
  1127. if (j[options.pk] == id) {
  1128. row = j;
  1129. return false;
  1130. }
  1131. });
  1132. return row;
  1133. }
  1134. },
  1135. };
  1136. return Table;
  1137. });