require-table.js 63 KB

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