com-district-single.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <!--注:本组件仅做省市区选择,返回值仅为选中的省市区数组-->
  2. <template id="com-district-single">
  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" :disabled="item.unchecked">
  12. <span style="display: none" class="el-checkbox__label">{{item.name}}</span>
  13. </el-checkbox>
  14. <span class="el-checkbox__label">{{item.name}}</span>
  15. </div>
  16. </template>
  17. </el-card>
  18. <el-card style="max-height: 400px;overflow-y: auto;margin-right: 20px;" shadow="never"
  19. v-if="list[p_index].children && list[p_index].children.length > 0">
  20. <template v-for="(item, index) in list[p_index].children">
  21. <div class="el-checkbox" @click="c_index = index" style="margin: 0 0 20px;display: block">
  22. <el-checkbox @change="pickerChange(item, index)" v-model="item.checked" :disabled="item.unchecked">
  23. <span style="display: none" class="el-checkbox__label">{{item.name}}</span>
  24. </el-checkbox>
  25. <span class="el-checkbox__label">{{item.name}}</span>
  26. </div>
  27. </template>
  28. </el-card>
  29. <el-card style="max-height: 400px;overflow-y: auto;margin-right: 20px;" shadow="never"
  30. v-if="list[p_index].children[c_index].children && list[p_index].children[c_index].children.length > 0">
  31. <template v-for="(item, index) in list[p_index].children[c_index].children">
  32. <div class="el-checkbox" @click="d_index = index" style="margin: 0 0 20px;display: block">
  33. <el-checkbox @change="pickerChange(item, index)" v-model="item.checked" :disabled="item.unchecked">
  34. <span style="display: none" class="el-checkbox__label">{{item.name}}</span>
  35. </el-checkbox>
  36. <span class="el-checkbox__label">{{item.name}}</span>
  37. </div>
  38. </template>
  39. </el-card>
  40. </template>
  41. </div>
  42. </el-card>
  43. </div>
  44. </template>
  45. <script>
  46. Vue.component('com-district-single', {
  47. template: '#com-district-single',
  48. data: function () {
  49. return {
  50. p_index: 0,
  51. c_index: 0,
  52. d_index: 0,
  53. districtLoading: false,
  54. defaultList: [], // 所有省市区
  55. tempList: [],// 已选择待返回的省市区
  56. list: [], // 渲染页面使用省市区列表
  57. backList: [] // 返回的列表
  58. }
  59. },
  60. props: {
  61. detail: Array, // 传入的所有省市区数组
  62. edit: Array, // 传入的选中省市区数组
  63. level: Number, // 展示省市区的级数
  64. params: Array,// 传出值
  65. },
  66. mounted: function () {
  67. let self = this;
  68. self.districtLoading = true;
  69. request({
  70. params: {
  71. r: 'common/region/list',
  72. level: this.level
  73. },
  74. method: 'get'
  75. }).then(function (e) {
  76. self.districtLoading = false;
  77. if (e.data.code == 0) {
  78. self.defaultList = e.data.data.list;
  79. self.newList();
  80. } else {
  81. self.$message.error(e.data.msg);
  82. }
  83. }).catch(function (e) {
  84. self.districtLoading = false;
  85. });
  86. },
  87. watch: {
  88. edit() {
  89. this.setList();
  90. }
  91. },
  92. methods: {
  93. // 根据传入数据重新获取省市区列表
  94. newList() {
  95. let defaultList = this.checkList(this.defaultList, this.detail, this.edit);
  96. this.list = JSON.parse(JSON.stringify(defaultList));
  97. },
  98. // 判断哪些省市区已经选择
  99. checkList(defaultList, formDetail, edit) {
  100. if (typeof defaultList == 'undefined') {
  101. return [];
  102. }
  103. for (let i in defaultList) {
  104. defaultList[i].checked = false;
  105. defaultList[i].unchecked = false;
  106. defaultList[i].isIndeterminate = false;
  107. if (typeof defaultList[i].children != 'undefined') {
  108. defaultList[i].children = this.checkList(defaultList[i].children, formDetail, edit);
  109. }
  110. if (typeof edit != 'undefined') {
  111. for (let j in edit) {
  112. if (defaultList[i].id == edit[j].id) {
  113. defaultList[i].checked = true;
  114. defaultList[i].unchecked = false;
  115. break;
  116. }
  117. }
  118. }
  119. }
  120. return defaultList;
  121. },
  122. setList(list, item) {
  123. if (typeof list != 'undefined') {
  124. for (let i in list) {
  125. if (list[i].id == item.id) {
  126. list[i].checked = item.checked
  127. break;
  128. }
  129. if (typeof list[i].children != 'undefined') {
  130. this.setList(list[i].children, item);
  131. }
  132. }
  133. return list;
  134. } else {
  135. this.newList();
  136. }
  137. },
  138. // 向父组件发送消息
  139. pickerChange(item, index) {
  140. this.checkSelectItem(item);
  141. let tempList = this.setList(this.list, item)
  142. this.list = JSON.parse(JSON.stringify(tempList));
  143. this.$emit('selected', this.backList, this.params);
  144. },
  145. checkSelectItem(item) {
  146. if (item.checked == true) {
  147. this.backList.push({
  148. 'id': item.id,
  149. 'name': item.name,
  150. });
  151. } else {
  152. for(let i in this.backList) {
  153. if (this.backList[i].id == item.id) {
  154. this.backList.splice(i, 1)
  155. }
  156. }
  157. }
  158. },
  159. // 选择省份
  160. selectProvince(index) {
  161. this.p_index = index;
  162. this.c_index = 0;
  163. this.d_index = 0;
  164. }
  165. }
  166. });
  167. </script>