require-table.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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:"><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:"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>');
  447. });
  448. return html.join(' ');
  449. },
  450. content: function (value, row, index) {
  451. var width = this.width != undefined ? this.width : 250;
  452. return "<div style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + "px;'>" + value + "</div>";
  453. },
  454. status: function (value, row, index) {
  455. var custom = {normal: 'success', hidden: 'gray', deleted: 'danger', locked: 'info'};
  456. if (typeof this.custom !== 'undefined') {
  457. custom = $.extend(custom, this.custom);
  458. }
  459. this.custom = custom;
  460. this.icon = 'fa fa-circle';
  461. return Table.api.formatter.normal.call(this, value, row, index);
  462. },
  463. normal: function (value, row, index) {
  464. var colorArr = ["primary", "success", "danger", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
  465. var custom = {};
  466. if (typeof this.custom !== 'undefined') {
  467. custom = $.extend(custom, this.custom);
  468. }
  469. value = value === null ? '' : value.toString();
  470. var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
  471. var index = keys.indexOf(value);
  472. var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
  473. var display = index > -1 ? this.searchList[value] : null;
  474. var icon = typeof this.icon !== 'undefined' ? this.icon : null;
  475. if (!color) {
  476. color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
  477. }
  478. if (!display) {
  479. display = __(value.charAt(0).toUpperCase() + value.slice(1));
  480. }
  481. var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
  482. if (this.operate != false) {
  483. html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
  484. }
  485. return html;
  486. },
  487. toggle: function (value, row, index) {
  488. var color = typeof this.color !== 'undefined' ? this.color : 'success';
  489. var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
  490. var no = typeof this.no !== 'undefined' ? this.no : 0;
  491. var url = typeof this.url !== 'undefined' ? this.url : '';
  492. var disable = false;
  493. if (typeof this.disable !== "undefined") {
  494. disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable;
  495. }
  496. return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change " + (disable ? 'btn disabled' : '') + "' data-id='"
  497. + 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>";
  498. },
  499. url: function (value, row, index) {
  500. 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>';
  501. },
  502. search: function (value, row, index) {
  503. var field = this.field;
  504. if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
  505. value = row[this.customField];
  506. field = this.customField;
  507. }
  508. return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', value) + '" data-field="' + field + '" data-value="' + value + '">' + value + '</a>';
  509. },
  510. addtabs: function (value, row, index) {
  511. var url = Table.api.replaceurl(this.url, row, this.table);
  512. var title = this.atitle ? this.atitle : __("Search %s", value);
  513. return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  514. },
  515. dialog: function (value, row, index) {
  516. var url = Table.api.replaceurl(this.url, row, this.table);
  517. var title = this.atitle ? this.atitle : __("View %s", value);
  518. return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  519. },
  520. flag: function (value, row, index) {
  521. var that = this;
  522. value = value === null ? '' : value.toString();
  523. var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
  524. //如果字段列有定义custom
  525. if (typeof this.custom !== 'undefined') {
  526. colorArr = $.extend(colorArr, this.custom);
  527. }
  528. var field = this.field;
  529. if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
  530. value = row[this.customField];
  531. field = this.customField;
  532. }
  533. //渲染Flag
  534. var html = [];
  535. var arr = value.split(',');
  536. var color, display, label;
  537. $.each(arr, function (i, value) {
  538. value = value === null ? '' : value.toString();
  539. if (value == '')
  540. return true;
  541. color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
  542. display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
  543. label = '<span class="label label-' + color + '">' + display + '</span>';
  544. if (that.operate) {
  545. html.push('<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + field + '" data-value="' + value + '">' + label + '</a>');
  546. } else {
  547. html.push(label);
  548. }
  549. });
  550. return html.join(' ');
  551. },
  552. label: function (value, row, index) {
  553. return Table.api.formatter.flag.call(this, value, row, index);
  554. },
  555. datetime: function (value, row, index) {
  556. var datetimeFormat = typeof this.datetimeFormat === 'undefined' ? 'YYYY-MM-DD HH:mm:ss' : this.datetimeFormat;
  557. if (isNaN(value)) {
  558. return value ? Moment(value).format(datetimeFormat) : __('None');
  559. } else {
  560. return value ? Moment(parseInt(value) * 1000).format(datetimeFormat) : __('None');
  561. }
  562. },
  563. operate: function (value, row, index) {
  564. var table = this.table;
  565. // 操作配置
  566. var options = table ? table.bootstrapTable('getOptions') : {};
  567. // 默认按钮组
  568. var buttons = $.extend([], this.buttons || []);
  569. // 所有按钮名称
  570. var names = [];
  571. buttons.forEach(function (item) {
  572. names.push(item.name);
  573. });
  574. if (options.extend.dragsort_url !== '' && names.indexOf('dragsort') === -1) {
  575. buttons.push({
  576. name: 'dragsort',
  577. icon: 'fa fa-arrows',
  578. title: __('Drag to sort'),
  579. extend: 'data-toggle="tooltip"',
  580. classname: 'btn btn-xs btn-primary btn-dragsort'
  581. });
  582. }
  583. if (options.extend.edit_url !== '' && names.indexOf('edit') === -1) {
  584. buttons.push({
  585. name: 'edit',
  586. icon: 'fa fa-pencil',
  587. title: __('Edit'),
  588. extend: 'data-toggle="tooltip"',
  589. classname: 'btn btn-xs btn-success btn-editone',
  590. url: options.extend.edit_url
  591. });
  592. }
  593. if (options.extend.del_url !== '' && names.indexOf('del') === -1) {
  594. buttons.push({
  595. name: 'del',
  596. icon: 'fa fa-trash',
  597. title: __('Del'),
  598. extend: 'data-toggle="tooltip"',
  599. classname: 'btn btn-xs btn-danger btn-delone'
  600. });
  601. }
  602. return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
  603. }
  604. ,
  605. buttons: function (value, row, index) {
  606. // 默认按钮组
  607. var buttons = $.extend([], this.buttons || []);
  608. return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
  609. }
  610. },
  611. buttonlink: function (column, buttons, value, row, index, type) {
  612. var table = column.table;
  613. type = typeof type === 'undefined' ? 'buttons' : type;
  614. var options = table ? table.bootstrapTable('getOptions') : {};
  615. var html = [];
  616. var hidden, visible, disable, url, classname, icon, text, title, refresh, confirm, extend,
  617. dropdown, link;
  618. var fieldIndex = column.fieldIndex;
  619. var dropdowns = {};
  620. $.each(buttons, function (i, j) {
  621. if (type === 'operate') {
  622. if (j.name === 'dragsort' && typeof row[Table.config.dragsortfield] === 'undefined') {
  623. return true;
  624. }
  625. if (['add', 'edit', 'del', 'multi', 'dragsort'].indexOf(j.name) > -1 && !options.extend[j.name + "_url"]) {
  626. return true;
  627. }
  628. }
  629. var attr = table.data(type + "-" + j.name);
  630. if (typeof attr === 'undefined' || attr) {
  631. hidden = typeof j.hidden === 'function' ? j.hidden.call(table, row, j) : (typeof j.hidden !== 'undefined' ? j.hidden : false);
  632. if (hidden) {
  633. return true;
  634. }
  635. visible = typeof j.visible === 'function' ? j.visible.call(table, row, j) : (typeof j.visible !== 'undefined' ? j.visible : true);
  636. if (!visible) {
  637. return true;
  638. }
  639. dropdown = j.dropdown ? j.dropdown : '';
  640. url = j.url ? j.url : '';
  641. url = typeof url === 'function' ? url.call(table, row, j) : (url ? Fast.api.fixurl(Table.api.replaceurl(url, row, table)) : 'javascript:;');
  642. classname = j.classname ? j.classname : 'btn-primary btn-' + name + 'one';
  643. icon = j.icon ? j.icon : '';
  644. text = typeof j.text === 'function' ? j.text.call(table, row, j) : j.text ? j.text : '';
  645. title = typeof j.title === 'function' ? j.title.call(table, row, j) : j.title ? j.title : text;
  646. refresh = j.refresh ? 'data-refresh="' + j.refresh + '"' : '';
  647. confirm = typeof j.confirm === 'function' ? j.confirm.call(table, row, j) : (typeof j.confirm !== 'undefined' ? j.confirm : false);
  648. confirm = confirm ? 'data-confirm="' + confirm + '"' : '';
  649. extend = j.extend ? j.extend : '';
  650. disable = typeof j.disable === 'function' ? j.disable.call(table, row, j) : (typeof j.disable !== 'undefined' ? j.disable : false);
  651. if (disable) {
  652. classname = classname + ' disabled';
  653. }
  654. 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>';
  655. if (dropdown) {
  656. if (typeof dropdowns[dropdown] == 'undefined') {
  657. dropdowns[dropdown] = [];
  658. }
  659. dropdowns[dropdown].push(link);
  660. } else {
  661. html.push(link);
  662. }
  663. }
  664. });
  665. if (!$.isEmptyObject(dropdowns)) {
  666. var dropdownHtml = [];
  667. $.each(dropdowns, function (i, j) {
  668. 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>');
  669. });
  670. html.unshift(dropdownHtml);
  671. }
  672. return html.join(' ');
  673. },
  674. //替换URL中的数据
  675. replaceurl: function (url, row, table) {
  676. var options = table ? table.bootstrapTable('getOptions') : null;
  677. var ids = options ? row[options.pk] : 0;
  678. row.ids = ids ? ids : (typeof row.ids !== 'undefined' ? row.ids : 0);
  679. //自动添加ids参数
  680. url = !url.match(/\{ids\}/i) ? url + (url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + '{ids}' : url;
  681. url = url.replace(/\{(.*?)\}/gi, function (matched) {
  682. matched = matched.substring(1, matched.length - 1);
  683. if (matched.indexOf(".") !== -1) {
  684. var temp = row;
  685. var arr = matched.split(/\./);
  686. for (var i = 0; i < arr.length; i++) {
  687. if (typeof temp[arr[i]] !== 'undefined') {
  688. temp = temp[arr[i]];
  689. }
  690. }
  691. return typeof temp === 'object' ? '' : temp;
  692. }
  693. return row[matched];
  694. });
  695. return url;
  696. },
  697. // 获取选中的条目ID集合
  698. selectedids: function (table) {
  699. var options = table.bootstrapTable('getOptions');
  700. if (options.templateView) {
  701. return $.map($("input[data-id][name='checkbox']:checked"), function (dom) {
  702. return $(dom).data("id");
  703. });
  704. } else {
  705. return $.map(table.bootstrapTable('getSelections'), function (row) {
  706. return row[options.pk];
  707. });
  708. }
  709. },
  710. // 切换复选框状态
  711. toggleattr: function (table) {
  712. $("input[type='checkbox']", table).trigger('click');
  713. },
  714. // 根据行索引获取行数据
  715. getrowdata: function (table, index) {
  716. index = parseInt(index);
  717. var data = table.bootstrapTable('getData');
  718. return typeof data[index] !== 'undefined' ? data[index] : null;
  719. },
  720. // 根据行索引获取行数据
  721. getrowbyindex: function (table, index) {
  722. return Table.api.getrowdata(table, index);
  723. },
  724. // 根据主键ID获取行数据
  725. getrowbyid: function (table, id) {
  726. var row = {};
  727. var options = table.bootstrapTable("getOptions");
  728. $.each(table.bootstrapTable('getData'), function (i, j) {
  729. if (j[options.pk] == id) {
  730. row = j;
  731. return false;
  732. }
  733. });
  734. return row;
  735. }
  736. },
  737. };
  738. return Table;
  739. });