require-table.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. // 绑定TAB事件
  162. $('.panel-heading ul[data-field] li a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  163. var field = $(this).closest("ul").data("field");
  164. var value = $(this).data("value");
  165. $("select[name='" + field + "'] option[value='" + value + "']", table.closest(".bootstrap-table").find(".commonsearch-table")).prop("selected", true);
  166. table.bootstrapTable('refresh', {});
  167. return false;
  168. });
  169. // 刷新按钮事件
  170. $(toolbar).on('click', Table.config.refreshbtn, function () {
  171. table.bootstrapTable('refresh');
  172. });
  173. // 添加按钮事件
  174. $(toolbar).on('click', Table.config.addbtn, function () {
  175. var ids = Table.api.selectedids(table);
  176. var url = options.extend.add_url;
  177. if (url.indexOf("{ids}") !== -1) {
  178. url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
  179. }
  180. Fast.api.open(url, __('Add'), $(this).data() || {});
  181. });
  182. // 导入按钮事件
  183. if ($(Table.config.importbtn, toolbar).size() > 0) {
  184. require(['upload'], function (Upload) {
  185. Upload.api.plupload($(Table.config.importbtn, toolbar), function (data, ret) {
  186. Fast.api.ajax({
  187. url: options.extend.import_url,
  188. data: {file: data.url},
  189. }, function (data, ret) {
  190. table.bootstrapTable('refresh');
  191. });
  192. });
  193. });
  194. }
  195. // 批量编辑按钮事件
  196. $(toolbar).on('click', Table.config.editbtn, function () {
  197. var that = this;
  198. //循环弹出多个编辑框
  199. $.each(table.bootstrapTable('getSelections'), function (index, row) {
  200. var url = options.extend.edit_url;
  201. row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
  202. var url = Table.api.replaceurl(url, row, table);
  203. Fast.api.open(url, __('Edit'), $(that).data() || {});
  204. });
  205. });
  206. // 批量操作按钮事件
  207. $(toolbar).on('click', Table.config.multibtn, function () {
  208. var ids = Table.api.selectedids(table);
  209. Table.api.multi($(this).data("action"), ids, table, this);
  210. });
  211. // 批量删除按钮事件
  212. $(toolbar).on('click', Table.config.delbtn, function () {
  213. var that = this;
  214. var ids = Table.api.selectedids(table);
  215. Layer.confirm(
  216. __('Are you sure you want to delete the %s selected item?', ids.length),
  217. {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
  218. function (index) {
  219. Table.api.multi("del", ids, table, that);
  220. Layer.close(index);
  221. }
  222. );
  223. });
  224. // 拖拽排序
  225. require(['dragsort'], function () {
  226. //绑定拖动排序
  227. $("tbody", table).dragsort({
  228. itemSelector: 'tr:visible',
  229. dragSelector: "a.btn-dragsort",
  230. dragEnd: function (a, b) {
  231. var element = $("a.btn-dragsort", this);
  232. var data = table.bootstrapTable('getData');
  233. var current = data[parseInt($(this).data("index"))];
  234. var options = table.bootstrapTable('getOptions');
  235. //改变的值和改变的ID集合
  236. var ids = $.map($("tbody tr:visible", table), function (tr) {
  237. return data[parseInt($(tr).data("index"))][options.pk];
  238. });
  239. var changeid = current[options.pk];
  240. var pid = typeof current.pid != 'undefined' ? current.pid : '';
  241. var params = {
  242. url: table.bootstrapTable('getOptions').extend.dragsort_url,
  243. data: {
  244. ids: ids.join(','),
  245. changeid: changeid,
  246. pid: pid,
  247. field: Table.config.dragsortfield,
  248. orderway: options.sortOrder,
  249. table: options.extend.table
  250. }
  251. };
  252. Fast.api.ajax(params, function (data, ret) {
  253. var success = $(element).data("success") || $.noop;
  254. if (typeof success === 'function') {
  255. if (false === success.call(element, data, ret)) {
  256. return false;
  257. }
  258. }
  259. table.bootstrapTable('refresh');
  260. }, function (data, ret) {
  261. var error = $(element).data("error") || $.noop;
  262. if (typeof error === 'function') {
  263. if (false === error.call(element, data, ret)) {
  264. return false;
  265. }
  266. }
  267. table.bootstrapTable('refresh');
  268. });
  269. },
  270. placeHolderTemplate: ""
  271. });
  272. });
  273. $(table).on("click", "input[data-id][name='checkbox']", function (e) {
  274. var ids = $(this).data("id");
  275. var row = Table.api.getrowbyid(table, ids);
  276. table.trigger('check.bs.table', [row, this]);
  277. });
  278. $(table).on("click", "[data-id].btn-change", function (e) {
  279. e.preventDefault();
  280. Table.api.multi($(this).data("action") ? $(this).data("action") : '', [$(this).data("id")], table, this);
  281. });
  282. $(table).on("click", "[data-id].btn-edit", function (e) {
  283. e.preventDefault();
  284. var ids = $(this).data("id");
  285. var row = Table.api.getrowbyid(table, ids);
  286. row.ids = ids;
  287. var url = Table.api.replaceurl(options.extend.edit_url, row, table);
  288. Fast.api.open(url, __('Edit'), $(this).data() || {});
  289. });
  290. $(table).on("click", "[data-id].btn-del", function (e) {
  291. e.preventDefault();
  292. var id = $(this).data("id");
  293. var that = this;
  294. Layer.confirm(
  295. __('Are you sure you want to delete this item?'),
  296. {icon: 3, title: __('Warning'), shadeClose: true},
  297. function (index) {
  298. Table.api.multi("del", id, table, that);
  299. Layer.close(index);
  300. }
  301. );
  302. });
  303. var id = table.attr("id");
  304. Table.list[id] = table;
  305. return table;
  306. },
  307. // 批量操作请求
  308. multi: function (action, ids, table, element) {
  309. var options = table.bootstrapTable('getOptions');
  310. var data = element ? $(element).data() : {};
  311. var ids = ($.isArray(ids) ? ids.join(",") : ids);
  312. var url = typeof data.url !== "undefined" ? data.url : (action == "del" ? options.extend.del_url : options.extend.multi_url);
  313. url = this.replaceurl(url, {ids: ids}, table);
  314. var params = typeof data.params !== "undefined" ? (typeof data.params == 'object' ? $.param(data.params) : data.params) : '';
  315. var options = {url: url, data: {action: action, ids: ids, params: params}};
  316. Fast.api.ajax(options, function (data, ret) {
  317. var success = $(element).data("success") || $.noop;
  318. if (typeof success === 'function') {
  319. if (false === success.call(element, data, ret)) {
  320. return false;
  321. }
  322. }
  323. table.bootstrapTable('refresh');
  324. }, function (data, ret) {
  325. var error = $(element).data("error") || $.noop;
  326. if (typeof error === 'function') {
  327. if (false === error.call(element, data, ret)) {
  328. return false;
  329. }
  330. }
  331. });
  332. },
  333. // 单元格元素事件
  334. events: {
  335. operate: {
  336. 'click .btn-editone': function (e, value, row, index) {
  337. e.stopPropagation();
  338. e.preventDefault();
  339. var table = $(this).closest('table');
  340. var options = table.bootstrapTable('getOptions');
  341. var ids = row[options.pk];
  342. row = $.extend({}, row ? row : {}, {ids: ids});
  343. var url = options.extend.edit_url;
  344. Fast.api.open(Table.api.replaceurl(url, row, table), __('Edit'), $(this).data() || {});
  345. },
  346. 'click .btn-delone': function (e, value, row, index) {
  347. e.stopPropagation();
  348. e.preventDefault();
  349. var that = this;
  350. var top = $(that).offset().top - $(window).scrollTop();
  351. var left = $(that).offset().left - $(window).scrollLeft() - 260;
  352. if (top + 154 > $(window).height()) {
  353. top = top - 154;
  354. }
  355. if ($(window).width() < 480) {
  356. top = left = undefined;
  357. }
  358. Layer.confirm(
  359. __('Are you sure you want to delete this item?'),
  360. {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
  361. function (index) {
  362. var table = $(that).closest('table');
  363. var options = table.bootstrapTable('getOptions');
  364. Table.api.multi("del", row[options.pk], table, that);
  365. Layer.close(index);
  366. }
  367. );
  368. }
  369. }
  370. },
  371. // 单元格数据格式化
  372. formatter: {
  373. icon: function (value, row, index) {
  374. if (!value)
  375. return '';
  376. value = value === null ? '' : value.toString();
  377. value = value.indexOf(" ") > -1 ? value : "fa fa-" + value;
  378. //渲染fontawesome图标
  379. return '<i class="' + value + '"></i> ' + value;
  380. },
  381. image: function (value, row, index) {
  382. value = value ? value : '/assets/img/blank.gif';
  383. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  384. return '<a href="' + Fast.api.cdnurl(value) + '" target="_blank"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>';
  385. },
  386. images: function (value, row, index) {
  387. value = value === null ? '' : value.toString();
  388. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  389. var arr = value.split(',');
  390. var html = [];
  391. $.each(arr, function (i, value) {
  392. value = value ? value : '/assets/img/blank.gif';
  393. html.push('<a href="' + Fast.api.cdnurl(value) + '" target="_blank"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>');
  394. });
  395. return html.join(' ');
  396. },
  397. status: function (value, row, index) {
  398. var custom = {normal: 'success', hidden: 'gray', deleted: 'danger', locked: 'info'};
  399. if (typeof this.custom !== 'undefined') {
  400. custom = $.extend(custom, this.custom);
  401. }
  402. this.custom = custom;
  403. this.icon = 'fa fa-circle';
  404. return Table.api.formatter.normal.call(this, value, row, index);
  405. },
  406. normal: function (value, row, index) {
  407. var colorArr = ["primary", "success", "danger", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
  408. var custom = {};
  409. if (typeof this.custom !== 'undefined') {
  410. custom = $.extend(custom, this.custom);
  411. }
  412. value = value === null ? '' : value.toString();
  413. var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
  414. var index = keys.indexOf(value);
  415. var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
  416. var display = index > -1 ? this.searchList[value] : null;
  417. var icon = typeof this.icon !== 'undefined' ? this.icon : null;
  418. if (!color) {
  419. color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
  420. }
  421. if (!display) {
  422. display = __(value.charAt(0).toUpperCase() + value.slice(1));
  423. }
  424. var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
  425. if (this.operate != false) {
  426. html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
  427. }
  428. return html;
  429. },
  430. toggle: function (value, row, index) {
  431. var color = typeof this.color !== 'undefined' ? this.color : 'success';
  432. var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
  433. var no = typeof this.no !== 'undefined' ? this.no : 0;
  434. return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change' data-id='"
  435. + row.id + "' data-params='" + this.field + "=" + (value == yes ? no : yes) + "'><i class='fa fa-toggle-on " + (value == yes ? 'text-' + color : 'fa-flip-horizontal text-gray') + " fa-2x'></i></a>";
  436. },
  437. url: function (value, row, index) {
  438. 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>';
  439. },
  440. search: function (value, row, index) {
  441. return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', value) + '" data-field="' + this.field + '" data-value="' + value + '">' + value + '</a>';
  442. },
  443. addtabs: function (value, row, index) {
  444. var url = Table.api.replaceurl(this.url, row, this.table);
  445. var title = this.atitle ? this.atitle : __("Search %s", value);
  446. return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  447. },
  448. dialog: function (value, row, index) {
  449. var url = Table.api.replaceurl(this.url, row, this.table);
  450. var title = this.atitle ? this.atitle : __("View %s", value);
  451. return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  452. },
  453. flag: function (value, row, index) {
  454. var that = this;
  455. value = value === null ? '' : value.toString();
  456. var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
  457. //如果字段列有定义custom
  458. if (typeof this.custom !== 'undefined') {
  459. colorArr = $.extend(colorArr, this.custom);
  460. }
  461. var field = this.field;
  462. if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
  463. value = row[this.customField];
  464. field = this.customField;
  465. }
  466. //渲染Flag
  467. var html = [];
  468. var arr = value.split(',');
  469. $.each(arr, function (i, value) {
  470. value = value === null ? '' : value.toString();
  471. if (value == '')
  472. return true;
  473. var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
  474. var display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
  475. html.push('<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + field + '" data-value="' + value + '"><span class="label label-' + color + '">' + display + '</span></a>');
  476. });
  477. return html.join(' ');
  478. },
  479. label: function (value, row, index) {
  480. return Table.api.formatter.flag.call(this, value, row, index);
  481. },
  482. datetime: function (value, row, index) {
  483. var datetimeFormat = typeof this.datetimeFormat === 'undefined' ? 'YYYY-MM-DD HH:mm:ss' : this.datetimeFormat;
  484. if (isNaN(value)) {
  485. return value ? Moment(value).format(datetimeFormat) : __('None');
  486. } else {
  487. return value ? Moment(parseInt(value) * 1000).format(datetimeFormat) : __('None');
  488. }
  489. },
  490. operate: function (value, row, index) {
  491. var table = this.table;
  492. // 操作配置
  493. var options = table ? table.bootstrapTable('getOptions') : {};
  494. // 默认按钮组
  495. var buttons = $.extend([], this.buttons || []);
  496. // 所有按钮名称
  497. var names = [];
  498. buttons.forEach(function (item) {
  499. names.push(item.name);
  500. });
  501. if (options.extend.dragsort_url !== '' && names.indexOf('dragsort') === -1) {
  502. buttons.push({
  503. name: 'dragsort',
  504. icon: 'fa fa-arrows',
  505. title: __('Drag to sort'),
  506. extend: 'data-toggle="tooltip"',
  507. classname: 'btn btn-xs btn-primary btn-dragsort'
  508. });
  509. }
  510. if (options.extend.edit_url !== '' && names.indexOf('edit') === -1) {
  511. buttons.push({
  512. name: 'edit',
  513. icon: 'fa fa-pencil',
  514. title: __('Edit'),
  515. extend: 'data-toggle="tooltip"',
  516. classname: 'btn btn-xs btn-success btn-editone',
  517. url: options.extend.edit_url
  518. });
  519. }
  520. if (options.extend.del_url !== '' && names.indexOf('del') === -1) {
  521. buttons.push({
  522. name: 'del',
  523. icon: 'fa fa-trash',
  524. title: __('Del'),
  525. extend: 'data-toggle="tooltip"',
  526. classname: 'btn btn-xs btn-danger btn-delone'
  527. });
  528. }
  529. return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
  530. }
  531. ,
  532. buttons: function (value, row, index) {
  533. // 默认按钮组
  534. var buttons = $.extend([], this.buttons || []);
  535. return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
  536. }
  537. },
  538. buttonlink: function (column, buttons, value, row, index, type) {
  539. var table = column.table;
  540. type = typeof type === 'undefined' ? 'buttons' : type;
  541. var options = table ? table.bootstrapTable('getOptions') : {};
  542. var html = [];
  543. var hidden, visible, disable, url, classname, icon, text, title, refresh, confirm, extend, click, dropdown, link;
  544. var fieldIndex = column.fieldIndex;
  545. var dropdowns = {};
  546. $.each(buttons, function (i, j) {
  547. if (type === 'operate') {
  548. if (j.name === 'dragsort' && typeof row[Table.config.dragsortfield] === 'undefined') {
  549. return true;
  550. }
  551. if (['add', 'edit', 'del', 'multi', 'dragsort'].indexOf(j.name) > -1 && !options.extend[j.name + "_url"]) {
  552. return true;
  553. }
  554. }
  555. var attr = table.data(type + "-" + j.name);
  556. if (typeof attr === 'undefined' || attr) {
  557. hidden = typeof j.hidden === 'function' ? j.hidden.call(table, row, j) : (j.hidden ? j.hidden : false);
  558. if (hidden) {
  559. return true;
  560. }
  561. visible = typeof j.visible === 'function' ? j.visible.call(table, row, j) : (j.visible ? j.visible : true);
  562. if (!visible) {
  563. return true;
  564. }
  565. dropdown = j.dropdown ? j.dropdown : '';
  566. url = j.url ? j.url : '';
  567. url = typeof url === 'function' ? url.call(table, row, j) : (url ? Fast.api.fixurl(Table.api.replaceurl(url, row, table)) : 'javascript:;');
  568. classname = j.classname ? j.classname : 'btn-primary btn-' + name + 'one';
  569. icon = j.icon ? j.icon : '';
  570. text = j.text ? j.text : '';
  571. title = j.title ? j.title : text;
  572. refresh = j.refresh ? 'data-refresh="' + j.refresh + '"' : '';
  573. confirm = j.confirm ? 'data-confirm="' + j.confirm + '"' : '';
  574. extend = j.extend ? j.extend : '';
  575. disable = typeof j.disable === 'function' ? j.disable.call(table, row, j) : (j.disable ? j.disable : false);
  576. if (disable) {
  577. classname = classname + ' disabled';
  578. }
  579. 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>';
  580. if (dropdown) {
  581. if (typeof dropdowns[dropdown] == 'undefined') {
  582. dropdowns[dropdown] = [];
  583. }
  584. dropdowns[dropdown].push(link);
  585. } else {
  586. html.push(link);
  587. }
  588. }
  589. });
  590. if (!$.isEmptyObject(dropdowns)) {
  591. var dropdownHtml = [];
  592. $.each(dropdowns, function (i, j) {
  593. 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 pull-right"><li>' + j.join('</li><li>') + '</li></ul></div>');
  594. });
  595. html.unshift(dropdownHtml);
  596. }
  597. return html.join(' ');
  598. },
  599. //替换URL中的数据
  600. replaceurl: function (url, row, table) {
  601. var options = table ? table.bootstrapTable('getOptions') : null;
  602. var ids = options ? row[options.pk] : 0;
  603. row.ids = ids ? ids : (typeof row.ids !== 'undefined' ? row.ids : 0);
  604. //自动添加ids参数
  605. url = !url.match(/\{ids\}/i) ? url + (url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + '{ids}' : url;
  606. url = url.replace(/\{(.*?)\}/gi, function (matched) {
  607. matched = matched.substring(1, matched.length - 1);
  608. if (matched.indexOf(".") !== -1) {
  609. var temp = row;
  610. var arr = matched.split(/\./);
  611. for (var i = 0; i < arr.length; i++) {
  612. if (typeof temp[arr[i]] !== 'undefined') {
  613. temp = temp[arr[i]];
  614. }
  615. }
  616. return typeof temp === 'object' ? '' : temp;
  617. }
  618. return row[matched];
  619. });
  620. return url;
  621. },
  622. // 获取选中的条目ID集合
  623. selectedids: function (table) {
  624. var options = table.bootstrapTable('getOptions');
  625. if (options.templateView) {
  626. return $.map($("input[data-id][name='checkbox']:checked"), function (dom) {
  627. return $(dom).data("id");
  628. });
  629. } else {
  630. return $.map(table.bootstrapTable('getSelections'), function (row) {
  631. return row[options.pk];
  632. });
  633. }
  634. },
  635. // 切换复选框状态
  636. toggleattr: function (table) {
  637. $("input[type='checkbox']", table).trigger('click');
  638. },
  639. // 根据行索引获取行数据
  640. getrowdata: function (table, index) {
  641. index = parseInt(index);
  642. var data = table.bootstrapTable('getData');
  643. return typeof data[index] !== 'undefined' ? data[index] : null;
  644. },
  645. // 根据行索引获取行数据
  646. getrowbyindex: function (table, index) {
  647. return Table.api.getrowdata(table, index);
  648. },
  649. // 根据主键ID获取行数据
  650. getrowbyid: function (table, id) {
  651. var row = {};
  652. var options = table.bootstrapTable("getOptions");
  653. $.each(table.bootstrapTable('getData'), function (i, j) {
  654. if (j[options.pk] == id) {
  655. row = j;
  656. return false;
  657. }
  658. });
  659. return row;
  660. }
  661. },
  662. };
  663. return Table;
  664. });