You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

766 lines
41 KiB

  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'], 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. exportOptions: {
  20. fileName: 'export_' + Moment().format("YYYY-MM-DD"),
  21. ignoreColumn: [0, 'operate'] //默认不导出第一列(checkbox)与操作(operate)列
  22. },
  23. pageSize: 10,
  24. pageList: [10, 25, 50, 'All'],
  25. pagination: true,
  26. clickToSelect: true, //是否启用点击选中
  27. dblClickToEdit: true, //是否启用双击编辑
  28. singleSelect: false, //是否启用单选
  29. showRefresh: false,
  30. showJumpto: true,
  31. locale: 'zh-CN',
  32. showToggle: true,
  33. showColumns: true,
  34. pk: 'id',
  35. sortName: 'id',
  36. sortOrder: 'desc',
  37. paginationFirstText: __("First"),
  38. paginationPreText: __("Previous"),
  39. paginationNextText: __("Next"),
  40. paginationLastText: __("Last"),
  41. cardView: false, //卡片视图
  42. checkOnInit: true, //是否在初始化时判断
  43. escape: true, //是否对内容进行转义
  44. extend: {
  45. index_url: '',
  46. add_url: '',
  47. edit_url: '',
  48. del_url: '',
  49. import_url: '',
  50. multi_url: '',
  51. dragsort_url: 'ajax/weigh',
  52. }
  53. },
  54. // Bootstrap-table 列配置
  55. columnDefaults: {
  56. align: 'center',
  57. valign: 'middle',
  58. },
  59. config: {
  60. firsttd: 'tbody tr td:first-child:not(:has(div.card-views))',
  61. toolbar: '.toolbar',
  62. refreshbtn: '.btn-refresh',
  63. addbtn: '.btn-add',
  64. editbtn: '.btn-edit',
  65. delbtn: '.btn-del',
  66. importbtn: '.btn-import',
  67. multibtn: '.btn-multi',
  68. disabledbtn: '.btn-disabled',
  69. editonebtn: '.btn-editone',
  70. restoreonebtn: '.btn-restoreone',
  71. destroyonebtn: '.btn-destroyone',
  72. restoreallbtn: '.btn-restoreall',
  73. destroyallbtn: '.btn-destroyall',
  74. dragsortfield: 'weigh',
  75. },
  76. button: {
  77. edit: {
  78. name: 'edit',
  79. icon: 'fa fa-pencil',
  80. title: __('Edit'),
  81. extend: 'data-toggle="tooltip"',
  82. classname: 'btn btn-xs btn-success btn-editone'
  83. },
  84. del: {
  85. name: 'del',
  86. icon: 'fa fa-trash',
  87. title: __('Del'),
  88. extend: 'data-toggle="tooltip"',
  89. classname: 'btn btn-xs btn-danger btn-delone'
  90. },
  91. dragsort: {
  92. name: 'dragsort',
  93. icon: 'fa fa-arrows',
  94. title: __('Drag to sort'),
  95. extend: 'data-toggle="tooltip"',
  96. classname: 'btn btn-xs btn-primary btn-dragsort'
  97. }
  98. },
  99. api: {
  100. init: function (defaults, columnDefaults, locales) {
  101. defaults = defaults ? defaults : {};
  102. columnDefaults = columnDefaults ? columnDefaults : {};
  103. locales = locales ? locales : {};
  104. // 如果是iOS设备则启用卡片视图
  105. if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
  106. Table.defaults.cardView = true;
  107. }
  108. // 写入bootstrap-table默认配置
  109. $.extend(true, $.fn.bootstrapTable.defaults, Table.defaults, defaults);
  110. // 写入bootstrap-table column配置
  111. $.extend($.fn.bootstrapTable.columnDefaults, Table.columnDefaults, columnDefaults);
  112. // 写入bootstrap-table locale配置
  113. $.extend($.fn.bootstrapTable.locales[Table.defaults.locale], {
  114. formatCommonSearch: function () {
  115. return __('Common search');
  116. },
  117. formatCommonSubmitButton: function () {
  118. return __('Submit');
  119. },
  120. formatCommonResetButton: function () {
  121. return __('Reset');
  122. },
  123. formatCommonCloseButton: function () {
  124. return __('Close');
  125. },
  126. formatCommonChoose: function () {
  127. return __('Choose');
  128. },
  129. formatJumpto: function () {
  130. return __('Go');
  131. }
  132. }, locales);
  133. if (typeof defaults.exportTypes != 'undefined') {
  134. $.fn.bootstrapTable.defaults.exportTypes = defaults.exportTypes;
  135. }
  136. },
  137. // 绑定事件
  138. bindevent: function (table) {
  139. //Bootstrap-table的父元素,包含table,toolbar,pagnation
  140. var parenttable = table.closest('.bootstrap-table');
  141. //Bootstrap-table配置
  142. var options = table.bootstrapTable('getOptions');
  143. //Bootstrap操作区
  144. var toolbar = $(options.toolbar, parenttable);
  145. //当刷新表格时
  146. table.on('load-error.bs.table', function (status, res, e) {
  147. if (e.status === 0) {
  148. return;
  149. }
  150. Toastr.error(__('Unknown data format'));
  151. });
  152. //当加载数据成功时
  153. table.on('load-success.bs.table', function (e, data) {
  154. if (typeof data.rows === 'undefined' && typeof data.code != 'undefined') {
  155. Toastr.error(data.msg);
  156. }
  157. });
  158. //当刷新表格时
  159. table.on('refresh.bs.table', function (e, settings, data) {
  160. $(Table.config.refreshbtn, toolbar).find(".fa").addClass("fa-spin");
  161. });
  162. if (options.dblClickToEdit) {
  163. //当双击单元格时
  164. table.on('dbl-click-row.bs.table', function (e, row, element, field) {
  165. $(Table.config.editonebtn, element).trigger("click");
  166. });
  167. }
  168. //当内容渲染完成后
  169. table.on('post-body.bs.table', function (e, settings, json, xhr) {
  170. $(Table.config.refreshbtn, toolbar).find(".fa").removeClass("fa-spin");
  171. $(Table.config.disabledbtn, toolbar).toggleClass('disabled', true);
  172. if ($(Table.config.firsttd, table).find("input[type='checkbox'][data-index]").size() > 0) {
  173. // 挺拽选择,需要重新绑定事件
  174. require(['drag', 'drop'], function () {
  175. $(Table.config.firsttd, table).drag("start", function (ev, dd) {
  176. return $('<div class="selection" />').css('opacity', .65).appendTo(document.body);
  177. }).drag(function (ev, dd) {
  178. $(dd.proxy).css({
  179. top: Math.min(ev.pageY, dd.startY),
  180. left: Math.min(ev.pageX, dd.startX),
  181. height: Math.abs(ev.pageY - dd.startY),
  182. width: Math.abs(ev.pageX - dd.startX)
  183. });
  184. }).drag("end", function (ev, dd) {
  185. $(dd.proxy).remove();
  186. });
  187. $(Table.config.firsttd, table).drop("start", function () {
  188. Table.api.toggleattr(this);
  189. }).drop(function () {
  190. Table.api.toggleattr(this);
  191. }).drop("end", function () {
  192. Table.api.toggleattr(this);
  193. });
  194. $.drop({
  195. multi: true
  196. });
  197. });
  198. }
  199. });
  200. // 处理选中筛选框后按钮的状态统一变更
  201. table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
  202. var ids = Table.api.selectedids(table);
  203. $(Table.config.disabledbtn, toolbar).toggleClass('disabled', !ids.length);
  204. });
  205. // 绑定TAB事件
  206. $('.panel-heading [data-field] a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  207. var field = $(this).closest("[data-field]").data("field");
  208. var value = $(this).data("value");
  209. var object = $("[name='" + field + "']", table.closest(".bootstrap-table").find(".commonsearch-table"));
  210. if (object.prop('tagName') == "SELECT") {
  211. $("option[value='" + value + "']", object).prop("selected", true);
  212. } else {
  213. object.val(value);
  214. }
  215. table.bootstrapTable('refresh', {pageNumber: 1});
  216. return false;
  217. });
  218. // 刷新按钮事件
  219. $(toolbar).on('click', Table.config.refreshbtn, function () {
  220. table.bootstrapTable('refresh');
  221. });
  222. // 添加按钮事件
  223. $(toolbar).on('click', Table.config.addbtn, function () {
  224. var ids = Table.api.selectedids(table);
  225. var url = options.extend.add_url;
  226. if (url.indexOf("{ids}") !== -1) {
  227. url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
  228. }
  229. Fast.api.open(url, __('Add'), $(this).data() || {});
  230. });
  231. // 导入按钮事件
  232. if ($(Table.config.importbtn, toolbar).size() > 0) {
  233. require(['upload'], function (Upload) {
  234. Upload.api.plupload($(Table.config.importbtn, toolbar), function (data, ret) {
  235. Fast.api.ajax({
  236. url: options.extend.import_url,
  237. data: {file: data.url},
  238. }, function (data, ret) {
  239. table.bootstrapTable('refresh');
  240. });
  241. });
  242. });
  243. }
  244. // 批量编辑按钮事件
  245. $(toolbar).on('click', Table.config.editbtn, function () {
  246. var that = this;
  247. //循环弹出多个编辑框
  248. $.each(table.bootstrapTable('getSelections'), function (index, row) {
  249. var url = options.extend.edit_url;
  250. row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
  251. var url = Table.api.replaceurl(url, row, table);
  252. Fast.api.open(url, __('Edit'), $(that).data() || {});
  253. });
  254. });
  255. //清空回收站
  256. $(document).on('click', Table.config.destroyallbtn, function () {
  257. var that = this;
  258. Layer.confirm(__('Are you sure you want to truncate?'), function () {
  259. var url = $(that).data("url") ? $(that).data("url") : $(that).attr("href");
  260. Fast.api.ajax(url, function () {
  261. Layer.closeAll();
  262. table.bootstrapTable('refresh');
  263. }, function () {
  264. Layer.closeAll();
  265. });
  266. });
  267. return false;
  268. });
  269. //还原或删除
  270. $(document).on('click', Table.config.restoreallbtn + ',' + Table.config.restoreonebtn + ',' + Table.config.destroyonebtn, function () {
  271. var that = this;
  272. var url = $(that).data("url") ? $(that).data("url") : $(that).attr("href");
  273. Fast.api.ajax(url, function () {
  274. table.bootstrapTable('refresh');
  275. });
  276. return false;
  277. });
  278. // 批量操作按钮事件
  279. $(toolbar).on('click', Table.config.multibtn, function () {
  280. var ids = Table.api.selectedids(table);
  281. Table.api.multi($(this).data("action"), ids, table, this);
  282. });
  283. // 批量删除按钮事件
  284. $(toolbar).on('click', Table.config.delbtn, function () {
  285. var that = this;
  286. var ids = Table.api.selectedids(table);
  287. Layer.confirm(
  288. __('Are you sure you want to delete the %s selected item?', ids.length),
  289. {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
  290. function (index) {
  291. Table.api.multi("del", ids, table, that);
  292. Layer.close(index);
  293. }
  294. );
  295. });
  296. // 拖拽排序
  297. require(['dragsort'], function () {
  298. //绑定拖动排序
  299. $("tbody", table).dragsort({
  300. itemSelector: 'tr:visible',
  301. dragSelector: "a.btn-dragsort",
  302. dragEnd: function (a, b) {
  303. var element = $("a.btn-dragsort", this);
  304. var data = table.bootstrapTable('getData');
  305. var current = data[parseInt($(this).data("index"))];
  306. var options = table.bootstrapTable('getOptions');
  307. //改变的值和改变的ID集合
  308. var ids = $.map($("tbody tr:visible", table), function (tr) {
  309. return data[parseInt($(tr).data("index"))][options.pk];
  310. });
  311. var changeid = current[options.pk];
  312. var pid = typeof current.pid != 'undefined' ? current.pid : '';
  313. var params = {
  314. url: table.bootstrapTable('getOptions').extend.dragsort_url,
  315. data: {
  316. ids: ids.join(','),
  317. changeid: changeid,
  318. pid: pid,
  319. field: Table.config.dragsortfield,
  320. orderway: options.sortOrder,
  321. table: options.extend.table,
  322. pk: options.pk
  323. }
  324. };
  325. Fast.api.ajax(params, function (data, ret) {
  326. var success = $(element).data("success") || $.noop;
  327. if (typeof success === 'function') {
  328. if (false === success.call(element, data, ret)) {
  329. return false;
  330. }
  331. }
  332. table.bootstrapTable('refresh');
  333. }, function (data, ret) {
  334. var error = $(element).data("error") || $.noop;
  335. if (typeof error === 'function') {
  336. if (false === error.call(element, data, ret)) {
  337. return false;
  338. }
  339. }
  340. table.bootstrapTable('refresh');
  341. });
  342. },
  343. placeHolderTemplate: ""
  344. });
  345. });
  346. $(table).on("click", "input[data-id][name='checkbox']", function (e) {
  347. var ids = $(this).data("id");
  348. var row = Table.api.getrowbyid(table, ids);
  349. table.trigger('check.bs.table', [row, this]);
  350. });
  351. $(table).on("click", "[data-id].btn-change", function (e) {
  352. e.preventDefault();
  353. Table.api.multi($(this).data("action") ? $(this).data("action") : '', [$(this).data("id")], table, this);
  354. });
  355. $(table).on("click", "[data-id].btn-edit", function (e) {
  356. e.preventDefault();
  357. var ids = $(this).data("id");
  358. var row = Table.api.getrowbyid(table, ids);
  359. row.ids = ids;
  360. var url = Table.api.replaceurl(options.extend.edit_url, row, table);
  361. Fast.api.open(url, __('Edit'), $(this).data() || {});
  362. });
  363. $(table).on("click", "[data-id].btn-del", function (e) {
  364. e.preventDefault();
  365. var id = $(this).data("id");
  366. var that = this;
  367. Layer.confirm(
  368. __('Are you sure you want to delete this item?'),
  369. {icon: 3, title: __('Warning'), shadeClose: true},
  370. function (index) {
  371. Table.api.multi("del", id, table, that);
  372. Layer.close(index);
  373. }
  374. );
  375. });
  376. var id = table.attr("id");
  377. Table.list[id] = table;
  378. return table;
  379. },
  380. // 批量操作请求
  381. multi: function (action, ids, table, element) {
  382. var options = table.bootstrapTable('getOptions');
  383. var data = element ? $(element).data() : {};
  384. var ids = ($.isArray(ids) ? ids.join(",") : ids);
  385. var url = typeof data.url !== "undefined" ? data.url : (action == "del" ? options.extend.del_url : options.extend.multi_url);
  386. url = this.replaceurl(url, {ids: ids}, table);
  387. var params = typeof data.params !== "undefined" ? (typeof data.params == 'object' ? $.param(data.params) : data.params) : '';
  388. var options = {url: url, data: {action: action, ids: ids, params: params}};
  389. Fast.api.ajax(options, function (data, ret) {
  390. var success = $(element).data("success") || $.noop;
  391. if (typeof success === 'function') {
  392. if (false === success.call(element, data, ret)) {
  393. return false;
  394. }
  395. }
  396. table.bootstrapTable('refresh');
  397. }, function (data, ret) {
  398. var error = $(element).data("error") || $.noop;
  399. if (typeof error === 'function') {
  400. if (false === error.call(element, data, ret)) {
  401. return false;
  402. }
  403. }
  404. });
  405. },
  406. // 单元格元素事件
  407. events: {
  408. operate: {
  409. 'click .btn-editone': function (e, value, row, index) {
  410. e.stopPropagation();
  411. e.preventDefault();
  412. var table = $(this).closest('table');
  413. var options = table.bootstrapTable('getOptions');
  414. var ids = row[options.pk];
  415. row = $.extend({}, row ? row : {}, {ids: ids});
  416. var url = options.extend.edit_url;
  417. Fast.api.open(Table.api.replaceurl(url, row, table), __('Edit'), $(this).data() || {});
  418. },
  419. 'click .btn-delone': function (e, value, row, index) {
  420. e.stopPropagation();
  421. e.preventDefault();
  422. var that = this;
  423. var top = $(that).offset().top - $(window).scrollTop();
  424. var left = $(that).offset().left - $(window).scrollLeft() - 260;
  425. if (top + 154 > $(window).height()) {
  426. top = top - 154;
  427. }
  428. if ($(window).width() < 480) {
  429. top = left = undefined;
  430. }
  431. Layer.confirm(
  432. __('Are you sure you want to delete this item?'),
  433. {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
  434. function (index) {
  435. var table = $(that).closest('table');
  436. var options = table.bootstrapTable('getOptions');
  437. Table.api.multi("del", row[options.pk], table, that);
  438. Layer.close(index);
  439. }
  440. );
  441. }
  442. },//单元格图片预览
  443. image: {
  444. 'click .img-center': function (e, value, row, index) {
  445. var data = [];
  446. value = value.toString().split(",");
  447. $.each(value, function (index, value) {
  448. data.push({
  449. src: Fast.api.cdnurl(value),
  450. });
  451. });
  452. Layer.photos({
  453. photos: {
  454. "start": $(this).parent().index(),
  455. "data": data
  456. },
  457. anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
  458. });
  459. },
  460. }
  461. },
  462. // 单元格数据格式化
  463. formatter: {
  464. icon: function (value, row, index) {
  465. if (!value)
  466. return '';
  467. value = value === null ? '' : value.toString();
  468. value = value.indexOf(" ") > -1 ? value : "fa fa-" + value;
  469. //渲染fontawesome图标
  470. return '<i class="' + value + '"></i> ' + value;
  471. },
  472. image: function (value, row, index) {
  473. value = value ? value : '/assets/img/blank.gif';
  474. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  475. return '<a href="javascript:"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>';
  476. },
  477. images: function (value, row, index) {
  478. value = value === null ? '' : value.toString();
  479. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  480. var arr = value.split(',');
  481. var html = [];
  482. $.each(arr, function (i, value) {
  483. value = value ? value : '/assets/img/blank.gif';
  484. html.push('<a href="javascript:"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>');
  485. });
  486. return html.join(' ');
  487. },
  488. content: function (value, row, index) {
  489. var width = this.width != undefined ? this.width : 250;
  490. return "<div style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + "px;'>" + value + "</div>";
  491. },
  492. status: function (value, row, index) {
  493. var custom = {normal: 'success', hidden: 'gray', deleted: 'danger', locked: 'info'};
  494. if (typeof this.custom !== 'undefined') {
  495. custom = $.extend(custom, this.custom);
  496. }
  497. this.custom = custom;
  498. this.icon = 'fa fa-circle';
  499. return Table.api.formatter.normal.call(this, value, row, index);
  500. },
  501. normal: function (value, row, index) {
  502. var colorArr = ["primary", "success", "danger", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
  503. var custom = {};
  504. if (typeof this.custom !== 'undefined') {
  505. custom = $.extend(custom, this.custom);
  506. }
  507. value = value === null ? '' : value.toString();
  508. var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
  509. var index = keys.indexOf(value);
  510. var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
  511. var display = index > -1 ? this.searchList[value] : null;
  512. var icon = typeof this.icon !== 'undefined' ? this.icon : null;
  513. if (!color) {
  514. color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
  515. }
  516. if (!display) {
  517. display = __(value.charAt(0).toUpperCase() + value.slice(1));
  518. }
  519. var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
  520. if (this.operate != false) {
  521. html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
  522. }
  523. return html;
  524. },
  525. toggle: function (value, row, index) {
  526. var table = this.table;
  527. var options = table ? table.bootstrapTable('getOptions') : {};
  528. var pk = options.pk || "id";
  529. var color = typeof this.color !== 'undefined' ? this.color : 'success';
  530. var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
  531. var no = typeof this.no !== 'undefined' ? this.no : 0;
  532. var url = typeof this.url !== 'undefined' ? this.url : '';
  533. var disable = false;
  534. if (typeof this.disable !== "undefined") {
  535. disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable;
  536. }
  537. return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change " + (disable ? 'btn disabled' : '') + "' data-id='"
  538. + row[pk] + "' " + (url ? "data-url='" + url + "'" : "") + " 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>";
  539. },
  540. url: function (value, row, index) {
  541. value = value === null ? '' : value.toString();
  542. 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>';
  543. },
  544. search: function (value, row, index) {
  545. var field = this.field;
  546. if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
  547. value = row[this.customField];
  548. field = this.customField;
  549. }
  550. return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', value) + '" data-field="' + field + '" data-value="' + value + '">' + value + '</a>';
  551. },
  552. addtabs: function (value, row, index) {
  553. var url = Table.api.replaceurl(this.url, row, this.table);
  554. var title = this.atitle ? this.atitle : __("Search %s", value);
  555. return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  556. },
  557. dialog: function (value, row, index) {
  558. var url = Table.api.replaceurl(this.url, row, this.table);
  559. var title = this.atitle ? this.atitle : __("View %s", value);
  560. return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  561. },
  562. flag: function (value, row, index) {
  563. var that = this;
  564. value = value === null ? '' : value.toString();
  565. var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
  566. //如果字段列有定义custom
  567. if (typeof this.custom !== 'undefined') {
  568. colorArr = $.extend(colorArr, this.custom);
  569. }
  570. var field = this.field;
  571. if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
  572. value = row[this.customField];
  573. field = this.customField;
  574. }
  575. //渲染Flag
  576. var html = [];
  577. var arr = value.split(',');
  578. var color, display, label;
  579. $.each(arr, function (i, value) {
  580. value = value === null ? '' : value.toString();
  581. if (value == '')
  582. return true;
  583. color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
  584. display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
  585. label = '<span class="label label-' + color + '">' + display + '</span>';
  586. if (that.operate) {
  587. html.push('<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + field + '" data-value="' + value + '">' + label + '</a>');
  588. } else {
  589. html.push(label);
  590. }
  591. });
  592. return html.join(' ');
  593. },
  594. label: function (value, row, index) {
  595. return Table.api.formatter.flag.call(this, value, row, index);
  596. },
  597. datetime: function (value, row, index) {
  598. var datetimeFormat = typeof this.datetimeFormat === 'undefined' ? 'YYYY-MM-DD HH:mm:ss' : this.datetimeFormat;
  599. if (isNaN(value)) {
  600. return value ? Moment(value).format(datetimeFormat) : __('None');
  601. } else {
  602. return value ? Moment(parseInt(value) * 1000).format(datetimeFormat) : __('None');
  603. }
  604. },
  605. operate: function (value, row, index) {
  606. var table = this.table;
  607. // 操作配置
  608. var options = table ? table.bootstrapTable('getOptions') : {};
  609. // 默认按钮组
  610. var buttons = $.extend([], this.buttons || []);
  611. // 所有按钮名称
  612. var names = [];
  613. buttons.forEach(function (item) {
  614. names.push(item.name);
  615. });
  616. if (options.extend.dragsort_url !== '' && names.indexOf('dragsort') === -1) {
  617. buttons.push(Table.button.dragsort);
  618. }
  619. if (options.extend.edit_url !== '' && names.indexOf('edit') === -1) {
  620. Table.button.edit.url = options.extend.edit_url;
  621. buttons.push(Table.button.edit);
  622. }
  623. if (options.extend.del_url !== '' && names.indexOf('del') === -1) {
  624. buttons.push(Table.button.del);
  625. }
  626. return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
  627. }
  628. ,
  629. buttons: function (value, row, index) {
  630. // 默认按钮组
  631. var buttons = $.extend([], this.buttons || []);
  632. return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
  633. }
  634. },
  635. buttonlink: function (column, buttons, value, row, index, type) {
  636. var table = column.table;
  637. type = typeof type === 'undefined' ? 'buttons' : type;
  638. var options = table ? table.bootstrapTable('getOptions') : {};
  639. var html = [];
  640. var hidden, visible, disable, url, classname, icon, text, title, refresh, confirm, extend,
  641. dropdown, link;
  642. var fieldIndex = column.fieldIndex;
  643. var dropdowns = {};
  644. $.each(buttons, function (i, j) {
  645. if (type === 'operate') {
  646. if (j.name === 'dragsort' && typeof row[Table.config.dragsortfield] === 'undefined') {
  647. return true;
  648. }
  649. if (['add', 'edit', 'del', 'multi', 'dragsort'].indexOf(j.name) > -1 && !options.extend[j.name + "_url"]) {
  650. return true;
  651. }
  652. }
  653. var attr = table.data(type + "-" + j.name);
  654. if (typeof attr === 'undefined' || attr) {
  655. hidden = typeof j.hidden === 'function' ? j.hidden.call(table, row, j) : (typeof j.hidden !== 'undefined' ? j.hidden : false);
  656. if (hidden) {
  657. return true;
  658. }
  659. visible = typeof j.visible === 'function' ? j.visible.call(table, row, j) : (typeof j.visible !== 'undefined' ? j.visible : true);
  660. if (!visible) {
  661. return true;
  662. }
  663. dropdown = j.dropdown ? j.dropdown : '';
  664. url = j.url ? j.url : '';
  665. url = typeof url === 'function' ? url.call(table, row, j) : (url ? Fast.api.fixurl(Table.api.replaceurl(url, row, table)) : 'javascript:;');
  666. classname = j.classname ? j.classname : 'btn-primary btn-' + name + 'one';
  667. icon = j.icon ? j.icon : '';
  668. text = typeof j.text === 'function' ? j.text.call(table, row, j) : j.text ? j.text : '';
  669. title = typeof j.title === 'function' ? j.title.call(table, row, j) : j.title ? j.title : text;
  670. refresh = j.refresh ? 'data-refresh="' + j.refresh + '"' : '';
  671. confirm = typeof j.confirm === 'function' ? j.confirm.call(table, row, j) : (typeof j.confirm !== 'undefined' ? j.confirm : false);
  672. confirm = confirm ? 'data-confirm="' + confirm + '"' : '';
  673. extend = j.extend ? j.extend : '';
  674. disable = typeof j.disable === 'function' ? j.disable.call(table, row, j) : (typeof j.disable !== 'undefined' ? j.disable : false);
  675. if (disable) {
  676. classname = classname + ' disabled';
  677. }
  678. 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>';
  679. if (dropdown) {
  680. if (typeof dropdowns[dropdown] == 'undefined') {
  681. dropdowns[dropdown] = [];
  682. }
  683. dropdowns[dropdown].push(link);
  684. } else {
  685. html.push(link);
  686. }
  687. }
  688. });
  689. if (!$.isEmptyObject(dropdowns)) {
  690. var dropdownHtml = [];
  691. $.each(dropdowns, function (i, j) {
  692. 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>');
  693. });
  694. html.unshift(dropdownHtml);
  695. }
  696. return html.join(' ');
  697. },
  698. //替换URL中的数据
  699. replaceurl: function (url, row, table) {
  700. var options = table ? table.bootstrapTable('getOptions') : null;
  701. var ids = options ? row[options.pk] : 0;
  702. row.ids = ids ? ids : (typeof row.ids !== 'undefined' ? row.ids : 0);
  703. //自动添加ids参数
  704. url = !url.match(/\{ids\}/i) ? url + (url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + '{ids}' : url;
  705. url = url.replace(/\{(.*?)\}/gi, function (matched) {
  706. matched = matched.substring(1, matched.length - 1);
  707. if (matched.indexOf(".") !== -1) {
  708. var temp = row;
  709. var arr = matched.split(/\./);
  710. for (var i = 0; i < arr.length; i++) {
  711. if (typeof temp[arr[i]] !== 'undefined') {
  712. temp = temp[arr[i]];
  713. }
  714. }
  715. return typeof temp === 'object' ? '' : temp;
  716. }
  717. return row[matched];
  718. });
  719. return url;
  720. },
  721. // 获取选中的条目ID集合
  722. selectedids: function (table) {
  723. var options = table.bootstrapTable('getOptions');
  724. if (options.templateView) {
  725. return $.map($("input[data-id][name='checkbox']:checked"), function (dom) {
  726. return $(dom).data("id");
  727. });
  728. } else {
  729. return $.map(table.bootstrapTable('getSelections'), function (row) {
  730. return row[options.pk];
  731. });
  732. }
  733. },
  734. // 切换复选框状态
  735. toggleattr: function (table) {
  736. $("input[type='checkbox']", table).trigger('click');
  737. },
  738. // 根据行索引获取行数据
  739. getrowdata: function (table, index) {
  740. index = parseInt(index);
  741. var data = table.bootstrapTable('getData');
  742. return typeof data[index] !== 'undefined' ? data[index] : null;
  743. },
  744. // 根据行索引获取行数据
  745. getrowbyindex: function (table, index) {
  746. return Table.api.getrowdata(table, index);
  747. },
  748. // 根据主键ID获取行数据
  749. getrowbyid: function (table, id) {
  750. var row = {};
  751. var options = table.bootstrapTable("getOptions");
  752. $.each(table.bootstrapTable('getData'), function (i, j) {
  753. if (j[options.pk] == id) {
  754. row = j;
  755. return false;
  756. }
  757. });
  758. return row;
  759. }
  760. },
  761. };
  762. return Table;
  763. });