require-table.js 41 KB

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