goods_install.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ($, undefined, Backend, Table, Form) {
  2. $.jstree.core.prototype.get_all_checked = function (full) {
  3. var obj = this.get_selected(), i, j;
  4. for (i = 0, j = obj.length; i < j; i++) {
  5. obj = obj.concat(this.get_node(obj[i]).parents);
  6. }
  7. obj = $.grep(obj, function (v, i, a) {
  8. return v != '#';
  9. });
  10. obj = obj.filter(function (itm, i, a) {
  11. return i == a.indexOf(itm);
  12. });
  13. return full ? $.map(obj, $.proxy(function (i) {
  14. return this.get_node(i);
  15. }, this)) : obj;
  16. };
  17. let extend;
  18. var Controller = {
  19. index: function () {
  20. // 初始化表格参数配置
  21. Table.api.init({
  22. extend: extend={
  23. index_url: 'goods_install/index' + location.search,
  24. add_url: 'goods_install/add',
  25. edit_url: 'goods_install/edit',
  26. del_url: 'goods_install/del',
  27. multi_url: 'goods_install/multi',
  28. import_url: 'goods_install/import',
  29. table: 'goods_install',
  30. }
  31. });
  32. var table = $("#table");
  33. // 初始化表格
  34. table.bootstrapTable({
  35. url: $.fn.bootstrapTable.defaults.extend.index_url,
  36. pk: 'id',
  37. sortName: 'id',
  38. columns: [
  39. [
  40. {checkbox: true},
  41. {field: 'id', title: __('Id')},
  42. {field: 'name', title: __('Name')},
  43. {field: 'create_time', title: __('Create_time')},
  44. {field: 'update_time', title: __('Update_time')},
  45. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  46. ]
  47. ]
  48. });
  49. // 为表格绑定事件
  50. Table.api.bindevent(table);
  51. },
  52. add: function () {
  53. Controller.api.bindevent();
  54. $('.addOne').click(function (){
  55. let html=$('.amountItem')[0].outerHTML,time=Date.now()
  56. html=html.replace(/value="\s?"/g,'').replace(/\[fee]\[\d+]/g,`[fee][${time}]`)
  57. $(this).parent().parent().before(html)
  58. })
  59. $(document).on('click','.delOne',function (){
  60. if($('.amountItem').length===1){
  61. return
  62. }
  63. $(this).parent().parent().remove()
  64. })
  65. },
  66. edit: function () {
  67. Controller.api.bindevent();
  68. },
  69. api: {
  70. bindevent: function () {
  71. Form.api.bindevent($("form[role=form]"), null, null, function () {
  72. if ($("#treeview").size() > 0) {
  73. var r = $("#treeview").jstree("get_all_checked");
  74. $("input[name='row[goods]']").val(r.join(','));
  75. }
  76. return true;
  77. });
  78. //渲染权限节点树
  79. //变更级别后需要重建节点树
  80. var id = 1;
  81. $.ajax({
  82. url: "goods_install/goods",
  83. type: 'post',
  84. dataType: 'json',
  85. data: {id: id},
  86. success: function (ret) {
  87. if (ret.hasOwnProperty("code")) {
  88. var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : "";
  89. if (ret.code === 1) {
  90. //销毁已有的节点树
  91. $("#treeview").jstree("destroy");
  92. Controller.api.rendertree(data);
  93. } else {
  94. Backend.api.toastr.error(ret.msg);
  95. }
  96. }
  97. }, error: function (e) {
  98. Backend.api.toastr.error(e.message);
  99. }
  100. });
  101. //全选和展开
  102. $(document).on("click", "#checkall", function () {
  103. $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
  104. });
  105. $(document).on("click", "#expandall", function () {
  106. $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
  107. });
  108. },
  109. rendertree: function (content) {
  110. $("#treeview")
  111. .on('redraw.jstree', function (e) {
  112. $(".layer-footer").attr("domrefresh", Math.random());
  113. })
  114. .jstree({
  115. "themes": {"stripes": true},
  116. "checkbox": {
  117. "keep_selected_style": false,
  118. },
  119. "types": {
  120. "root": {
  121. "icon": "fa fa-folder-open",
  122. },
  123. "menu": {
  124. "icon": "fa fa-folder-open",
  125. },
  126. "file": {
  127. "icon": "fa fa-file-o",
  128. }
  129. },
  130. "plugins": ["checkbox", "types"],
  131. "core": {
  132. 'check_callback': true,
  133. "data": content
  134. }
  135. });
  136. }
  137. }
  138. };
  139. return Controller;
  140. });