classify.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <template>
  2. <view class="content">
  3. <!-- @up-item="upNode"
  4. @down-item="downNode" -->
  5. <view style="margin: 24rpx;">
  6. <uni-segmented-control :current="current" :values="items" :style-type="styleType" :active-color="activeColor" @clickItem="onClickItem" />
  7. </view>
  8. <view class="user-box">
  9. <m-tree class="u-p-l-40" ref="mtree" :defaultProps="defaultProps" :data="tree" :divider="divider"
  10. :edit="edit" :unfold="true" @node-click="nodeClick" @add-item="addNode" @edit-item="editNode"
  11. @delete-item="deleteNode" @finger-action="fingerAction" @long-press="longpressNode">
  12. </m-tree>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. data() {
  19. return {
  20. /*
  21. tree 数据
  22. */
  23. current:0,
  24. styleType: 'button',
  25. items: ['产品', '物料'],
  26. activeColor: '#007aff',
  27. defaultProps: {
  28. id: 'id', // 此项为id项的key
  29. children: 'children', // 此项为修改子集数据的key
  30. label: 'label' // 此项为修改显示数据的key
  31. },
  32. divider: false,
  33. edit: true,
  34. tree: [],
  35. // 1产品分类 2物料分类
  36. type:1,
  37. }
  38. },
  39. onLoad() {
  40. const v = this
  41. v.getTree()
  42. },
  43. methods: {
  44. onClickItem(e){
  45. const v = this
  46. if(e.currentIndex === 0){
  47. v.type = 1
  48. }else{
  49. v.type = 2
  50. }
  51. v.getTree()
  52. },
  53. getTree() {
  54. const v = this
  55. v.$post('/api/basics/classify/tree', {
  56. type: this.type
  57. }).then(res => {
  58. if (res.code === 200) {
  59. v.tree = res.data
  60. }
  61. })
  62. },
  63. //遍历id节点并删除
  64. removeNodeData(datas, id) { //遍历树 获取id数组
  65. for (var i in datas) {
  66. if (id === datas[i].id) {
  67. // datas.push(datas[i]);
  68. console.log('要删除项目:', datas[i].id);
  69. datas.splice(i, 1);
  70. break;
  71. } else {
  72. if (datas[i].children) { //存在子节点就递归
  73. this.removeNodeData(datas[i].children, id);
  74. }
  75. }
  76. }
  77. },
  78. //遍历id节点添加子项
  79. addNodeData(datas, id, nodedata) { //遍历树 获取id数组
  80. var addflag = false;
  81. if (id === 0) {
  82. datas.unshift(nodedata);
  83. addflag = true;
  84. } else {
  85. for (var i in datas) {
  86. console.log(JSON.stringify(datas[i]));
  87. if (id === datas[i].id) {
  88. // datas.push(datas[i]);
  89. console.log('要增加项目:', datas[i].id, nodedata);
  90. datas[i].children.unshift(nodedata);
  91. addflag = true;
  92. break;
  93. } else {
  94. if (datas[i].children) { //存在子节点就递归
  95. this.addNodeData(datas[i].children, id, nodedata);
  96. }
  97. }
  98. }
  99. }
  100. return addflag;
  101. },
  102. //节点点击事件
  103. nodeClick(e) {
  104. console.log('点击的项目', JSON.stringify(e));
  105. },
  106. //节点up按钮点击事件
  107. upNode(e) {
  108. const that = this;
  109. console.log('upNode');
  110. if (e.index != 0) {
  111. // 根据自身需求,自行修改数据处理方法;
  112. // up上移操作,排序并更新tree中对应元素sort属性
  113. var data = {
  114. pnode: {},
  115. itemA: {
  116. id: e.items[e.index - 1].id,
  117. sort: e.items[e.index].sort
  118. },
  119. itemB: {
  120. id: e.items[e.index].id,
  121. sort: e.items[e.index - 1].sort
  122. },
  123. }
  124. that.$refs.mtree.treeSort(that.tree, e.item.pid, data); // 调用组件方法 排序sort 从小到大排序
  125. }
  126. },
  127. //节点down按钮点击事件
  128. downNode(e) {
  129. const that = this;
  130. console.log('downNode');
  131. if (e.index != e.items.length - 1) {
  132. // 根据自身需求,自行修改数据处理方法;
  133. // down下移操作,排序并更新tree中对应元素sort属性
  134. var data = {
  135. pnode: {},
  136. itemA: {
  137. id: e.items[e.index + 1].id,
  138. sort: e.items[e.index].sort
  139. },
  140. itemB: {
  141. id: e.items[e.index].id,
  142. sort: e.items[e.index + 1].sort
  143. },
  144. }
  145. that.$refs.mtree.treeSort(that.tree, e.item.pid, data); // 调用组件方法 排序sort 从小到大排序
  146. }
  147. },
  148. //节点新增按钮点击事件
  149. addNode(e) {
  150. const v = this
  151. //e.pNodeData, e.addContent
  152. console.log('点击的项目add', JSON.stringify(e));
  153. // 根据自身需求,自行修改数据新增方法;
  154. // 可以配合异步请求 执行服务器删除操作
  155. let data = {};
  156. v.$post('/api/basics/classify/add', {
  157. parentId: e.pNodeData.id,
  158. type: v.type,
  159. name: e.addContent,
  160. }).then(res => {
  161. if (res.code === 200) {
  162. v.getTree()
  163. }
  164. })
  165. },
  166. //节点更新按钮点击事件
  167. editNode(e) {
  168. const v = this
  169. //e.pNodeData, e.editContent
  170. console.log('点击的项目add', e);
  171. var parentId = null
  172. function treeRecursion(arr, id) {
  173. for (var i = 0; i < arr.length; i++) {
  174. if (arr[i].id == id) {
  175. console.log(parentId)
  176. return
  177. }
  178. if (arr[i].children && arr[i].children.length != 0) {
  179. parentId = arr[i].id
  180. treeRecursion(arr[i].children, id)
  181. }
  182. }
  183. }
  184. treeRecursion(v.tree, e.pNodeData.id)
  185. v.$post('/api/basics/classify/edit', {
  186. id: e.pNodeData.id,
  187. type: v.type,
  188. name: e.editContent,
  189. parentId: parentId,
  190. }).then(res => {
  191. if (res.code === 200) {
  192. v.getTree()
  193. }
  194. })
  195. },
  196. //节点删除按钮点击事件
  197. deleteNode(e) {
  198. const v = this
  199. console.log('点击的项目delete', JSON.stringify(e));
  200. uni.showModal({
  201. title: '提示',
  202. content: '确认删除该条信息吗?',
  203. success: res => {
  204. if (res.confirm) {
  205. v.$post('/api/basics/classify/delete', {
  206. id: e.id,
  207. type: v.type,
  208. }).then(res => {
  209. if (res.code === 200) {
  210. if (this.removeNodeData(this.tree, e.id)) {
  211. // 可以配合异步请求 执行服务器删除操作
  212. console.log('删除ID:', e, id, '数据');
  213. }
  214. }
  215. })
  216. }
  217. }
  218. })
  219. },
  220. //节点滑动
  221. fingerAction(e) {
  222. console.log('节点滑动fingerAction', JSON.stringify(e));
  223. },
  224. //节点选线长按事件
  225. longpressNode(e) {
  226. console.log('长按的项目longpress', JSON.stringify(e));
  227. }
  228. }
  229. }
  230. </script>
  231. <style>
  232. .user-box .p-t-20 image:nth-child(1){
  233. display: none;
  234. }
  235. .user-box .p-t-20 image:nth-child(2){
  236. display: none;
  237. }
  238. </style>
  239. <style lang="scss">
  240. page {
  241. background-color: #ededed;
  242. }
  243. .user-box {
  244. background-color: #fff;
  245. }
  246. </style>