require-table.js 66 KB

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