require-table.js 60 KB

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