require-table.js 40 KB

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