com-district-single-rule.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <!--注:本组件仅做省市区选择,返回值仅为选中的省市区数组-->
  2. <template id="com-district-single-rule">
  3. <div class="com-district-single">
  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, index)" v-model="item.checked"
  12. :disabled="item.unchecked">
  13. <span style="display: none" class="el-checkbox__label">{{item.name}}</span>
  14. </el-checkbox>
  15. <span class="el-checkbox__label">{{item.name}}
  16. </span>
  17. </div>
  18. </template>
  19. </el-card>
  20. <el-card style="max-height: 400px;overflow-y: auto;margin-right: 20px;" shadow="never"
  21. v-if="list[p_index].children && list[p_index].children.length > 0">
  22. <template v-for="(item, index) in list[p_index].children">
  23. <div class="el-checkbox" @click="c_index = index" style="margin: 0 0 20px;display: block">
  24. <el-checkbox @change="pickerChange(item, index)" v-model="item.checked"
  25. :disabled="item.unchecked">
  26. <span style="display: none" class="el-checkbox__label">{{item.name}}</span>
  27. </el-checkbox>
  28. <span class="el-checkbox__label">{{item.name}}
  29. </span>
  30. </div>
  31. </template>
  32. </el-card>
  33. <el-card style="max-height: 400px;overflow-y: auto;margin-right: 20px;" shadow="never"
  34. v-if="list[p_index].children[c_index].children && list[p_index].children[c_index].children.length > 0">
  35. <template v-for="(item, index) in list[p_index].children[c_index].children">
  36. <div class="el-checkbox" @click="d_index = index" style="margin: 0 0 20px;display: block">
  37. <el-checkbox @change="pickerChange(item, index)" v-model="item.checked"
  38. :disabled="item.unchecked">
  39. <span style="display: none" class="el-checkbox__label">{{item.name}}</span>
  40. </el-checkbox>
  41. <span class="el-checkbox__label">{{item.name}}</span>
  42. </div>
  43. </template>
  44. </el-card>
  45. </template>
  46. </div>
  47. </el-card>
  48. </div>
  49. </template>
  50. <script>
  51. Vue.component('com-district-single-rule', {
  52. template: '#com-district-single-rule',
  53. data: function () {
  54. return {
  55. p_index: 0,
  56. c_index: 0,
  57. d_index: 0,
  58. districtLoading: false,
  59. defaultList: [], // 所有省市区
  60. tempList: [],// 已选择待返回的省市区
  61. list: [], // 渲染页面使用省市区列表
  62. backList: [], // 返回的列表
  63. editIdsChunk: []
  64. }
  65. },
  66. props: {
  67. detail: Array, // 传入的所有省市区数组
  68. edit: Array, // 传入的选中省市区数组
  69. level: Number, // 展示省市区的级数
  70. params: Array,// 传出值
  71. notRule: Array //传入其它不能选择值
  72. },
  73. mounted: function () {
  74. let self = this;
  75. self.districtLoading = true;
  76. request({
  77. params: {
  78. r: 'common/region/list',
  79. level: this.level
  80. },
  81. method: 'get'
  82. }).then(function (e) {
  83. self.districtLoading = false;
  84. if (e.data.code == 0) {
  85. self.defaultList = e.data.data.list;
  86. self.newList();
  87. } else {
  88. self.$message.error(e.data.msg);
  89. }
  90. }).catch(function (e) {
  91. self.districtLoading = false;
  92. });
  93. },
  94. watch: {
  95. edit() {
  96. this.setList();
  97. }
  98. },
  99. methods: {
  100. // 根据传入数据重新获取省市区列表
  101. newList() {
  102. let defaultList = this.checkList(this.defaultList, this.detail, this.edit);
  103. this.list = JSON.parse(JSON.stringify(defaultList));
  104. this.list = this.setSelectedList(this.list, 0)
  105. },
  106. // 判断哪些省市区已经选择
  107. checkList(defaultList, formDetail, edit) {
  108. if (typeof defaultList == 'undefined') {
  109. return [];
  110. }
  111. for (let i in defaultList) {
  112. defaultList[i].checked = false;
  113. defaultList[i].unchecked = false;
  114. defaultList[i].isIndeterminate = false;
  115. if (typeof defaultList[i].children != 'undefined') {
  116. defaultList[i].children = this.checkList(defaultList[i].children, formDetail, edit);
  117. }
  118. if (typeof edit != 'undefined') {
  119. for (let j in edit) {
  120. if (defaultList[i].id == edit[j].id) {
  121. defaultList[i].checked = true;
  122. defaultList[i].unchecked = false;
  123. break;
  124. }
  125. }
  126. }
  127. }
  128. return defaultList;
  129. },
  130. setSelectedList(list, level = 1) {// 设置选中不能再选值
  131. if (typeof list == 'undefined') {
  132. return list;
  133. }
  134. for (let i in list) {
  135. if (typeof list[i].children == 'undefined') {
  136. continue;
  137. }
  138. if (level === 0) {
  139. list[i].children = this.setSelectedList(list[i].children, 0)
  140. }
  141. for (let j in list[i].children) {
  142. if (this.notRule.includes(list[i].children[j].id)) {
  143. list[i].children[j].unchecked = true;
  144. }
  145. }
  146. if (this.notRule.includes(list[i].id)) {
  147. list[i].unchecked = true;
  148. }
  149. list[i].children = this.setSelectedList(list[i].children, 1);
  150. }
  151. return list;
  152. },
  153. setList(list, item) {
  154. if (typeof list != 'undefined') {
  155. for (let i in list) {
  156. if (list[i].id == item.id) {
  157. list[i].checked = item.checked
  158. break;
  159. }
  160. if (typeof list[i].children != 'undefined') {
  161. this.setList(list[i].children, item);
  162. }
  163. }
  164. return list;
  165. } else {
  166. this.newList();
  167. }
  168. },
  169. // 向父组件发送消息
  170. pickerChange(item, index) {
  171. this.checkSelectItem(item);
  172. let tempList = this.setList(this.list, item)
  173. this.list = JSON.parse(JSON.stringify(tempList));
  174. this.$emit('selected', this.backList, this.params);
  175. },
  176. checkSelectItem(item) {
  177. if (item.checked == true) {
  178. this.backList.push({
  179. 'id': item.id,
  180. 'name': item.name,
  181. });
  182. } else {
  183. for (let i in this.backList) {
  184. if (this.backList[i].id == item.id) {
  185. this.backList.splice(i, 1)
  186. }
  187. }
  188. }
  189. },
  190. // 选择省份
  191. selectProvince(index) {
  192. this.p_index = index;
  193. this.c_index = 0;
  194. this.d_index = 0;
  195. },
  196. pushEditIdsChunk(id) {
  197. let ids = []
  198. if (typeof id !== 'object') {
  199. ids.push(id)
  200. } else {
  201. ids = id
  202. }
  203. for (let i in ids) {
  204. let pIndex = this.getModIndex(ids[i])
  205. if (this.editIdsChunk[pIndex] === undefined) {
  206. this.editIdsChunk[pIndex] = []
  207. }
  208. // 是否已经存在 不然会重复
  209. if (this.editIdsChunk[pIndex].indexOf(ids[i]) === -1) {
  210. this.editIdsChunk[pIndex].push(ids[i])
  211. }
  212. }
  213. this.$forceUpdate()
  214. },
  215. getCheckedSelectNum(list, level = 1) {// 获取选中对应个数
  216. let checkedItemsCount = 0;
  217. if (!Array.isArray(list)) {
  218. throw new Error('List must be an array.');
  219. }
  220. if (level === 1) {
  221. list.forEach(item => {
  222. if (Array.isArray(item.children) && item.children.length > 0) {
  223. const hasCheckedChildren = item.children.some(child => child.checked);
  224. if (hasCheckedChildren) {
  225. checkedItemsCount++;
  226. }
  227. }
  228. });
  229. } else {
  230. list.forEach(item => {
  231. if (item.checked) {
  232. checkedItemsCount++;
  233. }
  234. });
  235. }
  236. return checkedItemsCount;
  237. }
  238. }
  239. });
  240. </script>