require-table.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table', 'bootstrap-table-lang', 'bootstrap-table-export', 'bootstrap-table-commonsearch', 'bootstrap-table-template'], 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: "all",
  18. exportTypes: ['json', 'xml', 'csv', 'txt', 'doc', 'excel'],
  19. pageSize: 10,
  20. pageList: [10, 25, 50, 'All'],
  21. pagination: true,
  22. clickToSelect: true, //是否启用点击选中
  23. dblClickToEdit: true, //是否启用双击编辑
  24. singleSelect: false, //是否启用单选
  25. showRefresh: false,
  26. locale: 'zh-CN',
  27. showToggle: true,
  28. showColumns: true,
  29. pk: 'id',
  30. sortName: 'id',
  31. sortOrder: 'desc',
  32. paginationFirstText: __("First"),
  33. paginationPreText: __("Previous"),
  34. paginationNextText: __("Next"),
  35. paginationLastText: __("Last"),
  36. cardView: false, //卡片视图
  37. checkOnInit: true, //是否在初始化时判断
  38. escape: true, //是否对内容进行转义
  39. extend: {
  40. index_url: '',
  41. add_url: '',
  42. edit_url: '',
  43. del_url: '',
  44. import_url: '',
  45. multi_url: '',
  46. dragsort_url: 'ajax/weigh',
  47. }
  48. },
  49. // Bootstrap-table 列配置
  50. columnDefaults: {
  51. align: 'center',
  52. valign: 'middle',
  53. },
  54. config: {
  55. firsttd: 'tbody tr td:first-child:not(:has(div.card-views))',
  56. toolbar: '.toolbar',
  57. refreshbtn: '.btn-refresh',
  58. addbtn: '.btn-add',
  59. editbtn: '.btn-edit',
  60. delbtn: '.btn-del',
  61. importbtn: '.btn-import',
  62. multibtn: '.btn-multi',
  63. disabledbtn: '.btn-disabled',
  64. editonebtn: '.btn-editone',
  65. dragsortfield: 'weigh',
  66. },
  67. api: {
  68. init: function (defaults, columnDefaults, locales) {
  69. defaults = defaults ? defaults : {};
  70. columnDefaults = columnDefaults ? columnDefaults : {};
  71. locales = locales ? locales : {};
  72. // 如果是iOS设备则启用卡片视图
  73. if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
  74. Table.defaults.cardView = true;
  75. }
  76. // 写入bootstrap-table默认配置
  77. $.extend(true, $.fn.bootstrapTable.defaults, Table.defaults, defaults);
  78. // 写入bootstrap-table column配置
  79. $.extend($.fn.bootstrapTable.columnDefaults, Table.columnDefaults, columnDefaults);
  80. // 写入bootstrap-table locale配置
  81. $.extend($.fn.bootstrapTable.locales[Table.defaults.locale], {
  82. formatCommonSearch: function () {
  83. return __('Common search');
  84. },
  85. formatCommonSubmitButton: function () {
  86. return __('Submit');
  87. },
  88. formatCommonResetButton: function () {
  89. return __('Reset');
  90. },
  91. formatCommonCloseButton: function () {
  92. return __('Close');
  93. },
  94. formatCommonChoose: function () {
  95. return __('Choose');
  96. }
  97. }, locales);
  98. },
  99. // 绑定事件
  100. bindevent: function (table) {
  101. //Bootstrap-table的父元素,包含table,toolbar,pagnation
  102. var parenttable = table.closest('.bootstrap-table');
  103. //Bootstrap-table配置
  104. var options = table.bootstrapTable('getOptions');
  105. //Bootstrap操作区
  106. var toolbar = $(options.toolbar, parenttable);
  107. //当刷新表格时
  108. table.on('load-error.bs.table', function (status, res, e) {
  109. if (e.status === 0) {
  110. return;
  111. }
  112. Toastr.error(__('Unknown data format'));
  113. });
  114. //当刷新表格时
  115. table.on('refresh.bs.table', function (e, settings, data) {
  116. $(Table.config.refreshbtn, toolbar).find(".fa").addClass("fa-spin");
  117. });
  118. if (options.dblClickToEdit) {
  119. //当双击单元格时
  120. table.on('dbl-click-row.bs.table', function (e, row, element, field) {
  121. $(Table.config.editonebtn, element).trigger("click");
  122. });
  123. }
  124. //当内容渲染完成后
  125. table.on('post-body.bs.table', function (e, settings, json, xhr) {
  126. $(Table.config.refreshbtn, toolbar).find(".fa").removeClass("fa-spin");
  127. $(Table.config.disabledbtn, toolbar).toggleClass('disabled', true);
  128. if ($(Table.config.firsttd, table).find("input[type='checkbox'][data-index]").size() > 0) {
  129. // 挺拽选择,需要重新绑定事件
  130. require(['drag', 'drop'], function () {
  131. $(Table.config.firsttd, table).drag("start", function (ev, dd) {
  132. return $('<div class="selection" />').css('opacity', .65).appendTo(document.body);
  133. }).drag(function (ev, dd) {
  134. $(dd.proxy).css({
  135. top: Math.min(ev.pageY, dd.startY),
  136. left: Math.min(ev.pageX, dd.startX),
  137. height: Math.abs(ev.pageY - dd.startY),
  138. width: Math.abs(ev.pageX - dd.startX)
  139. });
  140. }).drag("end", function (ev, dd) {
  141. $(dd.proxy).remove();
  142. });
  143. $(Table.config.firsttd, table).drop("start", function () {
  144. Table.api.toggleattr(this);
  145. }).drop(function () {
  146. Table.api.toggleattr(this);
  147. }).drop("end", function () {
  148. Table.api.toggleattr(this);
  149. });
  150. $.drop({
  151. multi: true
  152. });
  153. });
  154. }
  155. });
  156. // 处理选中筛选框后按钮的状态统一变更
  157. table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
  158. var ids = Table.api.selectedids(table);
  159. $(Table.config.disabledbtn, toolbar).toggleClass('disabled', !ids.length);
  160. });
  161. // 刷新按钮事件
  162. $(toolbar).on('click', Table.config.refreshbtn, function () {
  163. table.bootstrapTable('refresh');
  164. });
  165. // 添加按钮事件
  166. $(toolbar).on('click', Table.config.addbtn, function () {
  167. var ids = Table.api.selectedids(table);
  168. var url = options.extend.add_url;
  169. if (url.indexOf("{ids}") !== -1) {
  170. url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
  171. }
  172. Fast.api.open(url, __('Add'), $(this).data() || {});
  173. });
  174. // 导入按钮事件
  175. if ($(Table.config.importbtn, toolbar).size() > 0) {
  176. require(['upload'], function (Upload) {
  177. Upload.api.plupload($(Table.config.importbtn, toolbar), function (data, ret) {
  178. Fast.api.ajax({
  179. url: options.extend.import_url,
  180. data: {file: data.url},
  181. }, function (data, ret) {
  182. table.bootstrapTable('refresh');
  183. });
  184. });
  185. });
  186. }
  187. // 批量编辑按钮事件
  188. $(toolbar).on('click', Table.config.editbtn, function () {
  189. var that = this;
  190. //循环弹出多个编辑框
  191. $.each(table.bootstrapTable('getSelections'), function (index, row) {
  192. var url = options.extend.edit_url;
  193. row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
  194. var url = Table.api.replaceurl(url, row, table);
  195. Fast.api.open(url, __('Edit'), $(that).data() || {});
  196. });
  197. });
  198. // 批量操作按钮事件
  199. $(toolbar).on('click', Table.config.multibtn, function () {
  200. var ids = Table.api.selectedids(table);
  201. Table.api.multi($(this).data("action"), ids, table, this);
  202. });
  203. // 批量删除按钮事件
  204. $(toolbar).on('click', Table.config.delbtn, function () {
  205. var that = this;
  206. var ids = Table.api.selectedids(table);
  207. Layer.confirm(
  208. __('Are you sure you want to delete the %s selected item?', ids.length),
  209. {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
  210. function (index) {
  211. Table.api.multi("del", ids, table, that);
  212. Layer.close(index);
  213. }
  214. );
  215. });
  216. // 拖拽排序
  217. require(['dragsort'], function () {
  218. //绑定拖动排序
  219. $("tbody", table).dragsort({
  220. itemSelector: 'tr:visible',
  221. dragSelector: "a.btn-dragsort",
  222. dragEnd: function (a, b) {
  223. var element = $("a.btn-dragsort", this);
  224. var data = table.bootstrapTable('getData');
  225. var current = data[parseInt($(this).data("index"))];
  226. var options = table.bootstrapTable('getOptions');
  227. //改变的值和改变的ID集合
  228. var ids = $.map($("tbody tr:visible", table), function (tr) {
  229. return data[parseInt($(tr).data("index"))][options.pk];
  230. });
  231. var changeid = current[options.pk];
  232. var pid = typeof current.pid != 'undefined' ? current.pid : '';
  233. var params = {
  234. url: table.bootstrapTable('getOptions').extend.dragsort_url,
  235. data: {
  236. ids: ids.join(','),
  237. changeid: changeid,
  238. pid: pid,
  239. field: Table.config.dragsortfield,
  240. orderway: options.sortOrder,
  241. table: options.extend.table
  242. }
  243. };
  244. Fast.api.ajax(params, function (data, ret) {
  245. var success = $(element).data("success") || $.noop;
  246. if (typeof success === 'function') {
  247. if (false === success.call(element, data, ret)) {
  248. return false;
  249. }
  250. }
  251. table.bootstrapTable('refresh');
  252. }, function () {
  253. var error = $(element).data("error") || $.noop;
  254. if (typeof error === 'function') {
  255. if (false === error.call(element, data, ret)) {
  256. return false;
  257. }
  258. }
  259. table.bootstrapTable('refresh');
  260. });
  261. },
  262. placeHolderTemplate: ""
  263. });
  264. });
  265. $(table).on("click", "input[data-id][name='checkbox']", function (e) {
  266. var ids = $(this).data("id");
  267. var row = Table.api.getrowbyid(table, ids);
  268. table.trigger('check.bs.table', [row, this]);
  269. });
  270. $(table).on("click", "[data-id].btn-change", function (e) {
  271. e.preventDefault();
  272. Table.api.multi($(this).data("action") ? $(this).data("action") : '', [$(this).data("id")], table, this);
  273. });
  274. $(table).on("click", "[data-id].btn-edit", function (e) {
  275. e.preventDefault();
  276. var ids = $(this).data("id");
  277. var row = Table.api.getrowbyid(table, ids);
  278. row.ids = ids;
  279. var url = Table.api.replaceurl(options.extend.edit_url, row, table);
  280. Fast.api.open(url, __('Edit'), $(this).data() || {});
  281. });
  282. $(table).on("click", "[data-id].btn-del", function (e) {
  283. e.preventDefault();
  284. var id = $(this).data("id");
  285. var that = this;
  286. Layer.confirm(
  287. __('Are you sure you want to delete this item?'),
  288. {icon: 3, title: __('Warning'), shadeClose: true},
  289. function (index) {
  290. Table.api.multi("del", id, table, that);
  291. Layer.close(index);
  292. }
  293. );
  294. });
  295. var id = table.attr("id");
  296. Table.list[id] = table;
  297. return table;
  298. },
  299. // 批量操作请求
  300. multi: function (action, ids, table, element) {
  301. var options = table.bootstrapTable('getOptions');
  302. var data = element ? $(element).data() : {};
  303. var ids = ($.isArray(ids) ? ids.join(",") : ids);
  304. var url = typeof data.url !== "undefined" ? data.url : (action == "del" ? options.extend.del_url : options.extend.multi_url);
  305. url = this.replaceurl(url, {ids: ids}, table);
  306. var params = typeof data.params !== "undefined" ? (typeof data.params == 'object' ? $.param(data.params) : data.params) : '';
  307. var options = {url: url, data: {action: action, ids: ids, params: params}};
  308. Fast.api.ajax(options, function (data, ret) {
  309. var success = $(element).data("success") || $.noop;
  310. if (typeof success === 'function') {
  311. if (false === success.call(element, data, ret)) {
  312. return false;
  313. }
  314. }
  315. table.bootstrapTable('refresh');
  316. }, function (data, ret) {
  317. var error = $(element).data("error") || $.noop;
  318. if (typeof error === 'function') {
  319. if (false === error.call(element, data, ret)) {
  320. return false;
  321. }
  322. }
  323. });
  324. },
  325. // 单元格元素事件
  326. events: {
  327. operate: {
  328. 'click .btn-editone': function (e, value, row, index) {
  329. e.stopPropagation();
  330. e.preventDefault();
  331. var table = $(this).closest('table');
  332. var options = table.bootstrapTable('getOptions');
  333. var ids = row[options.pk];
  334. row = $.extend({}, row ? row : {}, {ids: ids});
  335. var url = options.extend.edit_url;
  336. Fast.api.open(Table.api.replaceurl(url, row, table), __('Edit'), $(this).data() || {});
  337. },
  338. 'click .btn-delone': function (e, value, row, index) {
  339. e.stopPropagation();
  340. e.preventDefault();
  341. var that = this;
  342. var top = $(that).offset().top - $(window).scrollTop();
  343. var left = $(that).offset().left - $(window).scrollLeft() - 260;
  344. if (top + 154 > $(window).height()) {
  345. top = top - 154;
  346. }
  347. if ($(window).width() < 480) {
  348. top = left = undefined;
  349. }
  350. Layer.confirm(
  351. __('Are you sure you want to delete this item?'),
  352. {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
  353. function (index) {
  354. var table = $(that).closest('table');
  355. var options = table.bootstrapTable('getOptions');
  356. Table.api.multi("del", row[options.pk], table, that);
  357. Layer.close(index);
  358. }
  359. );
  360. }
  361. }
  362. },
  363. // 单元格数据格式化
  364. formatter: {
  365. icon: function (value, row, index) {
  366. if (!value)
  367. return '';
  368. value = value.indexOf(" ") > -1 ? value : "fa fa-" + value;
  369. //渲染fontawesome图标
  370. return '<i class="' + value + '"></i> ' + value;
  371. },
  372. image: function (value, row, index) {
  373. value = value ? value : '/assets/img/blank.gif';
  374. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  375. return '<img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" />';
  376. },
  377. images: function (value, row, index) {
  378. value = value === null ? '' : value.toString();
  379. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  380. var arr = value.split(',');
  381. var html = [];
  382. $.each(arr, function (i, value) {
  383. value = value ? value : '/assets/img/blank.gif';
  384. html.push('<img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" />');
  385. });
  386. return html.join(' ');
  387. },
  388. status: function (value, row, index) {
  389. //颜色状态数组,可使用red/yellow/aqua/blue/navy/teal/olive/lime/fuchsia/purple/maroon
  390. var colorArr = {normal: 'success', hidden: 'grey', deleted: 'danger', locked: 'info'};
  391. //如果字段列有定义custom
  392. if (typeof this.custom !== 'undefined') {
  393. colorArr = $.extend(colorArr, this.custom);
  394. }
  395. value = value === null ? '' : value.toString();
  396. var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
  397. value = value.charAt(0).toUpperCase() + value.slice(1);
  398. //渲染状态
  399. var html = '<span class="text-' + color + '"><i class="fa fa-circle"></i> ' + __(value) + '</span>';
  400. return html;
  401. },
  402. url: function (value, row, index) {
  403. 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>';
  404. },
  405. search: function (value, row, index) {
  406. return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', value) + '" data-field="' + this.field + '" data-value="' + value + '">' + value + '</a>';
  407. },
  408. addtabs: function (value, row, index) {
  409. var url = Table.api.replaceurl(this.url, row, this.table);
  410. var title = this.atitle ? this.atitle : __("Search %s", value);
  411. return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  412. },
  413. dialog: function (value, row, index) {
  414. var url = Table.api.replaceurl(this.url, row, this.table);
  415. var title = this.atitle ? this.atitle : __("View %s", value);
  416. return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  417. },
  418. flag: function (value, row, index) {
  419. value = value === null ? '' : value.toString();
  420. var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
  421. //如果字段列有定义custom
  422. if (typeof this.custom !== 'undefined') {
  423. colorArr = $.extend(colorArr, this.custom);
  424. }
  425. if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
  426. value = row[this.customField];
  427. }
  428. //渲染Flag
  429. var html = [];
  430. var arr = value.split(',');
  431. $.each(arr, function (i, value) {
  432. value = value === null ? '' : value.toString();
  433. if (value == '')
  434. return true;
  435. var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
  436. value = value.charAt(0).toUpperCase() + value.slice(1);
  437. html.push('<span class="label label-' + color + '">' + __(value) + '</span>');
  438. });
  439. return html.join(' ');
  440. },
  441. label: function (value, row, index) {
  442. return Table.api.formatter.flag.call(this, value, row, index);
  443. },
  444. datetime: function (value, row, index) {
  445. return value ? Moment(parseInt(value) * 1000).format("YYYY-MM-DD HH:mm:ss") : __('None');
  446. },
  447. operate: function (value, row, index) {
  448. var table = this.table;
  449. // 操作配置
  450. var options = table ? table.bootstrapTable('getOptions') : {};
  451. // 默认按钮组
  452. var buttons = $.extend([], this.buttons || []);
  453. if (options.extend.dragsort_url !== '') {
  454. buttons.push({
  455. name: 'dragsort',
  456. icon: 'fa fa-arrows',
  457. title: __('Drag to sort'),
  458. extend: 'data-toggle="tooltip"',
  459. classname: 'btn btn-xs btn-primary btn-dragsort'
  460. });
  461. }
  462. if (options.extend.edit_url !== '') {
  463. buttons.push({
  464. name: 'edit',
  465. icon: 'fa fa-pencil',
  466. title: __('Edit'),
  467. extend: 'data-toggle="tooltip"',
  468. classname: 'btn btn-xs btn-success btn-editone',
  469. url: options.extend.edit_url
  470. });
  471. }
  472. if (options.extend.del_url !== '') {
  473. buttons.push({
  474. name: 'del',
  475. icon: 'fa fa-trash',
  476. title: __('Del'),
  477. extend: 'data-toggle="tooltip"',
  478. classname: 'btn btn-xs btn-danger btn-delone'
  479. });
  480. }
  481. return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
  482. },
  483. buttons: function (value, row, index) {
  484. // 默认按钮组
  485. var buttons = $.extend([], this.buttons || []);
  486. return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
  487. }
  488. },
  489. buttonlink: function (column, buttons, value, row, index, type) {
  490. var table = column.table;
  491. type = typeof type === 'undefined' ? 'buttons' : type;
  492. var options = table ? table.bootstrapTable('getOptions') : {};
  493. var html = [];
  494. var hidden, url, classname, icon, text, title, refresh, confirm, extend;
  495. var fieldIndex = column.fieldIndex;
  496. $.each(buttons, function (i, j) {
  497. if (type === 'operate') {
  498. if (j.name === 'dragsort' && typeof row[Table.config.dragsortfield] === 'undefined') {
  499. return true;
  500. }
  501. if (['add', 'edit', 'del', 'multi', 'dragsort'].indexOf(j.name) > -1 && !options.extend[j.name + "_url"]) {
  502. return true;
  503. }
  504. }
  505. var attr = table.data(type + "-" + j.name);
  506. if (typeof attr === 'undefined' || attr) {
  507. hidden = typeof j.hidden === 'function' ? j.hidden.call(table, row, j) : (j.hidden ? j.hidden : false);
  508. if (hidden) {
  509. return true;
  510. }
  511. url = j.url ? j.url : '';
  512. url = typeof url === 'function' ? url.call(table, row, j) : (url ? Fast.api.fixurl(Table.api.replaceurl(url, row, table)) : 'javascript:;');
  513. classname = j.classname ? j.classname : 'btn-primary btn-' + name + 'one';
  514. icon = j.icon ? j.icon : '';
  515. text = j.text ? j.text : '';
  516. title = j.title ? j.title : text;
  517. refresh = j.refresh ? 'data-refresh="' + j.refresh + '"' : '';
  518. confirm = j.confirm ? 'data-confirm="' + j.confirm + '"' : '';
  519. extend = j.extend ? j.extend : '';
  520. html.push('<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>');
  521. }
  522. });
  523. return html.join(' ');
  524. },
  525. //替换URL中的数据
  526. replaceurl: function (url, row, table) {
  527. var options = table ? table.bootstrapTable('getOptions') : null;
  528. var ids = options ? row[options.pk] : 0;
  529. row.ids = ids ? ids : (typeof row.ids !== 'undefined' ? row.ids : 0);
  530. //自动添加ids参数
  531. url = !url.match(/\{ids\}/i) ? url + (url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + '{ids}' : url;
  532. url = url.replace(/\{(.*?)\}/gi, function (matched) {
  533. matched = matched.substring(1, matched.length - 1);
  534. if (matched.indexOf(".") !== -1) {
  535. var temp = row;
  536. var arr = matched.split(/\./);
  537. for (var i = 0; i < arr.length; i++) {
  538. if (typeof temp[arr[i]] !== 'undefined') {
  539. temp = temp[arr[i]];
  540. }
  541. }
  542. return typeof temp === 'object' ? '' : temp;
  543. }
  544. return row[matched];
  545. });
  546. return url;
  547. },
  548. // 获取选中的条目ID集合
  549. selectedids: function (table) {
  550. var options = table.bootstrapTable('getOptions');
  551. if (options.templateView) {
  552. return $.map($("input[data-id][name='checkbox']:checked"), function (dom) {
  553. return $(dom).data("id");
  554. });
  555. } else {
  556. return $.map(table.bootstrapTable('getSelections'), function (row) {
  557. return row[options.pk];
  558. });
  559. }
  560. },
  561. // 切换复选框状态
  562. toggleattr: function (table) {
  563. $("input[type='checkbox']", table).trigger('click');
  564. },
  565. // 根据行索引获取行数据
  566. getrowdata: function (table, index) {
  567. index = parseInt(index);
  568. var data = table.bootstrapTable('getData');
  569. return typeof data[index] !== 'undefined' ? data[index] : null;
  570. },
  571. // 根据行索引获取行数据
  572. getrowbyindex: function (table, index) {
  573. return Table.api.getrowdata(table, index);
  574. },
  575. // 根据主键ID获取行数据
  576. getrowbyid: function (table, id) {
  577. var row = {};
  578. var options = table.bootstrapTable("getOptions");
  579. $.each(table.bootstrapTable('getData'), function (i, j) {
  580. if (j[options.pk] == id) {
  581. row = j;
  582. return false;
  583. }
  584. });
  585. return row;
  586. }
  587. },
  588. };
  589. return Table;
  590. });