MultiCheckBox.vue 9.6 KB

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