require-table.js 60 KB

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