totalnumber.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {
  2. var Controller = {
  3. index: function () {
  4. // 初始化表格参数配置
  5. Table.api.init({
  6. extend: {
  7. index_url: 'customcharts/totalnumber/index' + location.search,
  8. add_url: 'customcharts/totalnumber/add',
  9. edit_url: 'customcharts/totalnumber/edit',
  10. del_url: 'customcharts/totalnumber/del',
  11. table: 'customcharts_total_number',
  12. }
  13. });
  14. var table = $("#table");
  15. // 初始化表格
  16. table.bootstrapTable({
  17. url: $.fn.bootstrapTable.defaults.extend.index_url,
  18. pk: 'id',
  19. sortName: 'weigh',
  20. columns: [
  21. [
  22. {checkbox: true},
  23. {field: 'id', title: __('Id')},
  24. {field: 'title', title: __('Title')},
  25. {field: 'name', title: __('Name')},
  26. {field: 'field_total', title: __('Field_total')},
  27. {field: 'type_total', title: __('Type_total'), searchList: {"sum":__('Sum'),"count":__('Count')}, formatter: Table.api.formatter.normal},
  28. {field: 'field_time', title: __('Field_time')},
  29. {field: 'type_time', title: __('Type_time'), searchList: {"today":__('Today'),"week":__('Week'),"month":__('Month'),"all":__('All')}, formatter: Table.api.formatter.normal},
  30. {field: 'icon', title: __('Icon'), formatter: Table.api.formatter.icon},
  31. {field: 'weigh', title: __('Weigh')},
  32. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  33. ]
  34. ]
  35. });
  36. // 为表格绑定事件
  37. Table.api.bindevent(table);
  38. },
  39. add: function () {
  40. Controller.api.bindevent();
  41. },
  42. edit: function () {
  43. Controller.api.bindevent();
  44. },
  45. api: {
  46. bindevent: function () {
  47. Form.api.bindevent($("form[role=form]"));
  48. //颜色选择器
  49. var refreshStyle = function () {
  50. var style = [];
  51. if ($(".btn-color").hasClass("active")) {
  52. style.push($(".btn-color").data("color"));
  53. }
  54. $("input[name='row[icon_color]']").val(style.join("|"));
  55. };
  56. if ($(".btn-color").hasClass("active")) {
  57. style.push($(".btn-color").data("color"));
  58. }
  59. require(['jquery-colorpicker'], function () {
  60. $('.colorpicker').colorpicker({
  61. color: function () {
  62. var color = "#000000";
  63. var rgb = $("#c-icon_color").css('color').match(/^rgb\(((\d+),\s*(\d+),\s*(\d+))\)$/);
  64. if (rgb) {
  65. color = rgb[1];
  66. }
  67. return color;
  68. }
  69. }, function (event, obj) {
  70. $("#c-icon_color").css('color', '#' + obj.hex);
  71. $(event).addClass("active").data("color", '#' + obj.hex);
  72. refreshStyle();
  73. }, function (event) {
  74. $("#c-icon_color").css('color', 'inherit');
  75. $(event).removeClass("active");
  76. refreshStyle();
  77. });
  78. });
  79. //选择表和渲染字段
  80. var typelist = {};
  81. $(document).on('change', "select[name='row[name]']", function () {
  82. var that = this;
  83. Fast.api.ajax({
  84. url: "customcharts/totalnumber/get_field_list",
  85. data: {table: $(that).val()},
  86. }, function (data, ret) {
  87. let mainfields = data.fieldlist;
  88. let commentlist = data.commentlist;
  89. typelist = data.typelist;//字段类型
  90. Controller.api.renderselect("#c-field_total", mainfields, commentlist, typelist);//渲染数据
  91. Controller.api.renderselect("#c-field_time" , mainfields, commentlist, typelist);//渲染数据
  92. return false;
  93. });
  94. return false;
  95. });
  96. $("select[name='row[name]']").change();
  97. //选择时间字段
  98. $(document).on('change', "select[name='row[field_time]']", function () {
  99. $('input[name="row[field_time_type]"]').val(typelist[$(this).val()]);
  100. });
  101. //搜索图标
  102. var iconlist = [];
  103. var iconfunc = function () {
  104. Layer.open({
  105. type: 1,
  106. area: ['99%', '98%'], //宽高
  107. content: Template('chooseicontpl', {iconlist: iconlist})
  108. });
  109. };
  110. $(document).on('click', ".btn-search-icon", function () {
  111. if (iconlist.length == 0) {
  112. $.get(Config.site.cdnurl + "/assets/libs/font-awesome/less/variables.less", function (ret) {
  113. var exp = /fa-var-(.*):/ig;
  114. var result;
  115. while ((result = exp.exec(ret)) != null) {
  116. iconlist.push(result[1]);
  117. }
  118. iconfunc();
  119. });
  120. } else {
  121. iconfunc();
  122. }
  123. });
  124. $(document).on('click', '#chooseicon ul li', function () {
  125. $("input[name='row[icon]']").val('fa fa-' + $(this).data("font"));
  126. Layer.closeAll();
  127. });
  128. $(document).on('keyup', 'input.js-icon-search', function () {
  129. $("#chooseicon ul li").show();
  130. if ($(this).val() != '') {
  131. $("#chooseicon ul li:not([data-font*='" + $(this).val() + "'])").hide();
  132. }
  133. });
  134. },
  135. renderselect: function(select, data, commentlist, typelist) {
  136. var val = $(select).data('value');
  137. var html = [];
  138. for (var i = 0; i < data.length; i++) {
  139. if ('#c-field_time' == select && typelist[data[i]] != 'int' && typelist[data[i]] != 'datetime' && typelist[data[i]] != 'date' && typelist[data[i]] != 'bigint') {
  140. continue;
  141. }
  142. if (val == data[i]) {
  143. html.push("<option data-subtext='" + commentlist[i] + "' value='" + data[i] + "' selected>" + data[i] + "</option>");
  144. } else {
  145. html.push("<option data-subtext='" + commentlist[i] + "' value='" + data[i] + "'>" + data[i] + "</option>");
  146. }
  147. }
  148. $(select).html(html.join(""));
  149. $(select).selectpicker('refresh');
  150. }
  151. }
  152. };
  153. return Controller;
  154. });