totalnumber.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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: 'where', title: __('Where')},
  32. {field: 'weigh', title: __('Weigh')},
  33. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  34. ]
  35. ]
  36. });
  37. // 为表格绑定事件
  38. Table.api.bindevent(table);
  39. },
  40. add: function () {
  41. Controller.api.bindevent();
  42. },
  43. edit: function () {
  44. Controller.api.bindevent();
  45. },
  46. api: {
  47. bindevent: function () {
  48. Form.api.bindevent($("form[role=form]"));
  49. //颜色选择器
  50. var refreshStyle = function () {
  51. var style = [];
  52. if ($(".btn-color").hasClass("active")) {
  53. style.push($(".btn-color").data("color"));
  54. }
  55. $("input[name='row[icon_color]']").val(style.join("|"));
  56. };
  57. if ($(".btn-color").hasClass("active")) {
  58. style.push($(".btn-color").data("color"));
  59. }
  60. require(['jquery-colorpicker'], function () {
  61. $('.colorpicker').colorpicker({
  62. color: function () {
  63. var color = "#000000";
  64. var rgb = $("#c-icon_color").css('color').match(/^rgb\(((\d+),\s*(\d+),\s*(\d+))\)$/);
  65. if (rgb) {
  66. color = rgb[1];
  67. }
  68. return color;
  69. }
  70. }, function (event, obj) {
  71. $("#c-icon_color").css('color', '#' + obj.hex);
  72. $(event).addClass("active").data("color", '#' + obj.hex);
  73. refreshStyle();
  74. }, function (event) {
  75. $("#c-icon_color").css('color', 'inherit');
  76. $(event).removeClass("active");
  77. refreshStyle();
  78. });
  79. });
  80. //选择表和渲染字段
  81. var typelist = {};
  82. $(document).on('change', "select[name='row[name]']", function () {
  83. var that = this;
  84. Fast.api.ajax({
  85. url: "customcharts/totalnumber/get_field_list",
  86. data: {table: $(that).val()},
  87. }, function (data, ret) {
  88. let mainfields = data.fieldlist;
  89. let commentlist = data.commentlist;
  90. typelist = data.typelist;//字段类型
  91. Controller.api.renderselect("#c-field_total", mainfields, commentlist, typelist);//渲染数据
  92. Controller.api.renderselect("#c-field_time" , mainfields, commentlist, typelist);//渲染数据
  93. return false;
  94. });
  95. return false;
  96. });
  97. $("select[name='row[name]']").change();
  98. //选择时间字段
  99. $(document).on('change', "select[name='row[field_time]']", function () {
  100. $('input[name="row[field_time_type]"]').val(typelist[$(this).val()]);
  101. });
  102. //搜索图标
  103. var iconlist = [];
  104. var iconfunc = function () {
  105. Layer.open({
  106. type: 1,
  107. area: ['99%', '98%'], //宽高
  108. content: Template('chooseicontpl', {iconlist: iconlist})
  109. });
  110. };
  111. $(document).on('click', ".btn-search-icon", function () {
  112. if (iconlist.length == 0) {
  113. $.get(Config.site.cdnurl + "/assets/libs/font-awesome/less/variables.less", function (ret) {
  114. var exp = /fa-var-(.*):/ig;
  115. var result;
  116. while ((result = exp.exec(ret)) != null) {
  117. iconlist.push(result[1]);
  118. }
  119. iconfunc();
  120. });
  121. } else {
  122. iconfunc();
  123. }
  124. });
  125. $(document).on('click', '#chooseicon ul li', function () {
  126. $("input[name='row[icon]']").val('fa fa-' + $(this).data("font"));
  127. Layer.closeAll();
  128. });
  129. $(document).on('keyup', 'input.js-icon-search', function () {
  130. $("#chooseicon ul li").show();
  131. if ($(this).val() != '') {
  132. $("#chooseicon ul li:not([data-font*='" + $(this).val() + "'])").hide();
  133. }
  134. });
  135. },
  136. renderselect: function(select, data, commentlist, typelist) {
  137. var val = $(select).data('value');
  138. var html = [];
  139. for (var i = 0; i < data.length; i++) {
  140. if ('#c-field_time' == select && typelist[data[i]] != 'int' && typelist[data[i]] != 'datetime' && typelist[data[i]] != 'date') {
  141. continue;
  142. }
  143. if (val == data[i]) {
  144. html.push("<option data-subtext='" + commentlist[i] + "' value='" + data[i] + "' selected>" + data[i] + "</option>");
  145. } else {
  146. html.push("<option data-subtext='" + commentlist[i] + "' value='" + data[i] + "'>" + data[i] + "</option>");
  147. }
  148. }
  149. $(select).html(html.join(""));
  150. $(select).selectpicker('refresh');
  151. }
  152. }
  153. };
  154. return Controller;
  155. });