MultiCheckBox.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <div class="multiCheckbox">
  3. <div class="linkMulti_list" ref="mycheckMul">
  4. <div class="title">{{title}}</div>
  5. <ul class="linkMulti_list_one">
  6. <li class="checkAll">
  7. <a-checkbox
  8. :indeterminate="indeterminate"
  9. :checked="checkAll"
  10. @change="onCheckAllChange"
  11. >全选</a-checkbox>
  12. </li>
  13. <li
  14. @click.stop="listItemClick($event,item)"
  15. v-for="(item,index) in listArr"
  16. :key="index"
  17. :data-layers="curLayerNum"
  18. ref="checkList"
  19. >
  20. <a-checkbox
  21. @change="onChange($event,item,index)"
  22. :value="item.value"
  23. :checked="item.checked"
  24. @click.stop="listItemClick($event,item)"
  25. >{{item.name}}</a-checkbox>
  26. <!-- <span>{{item.name}}</span> -->
  27. <span class="more" v-show="item.children">
  28. <a-icon type="right" />
  29. </span>
  30. </li>
  31. </ul>
  32. </div>
  33. <!-- <MultiCheckBox class="childClass" :title='currentItem.name' :listArr='currentItem.children' v-if="currentItem" :curLayerNum='currentLayerNum' /> -->
  34. </div>
  35. </template>
  36. <script>
  37. export default {
  38. name: 'MultiCheckBox',
  39. props: ['title', 'listArr', 'curLayerNum', 'parentChecked'],
  40. data() {
  41. return {
  42. checkedList: [],
  43. indeterminate: false,
  44. checkAll: false,
  45. plainOptions: [],
  46. layersNum: 0, //控制层级
  47. currentLayerNum: 0, //当前层级是多少层,
  48. currentItem: null
  49. }
  50. },
  51. watch: {
  52. checkAll: function(n, o) {
  53. if (n != o) {
  54. // this.onCheckAllChange()
  55. }
  56. },
  57. // listArr(n,o){
  58. // this.listArr.forEach(element => {
  59. // this.plainOptions.push(element.value);
  60. // if(element.checked){
  61. // this.checkedList.push(element.value)
  62. // }
  63. // });
  64. // if(this.checkedList.length==this.plainOptions.length){
  65. // this.checkAll=true;
  66. // }
  67. // },
  68. parentChecked: {
  69. handler(n, o) {
  70. console.log(n, o, '--parentChecked', this.curLayerNum)
  71. if (n) {
  72. this.checkAll = true
  73. this.listArr.forEach((item, index) => {
  74. item.checked = n
  75. this.checkedList.push(item.value)
  76. })
  77. } else if (n == false && o == true) {
  78. this.checkAll = false
  79. this.listArr.forEach((item, index) => {
  80. item.checked = n
  81. })
  82. this.checkedList = []
  83. }
  84. },
  85. immediate: true,
  86. deep: true
  87. }
  88. },
  89. methods: {
  90. onChange(e, item, index) {
  91. // console.log(e.target,item)
  92. if (e.target.checked) {
  93. // item.checked=true;
  94. this.listArr.forEach((ele, i) => {
  95. if (index == i) {
  96. this.$set(ele, 'checked', e.target.checked)
  97. this.checkedList.push(e.target.value)
  98. }
  99. })
  100. } else {
  101. this.listArr.forEach((ele, i) => {
  102. if (index == i) {
  103. this.$set(ele, 'checked', e.target.checked)
  104. }
  105. })
  106. let num = this.checkedList.indexOf(e.target.value)
  107. if (num > -1) {
  108. this.checkedList.splice(num, 1)
  109. }
  110. }
  111. this.indeterminate = !!this.checkedList.length && this.checkedList.length < this.plainOptions.length
  112. this.checkAll = this.checkedList.length === this.plainOptions.length
  113. // this.listItemClick(item);
  114. },
  115. onCheckAllChange(e) {
  116. Object.assign(this, {
  117. checkedList: e.target.checked ? this.plainOptions : [],
  118. indeterminate: false,
  119. checkAll: e.target.checked
  120. })
  121. if (this.checkedList.length) {
  122. this.listArr.forEach(item => {
  123. this.$set(item, 'checked', true)
  124. })
  125. } else {
  126. this.listArr.forEach(item => {
  127. this.$set(item, 'checked', false)
  128. })
  129. }
  130. },
  131. listItemClick(event, item) {
  132. console.log(item)
  133. this.$emit('getItemData', {
  134. clickObj: event.target,
  135. data: item
  136. })
  137. },
  138. // checkItemChange(e){
  139. // console.log(e.target.checked);
  140. // // if(e.target.checked){
  141. // // }
  142. // },
  143. //判断传入的数组的children层数
  144. getLayersNum(arr) {
  145. arr.map((item, index) => {
  146. if (item.children) {
  147. }
  148. })
  149. }
  150. },
  151. mounted() {
  152. this.currentLayerNum = this.curLayerNum
  153. this.listArr.forEach(element => {
  154. this.plainOptions.push(element.value)
  155. if (element.checked) {
  156. this.checkedList.push(element.value)
  157. }
  158. })
  159. if (this.checkedList.length == this.plainOptions.length) {
  160. this.checkAll = true
  161. }
  162. // console.log(this.checkedList)
  163. // console.log(this.curLayerNum)
  164. // console.log(this.parentChecked)
  165. console.log(this.$refs.checkList)
  166. if (this.$refs.checkList) {
  167. let refsCheckListArr = Array.from(this.$refs.checkList)
  168. refsCheckListArr.forEach((item, index) => {
  169. let nodeArr = Array.from(item.children[0].children)
  170. // console.log(nodeArr)
  171. nodeArr.forEach((ele, i) => {
  172. if (i == 0) {
  173. let nodeArr2 = Array.from(ele.children)
  174. console.log(nodeArr2)
  175. nodeArr2.forEach(element => {
  176. element.dataset['layers'] = this.curLayerNum
  177. })
  178. } else {
  179. ele.dataset['layers'] = this.curLayerNum
  180. }
  181. })
  182. })
  183. }
  184. }
  185. }
  186. </script>
  187. <style lang="scss" scoped>
  188. .multiCheckbox {
  189. width: 100%;
  190. height: 300px;
  191. ul,
  192. li {
  193. margin: 0;
  194. padding: 0;
  195. list-style: none;
  196. }
  197. .linkMulti_list {
  198. // padding: 5px 0px;
  199. display: inline-block;
  200. width: 100%;
  201. height: 100%;
  202. box-sizing: border-box;
  203. overflow: hidden;
  204. .title {
  205. height: 36px;
  206. line-height: 36px;
  207. font-size: 14px;
  208. font-weight: 600;
  209. border-radius: 5px 5px 0 0;
  210. background-color: rgb(248, 249, 250);
  211. padding: 0 15px;
  212. border-bottom: 1px solid #ccc;
  213. box-sizing: border-box;
  214. }
  215. ul {
  216. height: 264px;
  217. overflow: auto;
  218. }
  219. .checkAll {
  220. }
  221. li {
  222. height: 30px;
  223. line-height: 30px;
  224. cursor: pointer;
  225. padding: 0 15px;
  226. position: relative;
  227. .more {
  228. position: absolute;
  229. top: 2px;
  230. right: 10px;
  231. }
  232. }
  233. li:hover {
  234. background-color: rgb(240, 240, 240);
  235. }
  236. }
  237. .ant-checkbox-group {
  238. width: 100%;
  239. }
  240. }
  241. </style>