require-table.js 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. var confirm = $(this).data("confirm");
  467. var message = typeof confirm === 'function' ? confirm.call(this, ids) : (typeof confirm !== 'undefined' ? __(confirm, ids.length) : '');
  468. Layer.confirm(
  469. message || __('Are you sure you want to delete the %s selected item?', ids.length),
  470. {icon: 3, title: __('Warning'), offset: 0, shadeClose: true, btn: [__('OK'), __('Cancel')]},
  471. function (index) {
  472. Table.api.multi("del", ids, table, that);
  473. Layer.close(index);
  474. }
  475. );
  476. });
  477. // 拖拽排序
  478. require(['dragsort'], function () {
  479. //绑定拖动排序
  480. $("tbody", table).dragsort({
  481. itemSelector: 'tr:visible',
  482. dragSelector: "a.btn-dragsort",
  483. dragEnd: function (a, b) {
  484. var element = $("a.btn-dragsort", this);
  485. var data = table.bootstrapTable('getData');
  486. var current = data[parseInt($(this).data("index"))];
  487. var options = table.bootstrapTable('getOptions');
  488. //改变的值和改变的ID集合
  489. var ids = $.map($("tbody tr:visible", table), function (tr) {
  490. return data[parseInt($(tr).data("index"))][options.pk];
  491. });
  492. var changeid = current[options.pk];
  493. var pid = typeof current.pid != 'undefined' ? current.pid : '';
  494. var params = {
  495. url: table.bootstrapTable('getOptions').extend.dragsort_url,
  496. data: {
  497. ids: ids.join(','),
  498. changeid: changeid,
  499. pid: pid,
  500. field: Table.config.dragsortfield,
  501. orderway: options.sortOrder,
  502. table: options.extend.table,
  503. pk: options.pk
  504. }
  505. };
  506. Fast.api.ajax(params, function (data, ret) {
  507. var success = $(element).data("success") || $.noop;
  508. if (typeof success === 'function') {
  509. if (false === success.call(element, data, ret)) {
  510. return false;
  511. }
  512. }
  513. table.bootstrapTable('refresh');
  514. }, function (data, ret) {
  515. var error = $(element).data("error") || $.noop;
  516. if (typeof error === 'function') {
  517. if (false === error.call(element, data, ret)) {
  518. return false;
  519. }
  520. }
  521. table.bootstrapTable('refresh');
  522. });
  523. },
  524. placeHolderTemplate: ""
  525. });
  526. });
  527. table.on("click", "input[data-id][name='checkbox']", function (e) {
  528. var ids = $(this).data("id");
  529. table.bootstrapTable($(this).prop("checked") ? 'checkBy' : 'uncheckBy', {field: options.pk, values: [ids]});
  530. });
  531. table.on("click", "[data-id].btn-change", function (e) {
  532. e.preventDefault();
  533. var changer = $.proxy(function () {
  534. Table.api.multi($(this).data("action") ? $(this).data("action") : '', [$(this).data("id")], table, this);
  535. }, this);
  536. if (typeof $(this).data("confirm") !== 'undefined') {
  537. Layer.confirm($(this).data("confirm"), function (index) {
  538. changer();
  539. Layer.close(index);
  540. });
  541. } else {
  542. changer();
  543. }
  544. });
  545. table.on("click", "[data-id].btn-edit", function (e) {
  546. e.preventDefault();
  547. var ids = $(this).data("id");
  548. var row = Table.api.getrowbyid(table, ids);
  549. row.ids = ids;
  550. var url = Table.api.replaceurl(options.extend.edit_url, row, table);
  551. Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('Edit'), $(this).data() || {});
  552. });
  553. table.on("click", "[data-id].btn-del", function (e) {
  554. e.preventDefault();
  555. var id = $(this).data("id");
  556. var that = this;
  557. Layer.confirm(
  558. __('Are you sure you want to delete this item?'),
  559. {icon: 3, title: __('Warning'), shadeClose: true, btn: [__('OK'), __('Cancel')]},
  560. function (index) {
  561. Table.api.multi("del", id, table, that);
  562. Layer.close(index);
  563. }
  564. );
  565. });
  566. table.on("mouseenter mouseleave", ".autocontent", function (e) {
  567. var target = $(".autocontent-item", this).get(0);
  568. if (!target) return;
  569. if (e.type === 'mouseenter') {
  570. if (target.scrollWidth > target.offsetWidth && $(".autocontent-caret", this).length === 0) {
  571. $(this).append("<div class='autocontent-caret'><i class='fa fa-chevron-down'></div>");
  572. }
  573. } else {
  574. $(".autocontent-caret", this).remove();
  575. }
  576. });
  577. table.on("click mouseenter", ".autocontent-caret", function (e) {
  578. var hover = $(this).prev().hasClass("autocontent-hover");
  579. if (!hover && e.type === 'mouseenter') {
  580. return;
  581. }
  582. var text = $(this).prev().text();
  583. var tdrect = $(this).parent().get(0).getBoundingClientRect();
  584. var index = Layer.open({id: 'autocontent', skin: 'layui-layer-fast layui-layer-autocontent', title: false, content: text, btn: false, anim: false, shade: 0, isOutAnim: false, area: 'auto', maxWidth: 450, maxHeight: 350, offset: [tdrect.y, tdrect.x]});
  585. if (hover) {
  586. $(document).one("mouseleave", "#layui-layer" + index, function () {
  587. Layer.close(index);
  588. });
  589. }
  590. var mousedown = function (e) {
  591. if ($(e.target).closest(".layui-layer").length === 0) {
  592. Layer.close(index);
  593. $(document).off("mousedown", mousedown);
  594. }
  595. };
  596. $(document).off("mousedown", mousedown).on("mousedown", mousedown);
  597. });
  598. //修复dropdown定位溢出的情况
  599. if (options.fixDropdownPosition) {
  600. var tableBody = table.closest(".fixed-table-body");
  601. table.on('show.bs.dropdown fa.event.refreshdropdown', ".btn-group", function (e) {
  602. var dropdownMenu = $(".dropdown-menu", this);
  603. var btnGroup = $(this);
  604. var isPullRight = dropdownMenu.hasClass("pull-right") || dropdownMenu.hasClass("dropdown-menu-right");
  605. var left, top, position;
  606. if (true || dropdownMenu.outerHeight() + btnGroup.outerHeight() > tableBody.outerHeight() - 41) {
  607. position = 'fixed';
  608. top = btnGroup.offset().top - $(window).scrollTop() + btnGroup.outerHeight();
  609. if ((top + dropdownMenu.outerHeight()) > $(window).height()) {
  610. top = btnGroup.offset().top - dropdownMenu.outerHeight() - 5;
  611. }
  612. left = isPullRight ? btnGroup.offset().left + btnGroup.outerWidth() - dropdownMenu.outerWidth() : btnGroup.offset().left;
  613. }
  614. if (left || top) {
  615. dropdownMenu.css({
  616. position: position, left: left, top: top, right: 'inherit'
  617. });
  618. }
  619. });
  620. var checkdropdown = function () {
  621. if ($(".btn-group.open", table).length > 0 && $(".btn-group.open .dropdown-menu", table).css("position") == 'fixed') {
  622. $(".btn-group.open", table).trigger("fa.event.refreshdropdown");
  623. }
  624. };
  625. $(window).on("scroll", function () {
  626. checkdropdown();
  627. });
  628. tableBody.on("scroll", function () {
  629. checkdropdown();
  630. });
  631. }
  632. var id = table.attr("id");
  633. Table.list[id] = table;
  634. return table;
  635. },
  636. // 批量操作请求
  637. multi: function (action, ids, table, element) {
  638. var options = table.bootstrapTable('getOptions');
  639. var data = element ? $(element).data() : {};
  640. ids = ($.isArray(ids) ? ids.join(",") : ids);
  641. var url = typeof data.url !== "undefined" ? data.url : (action == "del" ? options.extend.del_url : options.extend.multi_url);
  642. var params = typeof data.params !== "undefined" ? (typeof data.params == 'object' ? $.param(data.params) : data.params) : '';
  643. options = {url: url, data: {action: action, ids: ids, params: params}};
  644. Fast.api.ajax(options, function (data, ret) {
  645. table.trigger("uncheckbox");
  646. var success = $(element).data("success") || $.noop;
  647. if (typeof success === 'function') {
  648. if (false === success.call(element, data, ret)) {
  649. return false;
  650. }
  651. }
  652. table.bootstrapTable('refresh');
  653. }, function (data, ret) {
  654. var error = $(element).data("error") || $.noop;
  655. if (typeof error === 'function') {
  656. if (false === error.call(element, data, ret)) {
  657. return false;
  658. }
  659. }
  660. });
  661. },
  662. // 单元格元素事件
  663. events: {
  664. operate: {
  665. 'click .btn-editone': function (e, value, row, index) {
  666. e.stopPropagation();
  667. e.preventDefault();
  668. var table = $(this).closest('table');
  669. var options = table.bootstrapTable('getOptions');
  670. var ids = row[options.pk];
  671. row = $.extend({}, row ? row : {}, {ids: ids});
  672. var url = options.extend.edit_url;
  673. Fast.api.open(Table.api.replaceurl(url, row, table), $(this).data("original-title") || $(this).attr("title") || __('Edit'), $(this).data() || {});
  674. },
  675. 'click .btn-delone': function (e, value, row, index) {
  676. e.stopPropagation();
  677. e.preventDefault();
  678. var that = this;
  679. var top = $(that).offset().top - $(window).scrollTop();
  680. var left = $(that).offset().left - $(window).scrollLeft() - 260;
  681. if (top + 154 > $(window).height()) {
  682. top = top - 154;
  683. }
  684. if ($(window).width() < 480) {
  685. top = left = undefined;
  686. }
  687. Layer.confirm(
  688. $(that).data("confirm") || __('Are you sure you want to delete this item?'),
  689. {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true, btn: [__('OK'), __('Cancel')]},
  690. function (index) {
  691. var table = $(that).closest('table');
  692. var options = table.bootstrapTable('getOptions');
  693. Table.api.multi("del", row[options.pk], table, that);
  694. Layer.close(index);
  695. }
  696. );
  697. }
  698. },//单元格图片预览
  699. image: {
  700. 'click .img-center': function (e, value, row, index) {
  701. var data = [];
  702. value = value === null ? '' : value.toString();
  703. var arr = value != '' ? value.split(",") : [];
  704. var url;
  705. $.each(arr, function (index, value) {
  706. url = Fast.api.cdnurl(value);
  707. data.push({
  708. src: url,
  709. thumb: url.match(/^(\/|data:image\\)/) ? url : url + Config.upload.thumbstyle
  710. });
  711. });
  712. Layer.photos({
  713. photos: {
  714. "start": $(this).parent().index(),
  715. "data": data
  716. },
  717. anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
  718. });
  719. },
  720. }
  721. },
  722. // 单元格数据格式化
  723. formatter: {
  724. icon: function (value, row, index) {
  725. value = value === null ? '' : value.toString();
  726. value = value.indexOf(" ") > -1 ? value : "fa fa-" + value;
  727. //渲染fontawesome图标
  728. return '<i class="' + value + '"></i> ' + value;
  729. },
  730. image: function (value, row, index) {
  731. return Table.api.formatter.images.call(this, value, row, index);
  732. },
  733. images: function (value, row, index) {
  734. value = value == null || value.length === 0 ? '' : value.toString();
  735. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  736. var arr = value !== '' ? (value.indexOf('data:image/') === -1 ? value.split(',') : [value]) : [];
  737. var html = [];
  738. var url;
  739. $.each(arr, function (i, value) {
  740. value = value ? value : '/assets/img/blank.gif';
  741. url = Fast.api.cdnurl(value, true);
  742. //匹配本地、data:image、或已包含标识符首字符
  743. url = !Config.upload.thumbstyle || url.match(/^(\/|data:image\/)/) || url.indexOf(Config.upload.thumbstyle.substring(0, 1)) > -1 ? url : url + Config.upload.thumbstyle;
  744. html.push('<a href="javascript:"><img class="' + classname + '" src="' + url + '" /></a>');
  745. });
  746. return html.join(' ');
  747. },
  748. file: function (value, row, index) {
  749. return Table.api.formatter.files.call(this, value, row, index);
  750. },
  751. files: function (value, row, index) {
  752. value = value == null || value.length === 0 ? '' : value.toString();
  753. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  754. var arr = value !== '' ? (value.indexOf('data:image/') === -1 ? value.split(',') : [value]) : [];
  755. var html = [];
  756. var suffix, url;
  757. $.each(arr, function (i, value) {
  758. value = Fast.api.cdnurl(value, true);
  759. suffix = /[\.]?([a-zA-Z0-9]+)$/.exec(value);
  760. suffix = suffix ? suffix[1] : 'file';
  761. url = Fast.api.fixurl("ajax/icon?suffix=" + suffix);
  762. html.push('<a href="' + value + '" target="_blank"><img src="' + url + '" class="' + classname + '" width="30" height="30"></a>');
  763. });
  764. return html.join(' ');
  765. },
  766. content: function (value, row, index) {
  767. var width = this.width != undefined ? (this.width.toString().match(/^\d+$/) ? this.width + "px" : this.width) : "250px";
  768. var hover = this.hover != undefined && this.hover ? "autocontent-hover" : "";
  769. return "<div class='autocontent-item " + hover + "' style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + ";'>" + value + "</div>";
  770. },
  771. status: function (value, row, index) {
  772. var custom = {normal: 'success', hidden: 'gray', deleted: 'danger', locked: 'info'};
  773. if (typeof this.custom !== 'undefined') {
  774. custom = $.extend(custom, this.custom);
  775. }
  776. this.custom = custom;
  777. this.icon = 'fa fa-circle';
  778. return Table.api.formatter.normal.call(this, value, row, index);
  779. },
  780. normal: function (value, row, index) {
  781. var colorArr = ["primary", "success", "danger", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
  782. var custom = {};
  783. if (typeof this.custom !== 'undefined') {
  784. custom = $.extend(custom, this.custom);
  785. }
  786. value = value == null || value.length === 0 ? '' : value.toString();
  787. var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
  788. var index = keys.indexOf(value);
  789. var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
  790. var display = index > -1 ? this.searchList[value] : null;
  791. var icon = typeof this.icon !== 'undefined' ? this.icon : null;
  792. if (!color) {
  793. color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
  794. }
  795. if (!display) {
  796. display = __(value.charAt(0).toUpperCase() + value.slice(1));
  797. }
  798. var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
  799. if (this.operate != false) {
  800. html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
  801. }
  802. return html;
  803. },
  804. toggle: function (value, row, index) {
  805. var table = this.table;
  806. var options = table ? table.bootstrapTable('getOptions') : {};
  807. var pk = options.pk || "id";
  808. var color = typeof this.color !== 'undefined' ? this.color : 'success';
  809. var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
  810. var no = typeof this.no !== 'undefined' ? this.no : 0;
  811. var url = typeof this.url !== 'undefined' ? this.url : '';
  812. var confirm = '';
  813. var disable = false;
  814. if (typeof this.confirm !== "undefined") {
  815. confirm = typeof this.confirm === "function" ? this.confirm.call(this, value, row, index) : this.confirm;
  816. }
  817. if (typeof this.disable !== "undefined") {
  818. disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable;
  819. }
  820. return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change " + (disable ? 'btn disabled no-padding' : '') + "' data-index='" + index + "' data-id='"
  821. + 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>";
  822. },
  823. url: function (value, row, index) {
  824. value = value == null || value.length === 0 ? '' : value.toString();
  825. 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>';
  826. },
  827. search: function (value, row, index) {
  828. var field = this.field;
  829. if (typeof this.customField !== 'undefined') {
  830. var customValue = this.customField.split('.').reduce(function (obj, key) {
  831. return obj === null || obj === undefined ? '' : obj[key];
  832. }, row);
  833. value = Fast.api.escape(customValue);
  834. field = this.customField;
  835. }
  836. return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', value) + '" data-field="' + field + '" data-value="' + value + '">' + value + '</a>';
  837. },
  838. addtabs: function (value, row, index) {
  839. var url = Table.api.replaceurl(this.url || '', row, this.table);
  840. var title = this.atitle ? this.atitle : __("Search %s", value);
  841. return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  842. },
  843. dialog: function (value, row, index) {
  844. var url = Table.api.replaceurl(this.url || '', row, this.table);
  845. var title = this.atitle ? this.atitle : __("View %s", value);
  846. return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  847. },
  848. flag: function (value, row, index) {
  849. var that = this;
  850. value = value == null || value.length === 0 ? '' : value.toString();
  851. var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
  852. //如果字段列有定义custom
  853. if (typeof this.custom !== 'undefined') {
  854. colorArr = $.extend(colorArr, this.custom);
  855. }
  856. var field = this.field;
  857. if (typeof this.customField !== 'undefined') {
  858. var customValue = this.customField.split('.').reduce(function (obj, key) {
  859. return obj === null || obj === undefined ? '' : obj[key];
  860. }, row);
  861. value = Fast.api.escape(customValue);
  862. field = this.customField;
  863. }
  864. if (typeof that.searchList === 'object' && typeof that.custom === 'undefined') {
  865. var i = 0;
  866. var searchValues = Object.keys(colorArr).map(function (e) {
  867. return colorArr[e];
  868. });
  869. $.each(that.searchList, function (key, val) {
  870. if (typeof colorArr[key] == 'undefined') {
  871. colorArr[key] = searchValues[i];
  872. i = typeof searchValues[i + 1] === 'undefined' ? 0 : i + 1;
  873. }
  874. });
  875. }
  876. //渲染Flag
  877. var html = [];
  878. var arr = value !== '' ? value.split(',') : [];
  879. var color, display, label;
  880. $.each(arr, function (i, value) {
  881. value = value == null || value.length === 0 ? '' : value.toString();
  882. if (value === '')
  883. return true;
  884. color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
  885. display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
  886. label = '<span class="label label-' + color + '">' + display + '</span>';
  887. if (that.operate) {
  888. html.push('<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + field + '" data-value="' + value + '">' + label + '</a>');
  889. } else {
  890. html.push(label);
  891. }
  892. });
  893. return html.join(' ');
  894. },
  895. label: function (value, row, index) {
  896. return Table.api.formatter.flag.call(this, value, row, index);
  897. },
  898. datetime: function (value, row, index) {
  899. var datetimeFormat = typeof this.datetimeFormat === 'undefined' ? 'YYYY-MM-DD HH:mm:ss' : this.datetimeFormat;
  900. if (isNaN(value)) {
  901. return value ? Moment(value).format(datetimeFormat) : __('None');
  902. } else {
  903. return value ? Moment(parseInt(value) * 1000).format(datetimeFormat) : __('None');
  904. }
  905. },
  906. operate: function (value, row, index) {
  907. var table = this.table;
  908. // 操作配置
  909. var options = table ? table.bootstrapTable('getOptions') : {};
  910. var buttons = [];
  911. var existBtn = [];
  912. var defaultBtn = ['dragsort', 'edit', 'del'];
  913. var tempButton = $.extend({}, Table.button, {});
  914. (this.buttons || []).forEach(function (item, index) {
  915. if (defaultBtn.indexOf(item.name) > -1) {
  916. $.extend(tempButton[item.name], item, Table.button[item.name], item.name === 'edit' ? {url: options.extend.edit_url} : {});
  917. if (item.keep) {
  918. if (options.extend[item.name + "_url"] !== '') {
  919. buttons.push(tempButton[item.name]);
  920. }
  921. existBtn.push(item.name);
  922. }
  923. } else {
  924. buttons.push(item);
  925. }
  926. });
  927. defaultBtn.forEach(function (value, index) {
  928. if (existBtn.indexOf(value) === -1) {
  929. buttons.push(tempButton[value]);
  930. }
  931. });
  932. return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
  933. }
  934. ,
  935. buttons: function (value, row, index) {
  936. // 默认按钮组
  937. var buttons = $.extend([], this.buttons || []);
  938. return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
  939. }
  940. },
  941. buttonlink: function (column, buttons, value, row, index, type) {
  942. var table = column.table;
  943. column.clickToSelect = false;
  944. type = typeof type === 'undefined' ? 'buttons' : type;
  945. var options = table ? table.bootstrapTable('getOptions') : {};
  946. var html = [];
  947. var hidden, visible, disable, url, classname, icon, text, title, refresh, confirm, extend,
  948. dropdown, link;
  949. var fieldIndex = column.fieldIndex;
  950. var dropdowns = {};
  951. $.each(buttons, function (i, j) {
  952. if (type === 'operate') {
  953. if (j.name === 'dragsort' && typeof row[Table.config.dragsortfield] === 'undefined') {
  954. return true;
  955. }
  956. if (['add', 'edit', 'del', 'multi', 'dragsort'].indexOf(j.name) > -1 && !options.extend[j.name + "_url"]) {
  957. return true;
  958. }
  959. }
  960. var attr = table.data(type + "-" + j.name);
  961. if (typeof attr === 'undefined' || attr) {
  962. hidden = typeof j.hidden === 'function' ? j.hidden.call(table, row, j) : (typeof j.hidden !== 'undefined' ? j.hidden : false);
  963. if (hidden) {
  964. return true;
  965. }
  966. visible = typeof j.visible === 'function' ? j.visible.call(table, row, j) : (typeof j.visible !== 'undefined' ? j.visible : true);
  967. if (!visible) {
  968. return true;
  969. }
  970. dropdown = j.dropdown ? j.dropdown : '';
  971. url = j.url ? j.url : '';
  972. url = typeof url === 'function' ? url.call(table, row, j) : (url ? Fast.api.fixurl(Table.api.replaceurl(url, row, table)) : 'javascript:;');
  973. classname = j.classname ? j.classname : (dropdown ? 'btn-' + name + 'one' : 'btn-primary btn-' + name + 'one');
  974. icon = j.icon ? j.icon : '';
  975. text = typeof j.text === 'function' ? j.text.call(table, row, j) : j.text ? j.text : '';
  976. title = typeof j.title === 'function' ? j.title.call(table, row, j) : j.title ? j.title : text;
  977. refresh = j.refresh ? 'data-refresh="' + j.refresh + '"' : '';
  978. confirm = typeof j.confirm === 'function' ? j.confirm.call(table, row, j) : (typeof j.confirm !== 'undefined' ? j.confirm : false);
  979. confirm = confirm ? 'data-confirm="' + confirm + '"' : '';
  980. extend = typeof j.extend === 'function' ? j.extend.call(table, row, j) : (typeof j.extend !== 'undefined' ? j.extend : '');
  981. disable = typeof j.disable === 'function' ? j.disable.call(table, row, j) : (typeof j.disable !== 'undefined' ? j.disable : false);
  982. if (disable) {
  983. classname = classname + ' disabled';
  984. }
  985. 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>';
  986. if (dropdown) {
  987. if (typeof dropdowns[dropdown] == 'undefined') {
  988. dropdowns[dropdown] = [];
  989. }
  990. dropdowns[dropdown].push(link);
  991. } else {
  992. html.push(link);
  993. }
  994. }
  995. });
  996. if (!$.isEmptyObject(dropdowns)) {
  997. var dropdownHtml = [];
  998. $.each(dropdowns, function (i, j) {
  999. 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>');
  1000. });
  1001. html.unshift(dropdownHtml.join(' '));
  1002. }
  1003. return html.join(' ');
  1004. },
  1005. //替换URL中的数据
  1006. replaceurl: function (url, row, table) {
  1007. var options = table ? table.bootstrapTable('getOptions') : null;
  1008. var ids = options ? row[options.pk] : 0;
  1009. row.ids = ids ? ids : (typeof row.ids !== 'undefined' ? row.ids : 0);
  1010. url = url == null || url.length === 0 ? '' : url.toString();
  1011. //自动添加ids参数
  1012. url = !url.match(/(?=([?&]ids=)|(\/ids\/)|(\{ids}))/i) ?
  1013. url + (url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + '{ids}' : url;
  1014. url = url.replace(/\{(.*?)\}/gi, function (matched) {
  1015. matched = matched.substring(1, matched.length - 1);
  1016. var temp = matched.split('.').reduce(function (obj, key) {
  1017. return obj === null || obj === undefined ? '' : obj[key];
  1018. }, row);
  1019. temp = Fast.api.escape(temp);
  1020. return temp;
  1021. });
  1022. return url;
  1023. },
  1024. // 获取选中的条目ID集合
  1025. selectedids: function (table, current) {
  1026. var options = table.bootstrapTable('getOptions');
  1027. //如果有设置翻页记忆模式
  1028. if (!current && options.maintainSelected) {
  1029. return options.selectedIds;
  1030. }
  1031. return $.map(table.bootstrapTable('getSelections'), function (row) {
  1032. return row[options.pk];
  1033. });
  1034. },
  1035. //获取选中的数据
  1036. selecteddata: function (table, current) {
  1037. var options = table.bootstrapTable('getOptions');
  1038. //如果有设置翻页记忆模式
  1039. if (!current && options.maintainSelected) {
  1040. return options.selectedData;
  1041. }
  1042. return table.bootstrapTable('getSelections');
  1043. },
  1044. // 切换复选框状态
  1045. toggleattr: function (table) {
  1046. $("input[type='checkbox']", table).trigger('click');
  1047. },
  1048. // 根据行索引获取行数据
  1049. getrowdata: function (table, index) {
  1050. index = parseInt(index);
  1051. var data = table.bootstrapTable('getData');
  1052. return typeof data[index] !== 'undefined' ? data[index] : null;
  1053. },
  1054. // 根据行索引获取行数据
  1055. getrowbyindex: function (table, index) {
  1056. return Table.api.getrowdata(table, index);
  1057. },
  1058. // 根据主键ID获取行数据
  1059. getrowbyid: function (table, id) {
  1060. var row = {};
  1061. var options = table.bootstrapTable("getOptions");
  1062. $.each(Table.api.selecteddata(table), function (i, j) {
  1063. if (j[options.pk] == id) {
  1064. row = j;
  1065. return false;
  1066. }
  1067. });
  1068. return row;
  1069. }
  1070. },
  1071. };
  1072. return Table;
  1073. });