require-table.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 '<a href="' + Fast.api.cdnurl(value) + '" target="_blank"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>';
  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('<a href="' + Fast.api.cdnurl(value) + '" target="_blank"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>');
  385. });
  386. return html.join(' ');
  387. },
  388. status: function (value, row, index) {
  389. var custom = {normal: 'success', hidden: 'gray', deleted: 'danger', locked: 'info'};
  390. if (typeof this.custom !== 'undefined') {
  391. custom = $.extend(custom, this.custom);
  392. }
  393. this.custom = custom;
  394. this.icon = 'fa fa-circle';
  395. return Table.api.formatter.normal.call(this, value, row, index);
  396. },
  397. normal: function (value, row, index) {
  398. var colorArr = ["primary", "success", "danger", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
  399. var custom = {};
  400. if (typeof this.custom !== 'undefined') {
  401. custom = $.extend(custom, this.custom);
  402. }
  403. var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
  404. var index = keys.indexOf(value);
  405. value = value === null ? '' : value.toString();
  406. var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
  407. var display = index > -1 ? this.searchList[value] : null;
  408. var icon = typeof this.icon !== 'undefined' ? this.icon : null;
  409. if (!color) {
  410. color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
  411. }
  412. if (!display) {
  413. display = value.charAt(0).toUpperCase() + value.slice(1);
  414. }
  415. var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
  416. if (this.operate != false) {
  417. html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
  418. }
  419. return html;
  420. },
  421. toggle: function (value, row, index) {
  422. var color = typeof this.color !== 'undefined' ? this.color : 'success';
  423. var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
  424. var no = typeof this.no !== 'undefined' ? this.no : 0;
  425. return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change' data-id='"
  426. + row.id + "' data-params='" + this.field + "=" + (value ? no : yes) + "'><i class='fa fa-toggle-on " + (value == yes ? 'text-' + color : 'fa-flip-horizontal text-gray') + " fa-2x'></i></a>";
  427. }
  428. ,
  429. url: function (value, row, index) {
  430. 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>';
  431. },
  432. search: function (value, row, index) {
  433. return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', value) + '" data-field="' + this.field + '" data-value="' + value + '">' + value + '</a>';
  434. },
  435. addtabs: function (value, row, index) {
  436. var url = Table.api.replaceurl(this.url, row, this.table);
  437. var title = this.atitle ? this.atitle : __("Search %s", value);
  438. return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  439. },
  440. dialog: function (value, row, index) {
  441. var url = Table.api.replaceurl(this.url, row, this.table);
  442. var title = this.atitle ? this.atitle : __("View %s", value);
  443. return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  444. },
  445. flag: function (value, row, index) {
  446. var that = this;
  447. value = value === null ? '' : value.toString();
  448. var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
  449. //如果字段列有定义custom
  450. if (typeof this.custom !== 'undefined') {
  451. colorArr = $.extend(colorArr, this.custom);
  452. }
  453. var field = this.field;
  454. if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
  455. value = row[this.customField];
  456. field = this.customField;
  457. }
  458. //渲染Flag
  459. var html = [];
  460. var arr = value.split(',');
  461. $.each(arr, function (i, value) {
  462. value = value === null ? '' : value.toString();
  463. if (value == '')
  464. return true;
  465. var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
  466. var display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : value.charAt(0).toUpperCase() + value.slice(1);
  467. 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>');
  468. });
  469. return html.join(' ');
  470. },
  471. label: function (value, row, index) {
  472. return Table.api.formatter.flag.call(this, value, row, index);
  473. },
  474. datetime: function (value, row, index) {
  475. var datetimeFormat = typeof this.datetimeFormat === 'undefined' ? 'YYYY-MM-DD HH:mm:ss' : this.datetimeFormat;
  476. if (isNaN(value)) {
  477. return value ? Moment(value).format(datetimeFormat) : __('None');
  478. } else {
  479. return value ? Moment(parseInt(value) * 1000).format(datetimeFormat) : __('None');
  480. }
  481. },
  482. operate: function (value, row, index) {
  483. var table = this.table;
  484. // 操作配置
  485. var options = table ? table.bootstrapTable('getOptions') : {};
  486. // 默认按钮组
  487. var buttons = $.extend([], this.buttons || []);
  488. if (options.extend.dragsort_url !== '') {
  489. buttons.push({
  490. name: 'dragsort',
  491. icon: 'fa fa-arrows',
  492. title: __('Drag to sort'),
  493. extend: 'data-toggle="tooltip"',
  494. classname: 'btn btn-xs btn-primary btn-dragsort'
  495. });
  496. }
  497. if (options.extend.edit_url !== '') {
  498. buttons.push({
  499. name: 'edit',
  500. icon: 'fa fa-pencil',
  501. title: __('Edit'),
  502. extend: 'data-toggle="tooltip"',
  503. classname: 'btn btn-xs btn-success btn-editone',
  504. url: options.extend.edit_url
  505. });
  506. }
  507. if (options.extend.del_url !== '') {
  508. buttons.push({
  509. name: 'del',
  510. icon: 'fa fa-trash',
  511. title: __('Del'),
  512. extend: 'data-toggle="tooltip"',
  513. classname: 'btn btn-xs btn-danger btn-delone'
  514. });
  515. }
  516. return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
  517. }
  518. ,
  519. buttons: function (value, row, index) {
  520. // 默认按钮组
  521. var buttons = $.extend([], this.buttons || []);
  522. return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
  523. }
  524. },
  525. buttonlink: function (column, buttons, value, row, index, type) {
  526. var table = column.table;
  527. type = typeof type === 'undefined' ? 'buttons' : type;
  528. var options = table ? table.bootstrapTable('getOptions') : {};
  529. var html = [];
  530. var hidden, visible, url, classname, icon, text, title, refresh, confirm, extend;
  531. var fieldIndex = column.fieldIndex;
  532. $.each(buttons, function (i, j) {
  533. if (type === 'operate') {
  534. if (j.name === 'dragsort' && typeof row[Table.config.dragsortfield] === 'undefined') {
  535. return true;
  536. }
  537. if (['add', 'edit', 'del', 'multi', 'dragsort'].indexOf(j.name) > -1 && !options.extend[j.name + "_url"]) {
  538. return true;
  539. }
  540. }
  541. var attr = table.data(type + "-" + j.name);
  542. if (typeof attr === 'undefined' || attr) {
  543. hidden = typeof j.hidden === 'function' ? j.hidden.call(table, row, j) : (j.hidden ? j.hidden : false);
  544. if (hidden) {
  545. return true;
  546. }
  547. visible = typeof j.visible === 'function' ? j.visible.call(table, row, j) : (j.visible ? j.visible : true);
  548. if (!visible) {
  549. return true;
  550. }
  551. url = j.url ? j.url : '';
  552. url = typeof url === 'function' ? url.call(table, row, j) : (url ? Fast.api.fixurl(Table.api.replaceurl(url, row, table)) : 'javascript:;');
  553. classname = j.classname ? j.classname : 'btn-primary btn-' + name + 'one';
  554. icon = j.icon ? j.icon : '';
  555. text = j.text ? j.text : '';
  556. title = j.title ? j.title : text;
  557. refresh = j.refresh ? 'data-refresh="' + j.refresh + '"' : '';
  558. confirm = j.confirm ? 'data-confirm="' + j.confirm + '"' : '';
  559. extend = j.extend ? j.extend : '';
  560. 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>');
  561. }
  562. });
  563. return html.join(' ');
  564. },
  565. //替换URL中的数据
  566. replaceurl: function (url, row, table) {
  567. var options = table ? table.bootstrapTable('getOptions') : null;
  568. var ids = options ? row[options.pk] : 0;
  569. row.ids = ids ? ids : (typeof row.ids !== 'undefined' ? row.ids : 0);
  570. //自动添加ids参数
  571. url = !url.match(/\{ids\}/i) ? url + (url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + '{ids}' : url;
  572. url = url.replace(/\{(.*?)\}/gi, function (matched) {
  573. matched = matched.substring(1, matched.length - 1);
  574. if (matched.indexOf(".") !== -1) {
  575. var temp = row;
  576. var arr = matched.split(/\./);
  577. for (var i = 0; i < arr.length; i++) {
  578. if (typeof temp[arr[i]] !== 'undefined') {
  579. temp = temp[arr[i]];
  580. }
  581. }
  582. return typeof temp === 'object' ? '' : temp;
  583. }
  584. return row[matched];
  585. });
  586. return url;
  587. },
  588. // 获取选中的条目ID集合
  589. selectedids: function (table) {
  590. var options = table.bootstrapTable('getOptions');
  591. if (options.templateView) {
  592. return $.map($("input[data-id][name='checkbox']:checked"), function (dom) {
  593. return $(dom).data("id");
  594. });
  595. } else {
  596. return $.map(table.bootstrapTable('getSelections'), function (row) {
  597. return row[options.pk];
  598. });
  599. }
  600. },
  601. // 切换复选框状态
  602. toggleattr: function (table) {
  603. $("input[type='checkbox']", table).trigger('click');
  604. },
  605. // 根据行索引获取行数据
  606. getrowdata: function (table, index) {
  607. index = parseInt(index);
  608. var data = table.bootstrapTable('getData');
  609. return typeof data[index] !== 'undefined' ? data[index] : null;
  610. },
  611. // 根据行索引获取行数据
  612. getrowbyindex: function (table, index) {
  613. return Table.api.getrowdata(table, index);
  614. },
  615. // 根据主键ID获取行数据
  616. getrowbyid: function (table, id) {
  617. var row = {};
  618. var options = table.bootstrapTable("getOptions");
  619. $.each(table.bootstrapTable('getData'), function (i, j) {
  620. if (j[options.pk] == id) {
  621. row = j;
  622. return false;
  623. }
  624. });
  625. return row;
  626. }
  627. },
  628. };
  629. return Table;
  630. });