gjs-selectCity.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <view class="">
  3. <view class="flex">
  4. <scroll-view scroll-y="true" class="box1" :style="{ height: scrollHeight + 'rpx' }">
  5. <view
  6. class="itemClass text-overflow"
  7. v-for="(item, index) in provinceList"
  8. :key="item.cityId"
  9. :class="{ selectedProvice: provinceIndex == index }"
  10. :style="{ color: provinceIndex == index ? selectedColor : '' }"
  11. @click="getClassificationList2(item.cityId, index)"
  12. >
  13. {{ item.name }}
  14. </view>
  15. </scroll-view>
  16. <scroll-view scroll-y="true" class="box2" :style="{ height: scrollHeight + 'rpx' }">
  17. <view
  18. class="itemClass text-overflow"
  19. v-for="(item, index) in cityList"
  20. :key="item.cityId"
  21. :style="{ color: cityIndex == index ? selectedColor : '' }"
  22. @click="getClassificationList3(item.cityId, index)"
  23. >
  24. {{ item.name }}
  25. </view>
  26. </scroll-view>
  27. <scroll-view
  28. scroll-y="true"
  29. class="box3"
  30. v-if="selectType == 'country' && !multiple"
  31. :style="{ height: scrollHeight + 'rpx' }"
  32. :class="{ bl: countyList.length > 0 }"
  33. >
  34. <view
  35. class="itemClass text-overflow"
  36. v-for="(item, index) in countyList"
  37. :key="item.cityId"
  38. :style="{ color: countyIndex == index ? selectedColor : '' }"
  39. @click="selectCounty({ name: item.name, cityId: item.cityId }, index)"
  40. >
  41. {{ item.name }}
  42. </view>
  43. </scroll-view>
  44. <scroll-view
  45. scroll-y="true"
  46. class="box3"
  47. v-else
  48. :style="{ height: scrollHeight + 'rpx' }"
  49. :class="{ bl: countyList.length > 0 }"
  50. >
  51. <!-- <view
  52. class="itemClass text-overflow"
  53. v-for="(item, index) in countyList"
  54. :key="item.cityId"
  55. :style="{ color: countyIndex == index ? selectedColor : '' }"
  56. @click="selectCounty({ name: item.name, cityId: item.cityId }, index)"
  57. >
  58. {{ item.name }}
  59. </view> -->
  60. <u-checkbox-group
  61. v-model="selectValue"
  62. iconPlacement="right"
  63. placement="column"
  64. @change="checkboxChange"
  65. >
  66. <view class="check-box-item" v-for="(item, index) in countyList" :key="index">
  67. <u-checkbox
  68. activeColor="#00d36d"
  69. labelSize="28rpx"
  70. labelColor="#333333"
  71. shape="circle"
  72. :label="item.name"
  73. :name="item.cityId"
  74. ></u-checkbox>
  75. </view>
  76. </u-checkbox-group>
  77. </scroll-view>
  78. </view>
  79. <view class="pop-bottom" v-if="multiple">
  80. <!-- <u-button shape="circle" text="取消" :customStyle="{ width: '300rpx' }"></u-button> -->
  81. <u-button
  82. color="linear-gradient( 275deg, #01CF6C 0%, #07E278 100%);"
  83. shape="circle"
  84. text="确定"
  85. @click="submit"
  86. ></u-button>
  87. </view>
  88. </view>
  89. </template>
  90. <script>
  91. export default {
  92. name: "gjs-selectCity",
  93. props: {
  94. selectType: {
  95. type: String,
  96. default: "country", //province选择省份city选择城市country选择区县
  97. validator: function (value) {
  98. // 这个数组包含所有允许的值
  99. const validValues = ["province", "city", "country"];
  100. // 如果value存在于validValues数组中,则返回true
  101. return validValues.indexOf(value) !== -1;
  102. },
  103. },
  104. scrollHeight: {
  105. type: Number,
  106. default: 572,
  107. },
  108. selectedColor: {
  109. type: String,
  110. default: "#00D36D",
  111. },
  112. multiple: {
  113. type: Boolean,
  114. default: false,
  115. },
  116. },
  117. data() {
  118. return {
  119. provinceIndex: null,
  120. cityIndex: 0,
  121. countyIndex: 0,
  122. provinceList: [],
  123. cityList: [],
  124. countyList: [],
  125. selectCode: [],
  126. provenceName: "",
  127. cityName: "",
  128. // 新增多选
  129. selectValue: [],
  130. };
  131. },
  132. mounted() {
  133. this.getClassificationList1();
  134. },
  135. methods: {
  136. // 获取一级
  137. getClassificationList1() {
  138. let param = {
  139. levelType: "",
  140. parentCityId: "",
  141. };
  142. this.api.getCity(param).then((res) => {
  143. if (res.code == 200) {
  144. this.provinceList = res.data;
  145. }
  146. });
  147. },
  148. // 获取二级
  149. getClassificationList2(id, index) {
  150. this.provinceIndex = index;
  151. this.cityIndex = 0;
  152. this.countyIndex = 0;
  153. // 选择省份后 清除区信息
  154. this.countyList = [];
  155. let param = {
  156. levelType: 2,
  157. parentCityId: id,
  158. };
  159. this.selectCode[0] = id;
  160. this.provenceName = this.provinceList[index].name;
  161. if (this.selectType == "province") {
  162. this.$emit("select", {
  163. name: this.provinceList[index].name,
  164. cityId: id,
  165. });
  166. return;
  167. }
  168. this.api.getCity(param).then((res) => {
  169. if (res.code == 200) {
  170. this.cityList = res.data;
  171. }
  172. });
  173. },
  174. // 获取三级
  175. getClassificationList3(id, index) {
  176. // 选择城市 情况 多选
  177. this.selectValue = [];
  178. this.cityIndex = index;
  179. this.countyIndex = 0;
  180. let param = {
  181. levelType: 3,
  182. parentCityId: id,
  183. };
  184. this.selectCode[1] = id;
  185. this.cityName = this.cityList[index].name;
  186. if (this.selectType == "city") {
  187. this.$emit("select", {
  188. name: this.cityList[index].name,
  189. cityId: id,
  190. selectCode: this.selectCode,
  191. wholeName: this.provenceName + this.cityName,
  192. });
  193. return;
  194. }
  195. this.api.getCity(param).then((res) => {
  196. if (res.code == 200) {
  197. this.countyList = res.data;
  198. }
  199. });
  200. },
  201. selectCounty(item, index) {
  202. this.countyIndex = index;
  203. this.$emit("select", {
  204. name: item.name,
  205. cityId: item.cityId,
  206. });
  207. },
  208. // 新增多选提交按钮
  209. submit() {
  210. // console.log(this.selectValue);
  211. let _arr = this.findObjectsByIds(this.countyList, this.selectValue);
  212. this.$emit("select", _arr);
  213. // console.log(_arr)
  214. },
  215. // 多选事件
  216. checkboxChange(e) {
  217. // console.log(e)
  218. },
  219. // 筛选事件
  220. findObjectsByIds(objArray, numArray) {
  221. return numArray
  222. .map((id) => {
  223. const obj = objArray.filter((item) => item.cityId === id);
  224. return obj.length > 0 ? obj[0] : null;
  225. })
  226. .filter((item) => item !== null);
  227. },
  228. },
  229. };
  230. </script>
  231. <style>
  232. .flex {
  233. display: flex;
  234. }
  235. .box1 {
  236. /* width: 220rpx; */
  237. display: flex;
  238. flex: 1;
  239. background-color: #f5f7f7;
  240. }
  241. .box2 {
  242. display: flex;
  243. flex: 1;
  244. /* width: 220rpx; */
  245. }
  246. .bl {
  247. /* border-left: 1rpx solid #eeeeee; */
  248. }
  249. .box3 {
  250. display: flex;
  251. flex: 1;
  252. /* width: calc(100% - 440rpx); */
  253. }
  254. .itemClass {
  255. height: 90rpx;
  256. line-height: 90rpx;
  257. padding-left: 20rpx;
  258. padding-right: 15rpx;
  259. font-size: 28rpx;
  260. color: #333333;
  261. }
  262. .text-overflow {
  263. display: -webkit-box; /* 使用-webkit-box布局模型 */
  264. -webkit-box-orient: vertical; /* 指定盒子内容的排列方向为垂直 */
  265. -webkit-line-clamp: 1; /* 控制显示的行数,这里设置为1 */
  266. overflow: hidden; /* 当内容超出容器时隐藏多余的内容 */
  267. text-overflow: ellipsis; /* 使用省略号显示多余的文本内容 */
  268. }
  269. .selectedProvice {
  270. background-color: #ffffff;
  271. }
  272. /deep/::-webkit-scrollbar {
  273. display: none;
  274. width: 0;
  275. height: 0;
  276. }
  277. .pop-bottom {
  278. margin: 24rpx;
  279. display: flex;
  280. flex-direction: row;
  281. align-items: center;
  282. justify-content: space-around;
  283. }
  284. .check-box-item {
  285. box-sizing: border-box;
  286. /* padding: 10rpx 15rpx; */
  287. height: 90rpx;
  288. line-height: 90rpx;
  289. padding-left: 20rpx;
  290. padding-right: 36rpx;
  291. /* display: flex;
  292. flex-direction:row;
  293. align-items: center;
  294. justify-content: space-between; */
  295. }
  296. </style>