com-district.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <!--注:本组件仅做省市区选择,返回值仅为选中的省市区数组-->
  2. <template id="com-district">
  3. <div class="com-district">
  4. <el-card v-loading="districtLoading" shadow="never" body-style="padding:0" style="border:0;min-height: 100px;">
  5. <div flex="dir:left box:mean">
  6. <template v-if="list.length > 0">
  7. <el-card style="max-height: 400px;overflow-y: auto;margin-right: 20px;" shadow="never">
  8. <template v-for="(item, index) in list">
  9. <div class="el-checkbox" style="margin: 0 0 20px;display: block"
  10. @click="selectProvince(index)">
  11. <el-checkbox @change="pickerChange(item)"
  12. v-model="item.checked"
  13. :indeterminate="item.isIndeterminate"
  14. :disabled="item.unchecked">
  15. <span style="display: none" class="el-checkbox__label">{{item.name}}</span>
  16. </el-checkbox>
  17. <span class="el-checkbox__label">{{item.name}}</span>
  18. </div>
  19. </template>
  20. </el-card>
  21. <el-card style="max-height: 400px;overflow-y: auto;margin-right: 20px;" shadow="never"
  22. v-if="list[p_index].children && list[p_index].children.length > 0">
  23. <template v-for="(item, index) in list[p_index].children">
  24. <div class="el-checkbox" @click="c_index = index" style="margin: 0 0 20px;display: block">
  25. <el-checkbox @change="pickerChange(item)"
  26. v-model="item.checked"
  27. :indeterminate="item.isIndeterminate"
  28. :disabled="item.unchecked">
  29. <span style="display: none" class="el-checkbox__label">{{item.name}}</span>
  30. </el-checkbox>
  31. <span class="el-checkbox__label">{{item.name}}</span>
  32. </div>
  33. </template>
  34. </el-card>
  35. <el-card style="max-height: 400px;overflow-y: auto;margin-right: 20px;" shadow="never"
  36. v-if="list[p_index].children[c_index].children && list[p_index].children[c_index].children.length > 0">
  37. <template v-for="(item, index) in list[p_index].children[c_index].children">
  38. <div class="el-checkbox" @click="d_index = index" style="margin: 0 0 20px;display: block">
  39. <el-checkbox @change="pickerChange(item)"
  40. v-model="item.checked"
  41. :indeterminate="item.isIndeterminate"
  42. :disabled="item.unchecked">
  43. <span style="display: none" class="el-checkbox__label">{{item.name}}</span>
  44. </el-checkbox>
  45. <span class="el-checkbox__label">{{item.name}}</span>
  46. </div>
  47. </template>
  48. </el-card>
  49. <!-- :checked="editIds.indexOf(item.id) > -1"-->
  50. <el-card style="max-height: 400px;overflow-y: auto;margin-right: 20px;" shadow="never"
  51. v-if="level == 4 && list[p_index].children[c_index].children[d_index].children && list[p_index].children[c_index].children[d_index].children.length > 0">
  52. <template v-for="(item, index) in list[p_index].children[c_index].children[d_index].children">
  53. <div class="el-checkbox" @click="t_index = index" style="margin: 0 0 20px;display: block">
  54. <el-checkbox @change="pickerChange(item)"
  55. v-model="item.checked"
  56. :disabled="item.unchecked">
  57. <span style="display: none" class="el-checkbox__label">{{item.name}}</span>
  58. </el-checkbox>
  59. <span class="el-checkbox__label">{{item.name}}</span>
  60. </div>
  61. </template>
  62. </el-card>
  63. </template>
  64. </div>
  65. </el-card>
  66. </div>
  67. </template>
  68. <script>
  69. Vue.component('com-district', {
  70. template: '#com-district',
  71. data: function () {
  72. return {
  73. p_index: 0,
  74. c_index: 0,
  75. d_index: 0,
  76. t_index: 0,
  77. districtLoading: false,
  78. defaultList: [], // 所有省市区
  79. tempList: [],// 已选择待返回的省市区
  80. list: [], // 渲染页面使用省市区列表
  81. isIndeterminateIds: [], //
  82. editIdsChunk: []
  83. }
  84. },
  85. props: {
  86. detail: Array, // 传入的所有省市区数组
  87. edit: Array, // 传入的选中省市区数组
  88. level: Number, // 展示省市区的级数
  89. params: Array, // 传出值
  90. mall_id: Number,
  91. is_area_limit_town: Number,
  92. dialogVisibles:Boolean
  93. },
  94. mounted: function () {
  95. let self = this;
  96. self.districtLoading = true;
  97. request({
  98. params: {
  99. r: 'common/region/list',
  100. level: this.level,
  101. mall_id: this.mall_id,
  102. is_area_limit_town: this.is_area_limit_town
  103. },
  104. method: 'get'
  105. }).then(function (e) {
  106. self.districtLoading = false;
  107. if (e.data.code == 0) {
  108. self.defaultList = e.data.data.list;
  109. self.newList();
  110. } else {
  111. self.$message.error(e.data.msg);
  112. }
  113. }).catch(function (e) {
  114. self.districtLoading = false;
  115. });
  116. },
  117. watch: {
  118. // 监听传入的省市区数组
  119. detail() {
  120. this.newList();
  121. },
  122. edit() {
  123. this.newList();
  124. },
  125. dialogVisibles(newVal) {
  126. if(newVal){
  127. this.resetNewList();
  128. }
  129. }
  130. },
  131. methods: {
  132. // 根据传入数据重新获取省市区列表
  133. newList() {
  134. if (this.editIdsChunk.length === 0) {
  135. this.pushEditIdsChunk(this.edit.map(val => {
  136. return val.id
  137. }))
  138. let defaultList = this.checkList(JSON.parse(JSON.stringify(this.defaultList)), this.edit);
  139. let listP = this.setListUnchecked(defaultList, 1);
  140. let list = this.setListUnchecked(listP, 0);
  141. this.list = JSON.parse(JSON.stringify(list));
  142. }
  143. },
  144. resetNewList() {
  145. this.editIdsChunk = []
  146. if (this.editIdsChunk.length === 0) {
  147. this.pushEditIdsChunk(this.edit.map(val => {
  148. return val.id
  149. }))
  150. let defaultList = this.checkList(JSON.parse(JSON.stringify(this.defaultList)), this.edit);
  151. let listP = this.setListUnchecked(defaultList, 1);
  152. let list = this.setListUnchecked(listP, 0);
  153. this.list = JSON.parse(JSON.stringify(list));
  154. this.selectProvince(0)
  155. }
  156. },
  157. // 判断哪些省市区已经选择
  158. checkList(defaultList, formDetail, edit) {
  159. if (typeof defaultList == 'undefined') {
  160. return [];
  161. }
  162. for (let i in defaultList) {
  163. defaultList[i].checked = false;
  164. defaultList[i].unchecked = false;
  165. defaultList[i].isIndeterminate = false;
  166. if (typeof defaultList[i].children != 'undefined') {
  167. defaultList[i].children = this.checkList(defaultList[i].children, formDetail, edit);
  168. }
  169. if (typeof formDetail != 'undefined') {
  170. for (let j in formDetail) {
  171. if (typeof formDetail[j].children != 'undefined') {
  172. for (let k in formDetail[j].children) {
  173. if (defaultList[i].id == formDetail[j].children[k].id) {
  174. defaultList[i].unchecked = true;
  175. break;
  176. }
  177. }
  178. } else {
  179. if (defaultList[i].id == formDetail[j].id) {
  180. defaultList[i].unchecked = true;
  181. break;
  182. }
  183. }
  184. }
  185. }
  186. if (this.editIdsChunk.length > 0) {
  187. defaultList[i].checked = this.isInEditIdsChunk(defaultList[i].id) > -1
  188. defaultList[i].unchecked = false;
  189. }
  190. }
  191. return defaultList;
  192. },
  193. // 重新赋值已经选择的省市区level == 1父级影响子级||level == 0子级影响父级
  194. setListUnchecked(list, level = 1) {
  195. if (typeof list == 'undefined') {
  196. return list;
  197. }
  198. for (let i in list) {
  199. if (typeof list[i].children != 'undefined') {
  200. if(level == 0) {
  201. list[i].children = this.setListUnchecked(list[i].children, 0);
  202. }
  203. let unchecked = false;
  204. let checkCount = 0;
  205. let isIndeterminateCount = 0;
  206. for (let j in list[i].children) {
  207. if (list[i].unchecked && level == 1) {
  208. list[i].children[j].unchecked = true;
  209. }
  210. if (list[i].children[j].unchecked) {
  211. unchecked = true;
  212. }
  213. if (list[i].checked) {
  214. list[i].children[j].checked = true;
  215. }
  216. if(list[i].children[j].checked) {
  217. checkCount++;
  218. }
  219. if(list[i].children[j].isIndeterminate) {
  220. isIndeterminateCount++;
  221. }
  222. /*
  223. let _unchecked = false;
  224. let _checkCount = 0;
  225. let _isIndeterminateCount = 0;
  226. for (let k in list[i].children[j].children) {
  227. if (list[i].children[j].unchecked && level == 2) {
  228. list[i].children[j].children[j].unchecked = true;
  229. }
  230. if (list[i].children[j].children[j].unchecked) {
  231. _unchecked = true;
  232. }
  233. if (list[i].children[j].checked) {
  234. list[i].children[j].children[j].checked = true;
  235. }
  236. if(list[i].children[j].children[j].checked) {
  237. _checkCount++;
  238. }
  239. if(list[i].children[j].children[j].isIndeterminate) {
  240. _isIndeterminateCount++;
  241. }
  242. }
  243. if (_unchecked && level == 2) {
  244. list[i].children[j].unchecked = true
  245. }
  246. if(list[i].children[j].children.length == _checkCount) {
  247. list[i].children[j].checked = true;
  248. } else if (_checkCount > 0) {
  249. list[i].children[j].checked = false;
  250. list[i].children[j].isIndeterminate = true;
  251. } else {
  252. list[i].children[j].checked = false;
  253. if(_isIndeterminateCount > 0) {
  254. list[i].children[j].isIndeterminate = true;
  255. } else {
  256. list[i].children[j].isIndeterminate = false;
  257. }
  258. }
  259. if(level == 2) {
  260. list[i].children[j].children = this.setListUnchecked(list[i].children[j].children, 2);
  261. }*/
  262. }
  263. if (unchecked && level == 0) {
  264. list[i].unchecked = true;
  265. }
  266. if(list[i].children.length == checkCount) {
  267. list[i].checked = true;
  268. } else if (checkCount > 0) {
  269. list[i].checked = false;
  270. list[i].isIndeterminate = true;
  271. } else {
  272. list[i].checked = false;
  273. if(isIndeterminateCount > 0) {
  274. list[i].isIndeterminate = true;
  275. } else {
  276. list[i].isIndeterminate = false;
  277. }
  278. }
  279. // if (list[i].isIndeterminate) console.log('list[i].isIndeterminate', list[i].id)
  280. if(level == 1) {
  281. list[i].children = this.setListUnchecked(list[i].children, 1);
  282. }
  283. }
  284. }
  285. return list;
  286. },
  287. // 将当前值的子列表的选中状态与当前值一致
  288. changeAllList(list, checked) {
  289. let ids = []
  290. if (typeof list != 'undefined') {
  291. for (let i in list) {
  292. if (typeof list[i].children != 'undefined') {
  293. list[i].children = this.changeAllList(list[i].children, checked);
  294. }
  295. list[i].checked = checked;
  296. // if (checked && this.editIds.indexOf(list[i].id) === -1) {
  297. if (checked && this.isInEditIdsChunk(list[i].id) === -1) {
  298. ids.push(list[i].id)
  299. }
  300. // if (!checked && this.editIds.indexOf(list[i].id) > -1) {
  301. if (!checked && this.isInEditIdsChunk(list[i].id) > -1) {
  302. // this.editIds.splice(this.editIds.indexOf(list[i].id), 1)
  303. this.spliceEditIdsChunk(list[i].id)
  304. }
  305. }
  306. if (ids.length > 0) {
  307. this.pushEditIdsChunk(ids)
  308. }
  309. }
  310. return list;
  311. },
  312. // 重置所有省市区的选中状态 返回已选中的省市区
  313. setTempList(list) {
  314. let newList = [];
  315. let checkCount = 0;
  316. let isIndeterminateCount = 0;
  317. for (let i in list) { // 省
  318. let iChecked = true
  319. let iIsIndeterminate = false
  320. if (typeof list[i].children != 'undefined') { // 市
  321. for (let i1 in list[i].children) {
  322. let i1Checked = true
  323. let i1IsIndeterminate = false
  324. if (typeof list[i].children[i1].children != 'undefined') { // 区
  325. for (let i2 in list[i].children[i1].children) {
  326. // 如果第三级被选中,那么就不处理第四级
  327. let i2Checked = true
  328. let i2IsIndeterminate = false
  329. // 全选,部分选,全不选
  330. if (typeof list[i].children[i1].children[i2].children != 'undefined') { // 镇
  331. for (let i3 in list[i].children[i1].children[i2].children) {
  332. if (this.isInEditIdsChunk(list[i].children[i1].children[i2].children[i3].id) > -1) {
  333. newList.push({
  334. id: list[i].children[i1].children[i2].children[i3].id,
  335. name: list[i].children[i1].children[i2].children[i3].name,
  336. parent_id: list[i].children[i1].children[i2].children[i3].parent_id
  337. });
  338. i2IsIndeterminate = true
  339. i1IsIndeterminate = true
  340. iIsIndeterminate = true // 有一个为TRUE那么就肯定为TRUE
  341. } else {
  342. i2Checked = false;
  343. }
  344. }
  345. } else {
  346. i2Checked = this.isInEditIdsChunk(list[i].children[i1].children[i2].id) > -1
  347. }
  348. list[i].children[i1].children[i2].checked = i2Checked
  349. list[i].children[i1].children[i2].isIndeterminate = i2Checked ? false : i2IsIndeterminate
  350. if (i2Checked) {
  351. this.pushEditIdsChunk(list[i].children[i1].children[i2].id)
  352. i1IsIndeterminate = true
  353. iIsIndeterminate = true // 有一个为TRUE那么就肯定为TRUE
  354. newList.push({
  355. id: list[i].children[i1].children[i2].id,
  356. name: list[i].children[i1].children[i2].name,
  357. parent_id: list[i].children[i1].children[i2].parent_id
  358. });
  359. } else {
  360. i1Checked = false
  361. this.spliceEditIdsChunk(list[i].children[i1].children[i2].id)
  362. }
  363. }
  364. } else {
  365. i1Checked = this.isInEditIdsChunk(list[i].children[i1].id) > -1
  366. }
  367. list[i].children[i1].checked = i1Checked
  368. list[i].children[i1].isIndeterminate = i1Checked ? false : i1IsIndeterminate
  369. if (i1Checked) {
  370. this.pushEditIdsChunk(list[i].children[i1].id)
  371. iIsIndeterminate = true
  372. newList.push({
  373. id: list[i].children[i1].id,
  374. name: list[i].children[i1].name,
  375. parent_id: list[i].children[i1].parent_id
  376. });
  377. } else {
  378. iChecked = false
  379. this.spliceEditIdsChunk(list[i].children[i1].id)
  380. }
  381. }
  382. } else {
  383. iChecked = this.isInEditIdsChunk(list[i].id) > -1
  384. }
  385. list[i].checked = iChecked
  386. list[i].isIndeterminate = iChecked ? false : iIsIndeterminate
  387. if (iChecked) {
  388. this.pushEditIdsChunk(list[i].id)
  389. newList.push({
  390. id: list[i].id,
  391. name: list[i].name,
  392. parent_id: list[i].parent_id
  393. });
  394. } else {
  395. this.spliceEditIdsChunk(list[i].id)
  396. }
  397. }
  398. /*
  399. for (let i in list) {
  400. if (typeof list[i].children != 'undefined') {
  401. let childList = this.setTempList(list[i].children);
  402. if (childList.isIndeterminateCount > 0) {
  403. // list[i].checked = false;
  404. // list[i].isIndeterminate = true;
  405. // this.setIsIndeterminateIds(list[i].id, true)
  406. // isIndeterminateCount++;
  407. // newList = newList.concat(childList.newList);
  408. } else if (childList.checkCount < list[i].children.length) {
  409. // list[i].checked = false;
  410. if (childList.checkCount > 0) {
  411. // list[i].isIndeterminate = true;
  412. // this.setIsIndeterminateIds(list[i].id, true)
  413. // isIndeterminateCount++;
  414. // newList = newList.concat(childList.newList);
  415. } else {
  416. // list[i].isIndeterminate = false;
  417. // this.setIsIndeterminateIds(list[i].id, false)
  418. }
  419. } else if(childList.checkCount == list[i].children.length){
  420. // console.log('setTempList', list[i].id, list[i].children, childList)
  421. // list[i].checked = true;
  422. // list[i].isIndeterminate = false;
  423. // this.setIsIndeterminateIds(list[i].id, false)
  424. // newList = newList.concat(childList.newList);
  425. }else {
  426. if (list[i].children > 0) {
  427. // list[i].checked = true;
  428. } else {
  429. // list[i].checked = false;
  430. }
  431. // list[i].isIndeterminate = false;
  432. // this.setIsIndeterminateIds(list[i].id, false)
  433. }
  434. }
  435. // if (list[i].checked) {
  436. if (this.editIds.indexOf(list[i].id) > -1) {
  437. // checkCount++;
  438. // newList.push(list[i]);
  439. }
  440. }*/
  441. let _ = this
  442. setTimeout(() => {
  443. _.list = list
  444. }, 5)
  445. return {
  446. newList: newList,
  447. checkCount: checkCount,
  448. isIndeterminateCount: isIndeterminateCount
  449. };
  450. },
  451. // 向父组件发送消息
  452. pickerChange(item) {
  453. if (item.checked) {
  454. this.pushEditIdsChunk(item.id)
  455. } else {
  456. this.spliceEditIdsChunk(item.id)
  457. }
  458. this.changeAllList(item.children, item.checked);
  459. let tempList = this.setTempList(this.list);
  460. this.tempList = tempList.newList;
  461. this.$forceUpdate()
  462. // 社区团购的基础设置中需要根据下级获取上级ID所以此处传递列表返回
  463. this.$emit('selected', this.tempList, this.params, this.list);
  464. },
  465. // 选择省份
  466. selectProvince(index) {
  467. this.p_index = index;
  468. this.c_index = 0;
  469. this.d_index = 0;
  470. this.t_index = 0;
  471. },
  472. setIsIndeterminateIds(id, value) {
  473. let index = this.isIndeterminateIds.indexOf(id)
  474. if (index === -1 && value) {
  475. this.isIndeterminateIds.push(id)
  476. }
  477. if (!value && index > -1) {
  478. this.isIndeterminateIds.splice(index, 1);
  479. }
  480. },
  481. isInEditIdsChunk(id) {
  482. let pIndex = this.getModIndex(id)
  483. let index = -1;
  484. if (this.editIdsChunk[pIndex] !== undefined) {
  485. index = this.editIdsChunk[pIndex].indexOf(id)
  486. }
  487. return index
  488. },
  489. getModIndex(id) {
  490. if (isNaN(parseInt(id))) {
  491. id = parseInt(id.substring(id.startsWith("__") ? 2 : 1))
  492. }
  493. return id % 100
  494. },
  495. pushEditIdsChunk(id) {
  496. let ids = []
  497. if (typeof id !== 'object') {
  498. ids.push(id)
  499. } else {
  500. ids = id
  501. }
  502. for (let i in ids) {
  503. let pIndex = this.getModIndex(ids[i])
  504. if (this.editIdsChunk[pIndex] === undefined) {
  505. this.editIdsChunk[pIndex] = []
  506. }
  507. // 是否已经存在 不然会重复
  508. if (this.editIdsChunk[pIndex].indexOf(ids[i]) === -1) {
  509. this.editIdsChunk[pIndex].push(ids[i])
  510. }
  511. }
  512. this.$forceUpdate()
  513. },
  514. spliceEditIdsChunk(id) {
  515. let pIndex = this.getModIndex(id)
  516. let index = this.isInEditIdsChunk(id)
  517. if ('spliceEditIdsChunk', index > -1) {
  518. this.editIdsChunk[pIndex].splice(index, 1)
  519. let _ = this
  520. _.$forceUpdate()
  521. }
  522. }
  523. }
  524. });
  525. </script>