require-table.js 58 KB

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