index.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <el-dialog :close-on-click-modal="false"
  3. :visible.sync="addressView"
  4. append-to-body
  5. class="modal"
  6. title="选择城市" width="950px">
  7. <el-row :gutter="24" type="flex">
  8. <el-col :xl="24" :lg="24" :md="24" :sm="24" :xs="24" class="item">
  9. <div class="check-btn">
  10. <el-checkbox v-model="iSselect" @change="allCheckbox">全选</el-checkbox>
  11. <div class="empty" @click="empty">清空</div>
  12. </div>
  13. </el-col>
  14. </el-row>
  15. <el-row :gutter="24" :loading="loading" >
  16. <el-col :xl="6" :lg="6" :md="6" :sm="8" :xs="6" class="item" v-for="(item,index) in cityList" :key="index">
  17. <div @mouseenter="enter(index)" @mouseleave="leave()" v-if="item.level==1">
  18. <el-checkbox v-model="item.checked" :label="item.cityName" @change="checkedClick(index)">{{item.cityName}}</el-checkbox>
  19. <div class="city" v-show="activeCity===index">
  20. <div class="checkBox">
  21. <div class="arrow"></div>
  22. <div>
  23. <el-checkbox v-model="subitem.checked" :label="subitem.cityName" @change="primary(index,subindex)"
  24. class="itemn" v-for="(subitem,subindex) in item.children" :key="subindex"
  25. >{{subitem.cityName}}</el-checkbox>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. </el-col>
  31. </el-row>
  32. <div slot="footer">
  33. <el-button @click="close">取消</el-button>
  34. <el-button type="primary" @click="confirm">确定</el-button>
  35. </div>
  36. </el-dialog>
  37. </template>
  38. <script>
  39. import {getAllList} from "@/api/hisStore/city";
  40. export default {
  41. name: 'city',
  42. props: {
  43. type: {
  44. type: Number,
  45. default: 0
  46. }
  47. },
  48. data () {
  49. return {
  50. iSselect: false,
  51. addressView: false,
  52. cityList: [],
  53. activeCity: -1,
  54. loading: false
  55. }
  56. },
  57. methods: {
  58. enter (index) {
  59. this.activeCity = index;
  60. },
  61. leave () {
  62. this.activeCity = null;
  63. },
  64. getCityList () {
  65. this.loading = true;
  66. getAllList().then(res => {
  67. this.loading = false;
  68. console.log(res.data)
  69. var data=res.data;
  70. this.cityList=data.filter(item => item.level===1 )
  71. this.cityList.forEach(function(item,index,arr){
  72. var subData=data.filter(subitem => subitem.parentId===item.cityId )
  73. console.log(subData)
  74. item.children=subData;
  75. });
  76. })
  77. },
  78. /**
  79. * 全选或者反选
  80. * @param checked
  81. */
  82. allCheckbox: function () {
  83. let that = this, checked = this.iSselect;
  84. that.cityList.forEach(function (item, key) {
  85. that.$set(that.cityList[key], 'checked', checked);
  86. if (checked) {
  87. that.$set(that.cityList[key], 'count', that.cityList[key].children.length);
  88. } else {
  89. that.$set(that.cityList[key], 'count', 0);
  90. }
  91. that.cityList[key].children.forEach(function (val, k) {
  92. that.$set(that.cityList[key].children[k], 'checked', checked);
  93. })
  94. });
  95. },
  96. // 清空;
  97. empty () {
  98. let that = this;
  99. that.cityList.forEach(function (item, key) {
  100. that.$set(that.cityList[key], 'checked', false);
  101. that.cityList[key].children.forEach(function (val, k) {
  102. that.$set(that.cityList[key].children[k], 'checked', false);
  103. });
  104. that.$set(that.cityList[key], 'count', 0);
  105. });
  106. this.iSselect = false;
  107. },
  108. /**
  109. * 点击省
  110. * @param index
  111. */
  112. checkedClick: function (index) {
  113. let that = this;
  114. if (that.cityList[index].checked) {
  115. that.$set(that.cityList[index], 'count', that.cityList[index].children.length);
  116. that.cityList[index].children.forEach(function (item, key) {
  117. that.$set(that.cityList[index].children[key], 'checked', true);
  118. });
  119. } else {
  120. that.$set(that.cityList[index], 'count', 0);
  121. that.$set(that.cityList[index], 'checked', false);
  122. that.cityList[index].children.forEach(function (item, key) {
  123. that.$set(that.cityList[index].children[key], 'checked', false);
  124. });
  125. that.iSselect = false;
  126. }
  127. },
  128. /**
  129. * 点击市区
  130. * @param index
  131. * @param ind
  132. */
  133. primary: function (index, ind) {
  134. let checked = false, count = 0;
  135. this.cityList[index].children.forEach(function (item, key) {
  136. console.log("item:"+item.checked)
  137. if (item.checked) {
  138. checked = true;
  139. count++;
  140. }
  141. });
  142. this.$set(this.cityList[index], 'count', count);
  143. this.$set(this.cityList[index], 'checked', checked);
  144. },
  145. // 确定;
  146. confirm () {
  147. let that = this;
  148. // 被选中的省市;
  149. let selectList = [];
  150. that.cityList.forEach(function (item, key) {
  151. let data = {};
  152. if (item.checked) {
  153. data = {
  154. name: item.cityName,
  155. cityId: item.cityId,
  156. children: []
  157. };
  158. }
  159. that.cityList[key].children.forEach(function (i, k) {
  160. if (i.checked) {
  161. data.children.push({
  162. cityId: i.cityId
  163. })
  164. }
  165. });
  166. if (data.cityId !== undefined) {
  167. selectList.push(data);
  168. }
  169. });
  170. console.log(selectList);
  171. if (selectList.length === 0) {
  172. return this.$message({
  173. message:'至少选择一个省份或者城市',
  174. type: 'error'
  175. });
  176. } else {
  177. this.$emit('selectCity', selectList, this.type);
  178. that.addressView = false;
  179. this.cityList = []
  180. }
  181. },
  182. close () {
  183. this.addressView = false;
  184. this.cityList = []
  185. }
  186. },
  187. mounted () {
  188. }
  189. }
  190. </script>
  191. <style scoped>
  192. .modal .item {
  193. position: relative;
  194. margin-bottom: 20px;
  195. }
  196. .modal .item .city {
  197. position: absolute;
  198. z-index: 9;
  199. top: 17px;
  200. width: 100%;
  201. padding-top: 18px;
  202. }
  203. .modal .item .city .checkBox {
  204. width: 97%;
  205. padding: 10px;
  206. border: 1px solid #eee;
  207. background-color: #fff;
  208. max-height: 100px;
  209. overflow-x: hidden;
  210. overflow-y: auto;
  211. }
  212. .modal .item .city .checkBox .arrow {
  213. position: absolute;
  214. top: 3px;
  215. width: 0;
  216. height: 0;
  217. border: 8px solid transparent;
  218. border-bottom-color: #ddd;
  219. }
  220. .modal .item .city .checkBox .arrow:before {
  221. position: absolute;
  222. bottom: -8px;
  223. right: -7px;
  224. content: "";
  225. width: 0;
  226. height: 0;
  227. border: 7px solid transparent;
  228. border-bottom-color: #fff;
  229. }
  230. .modal .item .city .checkBox .itemn {
  231. margin-bottom: 10px;
  232. }
  233. .radio {
  234. padding: 5px 0;
  235. font-size: 14px !important;
  236. }
  237. .red {
  238. color: #ff0000;
  239. }
  240. .empty {
  241. cursor: pointer;
  242. margin-left:10px
  243. }
  244. .check-btn{
  245. display: flex;
  246. flex-direction: row;
  247. align-items: center;
  248. justify-content: flex-end;
  249. }
  250. </style>