require-table.js 39 KB

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