region-picker.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // 支持添加默认id参数
  2. // 支持默认选项添加到省市区第一个选项
  3. // 支持确认后再更新选项结果(非双向绑定)
  4. // 支持 省市区,省市,省
  5. <template>
  6. <view class="cu-form-group">
  7. <!-- <view class="title">{{title_}}</view> -->
  8. <picker
  9. mode="multiSelector"
  10. @change="MultiChange"
  11. @columnchange="MultiColumnChange"
  12. :value="multiIndex"
  13. :range="multiArray.slice(0,column_)"
  14. range-key="region_name"
  15. >
  16. <view class="uni-input">
  17. <text v-for="(item, index) in column_" :key="index" class="margin-right-sm">
  18. {{multiArrayClone[index][multiIndexClone[index]].region_name }}
  19. <text v-if="index!==column_-1"></text>
  20. </text>
  21. </view>
  22. </picker>
  23. </view>
  24. <!-- </view> -->
  25. </template>
  26. <script>
  27. export default {
  28. name: "c-region",
  29. props: {
  30. title_: {
  31. type: String,
  32. default: "省市区",
  33. },
  34. multiIndex_: {
  35. // eg: multiIndex_="['14','14002','14002002']",
  36. type: Array,
  37. default: function () {
  38. return [];
  39. },
  40. },
  41. //自定义第一个选项的名称
  42. custom_: {
  43. type: String,
  44. default: "",
  45. },
  46. column_: {
  47. type: Number,
  48. default: 3,
  49. },
  50. region_:{
  51. type: Array,
  52. default: function () {
  53. return [];
  54. },
  55. }
  56. },
  57. components: {},
  58. data() {
  59. return {
  60. multiArray: [],
  61. multiArrayClone: [],
  62. multiIndex: [0, 0, 0],
  63. multiIndexClone: [0, 0, 0],
  64. region: uni.getStorageSync("region") || {},
  65. };
  66. },
  67. onShow() {},
  68. mounted() {
  69. if (!uni.getStorageSync("region")) {
  70. uni.showToast({
  71. title: "初始化省市区失败",
  72. });
  73. }
  74. },
  75. onLoad() {},
  76. computed: {},
  77. watch: {
  78. custom_: {
  79. handler: function (nVal, oVal) {
  80. if (nVal) {
  81. this.region = this.setDefaultFirstOption();
  82. }
  83. },
  84. immediate: true,
  85. },
  86. multiIndex_: {
  87. handler: function (nVal, oVal) {
  88. if (!nVal.length) {
  89. const column1 = this.objToArr(this.region);
  90. const column2 = this.objToArr(column1[0].child);
  91. const column3 = this.objToArr(column2[0].child);
  92. this.multiArray = [column1, column2, column3];
  93. this.multiArrayClone = JSON.parse(JSON.stringify(this.multiArray));
  94. return;
  95. }
  96. const provinceId = nVal[0];
  97. const cityId = nVal[1];
  98. const areaId = nVal[2];
  99. const tempMultiArray = [];
  100. const column0 = this.objToArr(this.region);
  101. const index0 = column0.findIndex(
  102. (item) => item.region_id === provinceId
  103. );
  104. const column1 = this.objToArr(column0[index0].child);
  105. const index1 = column1.findIndex((item) => item.region_id === cityId);
  106. tempMultiArray.push(column0);
  107. this.multiIndex[0] = index0;
  108. tempMultiArray.push(column1);
  109. this.multiIndex[1] = index1;
  110. const column2 = this.objToArr(tempMultiArray[1][index1].child);
  111. const index2 = column2.findIndex((item) => item.region_id === areaId);
  112. tempMultiArray.push(column2);
  113. this.multiIndex[2] = index2;
  114. this.multiArray = tempMultiArray;
  115. this.multiArrayClone = JSON.parse(JSON.stringify(this.multiArray));
  116. this.multiIndexClone = JSON.parse(JSON.stringify(this.multiIndex));
  117. },
  118. immediate: true,
  119. },
  120. },
  121. methods: {
  122. objToArr(obj) {
  123. const arr = Object.values(obj);
  124. let defaultIndex;
  125. let list = arr.map((item, index) => {
  126. if (item.region_name === this.custom_) {
  127. defaultIndex = index;
  128. }
  129. return {
  130. region_id: item.region_id,
  131. region_name: item.region_name,
  132. child: item.child,
  133. };
  134. });
  135. if (defaultIndex !== undefined) {
  136. const temp = list[defaultIndex];
  137. list.splice(defaultIndex, 1);
  138. list.unshift(temp);
  139. }
  140. return list;
  141. },
  142. MultiChange(e) {
  143. this.multiIndexClone = e.detail.value;
  144. this.multiArrayClone = JSON.parse(JSON.stringify(this.multiArray));
  145. const arr = [];
  146. const arrs = [];
  147. for (let index = 0; index < this.column_; index++) {
  148. arr.push(
  149. this.multiArrayClone[index][this.multiIndexClone[index]].region_id
  150. );
  151. arrs.push(
  152. this.multiArrayClone[index][this.multiIndexClone[index]].region_name
  153. );
  154. }
  155. // uni.showToast({
  156. // title: "值:" + arr,
  157. // });
  158. this.$emit("region_", arrs);
  159. this.$emit("selecteRegion_", arr);
  160. // return arr;
  161. },
  162. MultiColumnChange(e) {
  163. let data = {
  164. multiArray: this.multiArray,
  165. multiIndex: this.multiIndex,
  166. };
  167. data.multiIndex[e.detail.column] = e.detail.value;
  168. const column = e.detail.column; // 移动某一列
  169. if (column === 0) {
  170. //移动第一列
  171. const row = data.multiIndex[0]; //移动第一列的某一行
  172. data.multiArray[1] = this.objToArr(data.multiArray[0][row].child);
  173. data.multiArray[2] = this.objToArr(data.multiArray[1][0].child);
  174. data.multiIndex[1] = 0;
  175. data.multiIndex[2] = 0;
  176. } else if (column === 1) {
  177. //移动第二列
  178. const row = data.multiIndex[1]; //移动第一列的某一行
  179. data.multiArray[2] = this.objToArr(data.multiArray[1][row].child);
  180. data.multiIndex[2] = 0;
  181. }
  182. this.multiArray = [...data.multiArray];
  183. this.multiIndex = [...data.multiIndex];
  184. },
  185. setDefaultFirstOption() {
  186. let region = uni.getStorageSync("region");
  187. region["00"] = {
  188. region_id: "00",
  189. region_name: this.custom_,
  190. child: {
  191. "00": {
  192. region_id: "00",
  193. region_name: this.custom_,
  194. child: {
  195. "00": {
  196. region_id: "00",
  197. region_name: this.custom_,
  198. },
  199. },
  200. },
  201. },
  202. };
  203. for (const provinceK in region) {
  204. if (region.hasOwnProperty(provinceK)) {
  205. const province = region[provinceK];
  206. if (province.child) {
  207. province.child["00"] = {
  208. region_id: "00",
  209. region_name: this.custom_,
  210. child: {
  211. "00": {
  212. region_id: "00",
  213. region_name: this.custom_,
  214. },
  215. },
  216. };
  217. }
  218. for (const cityK in province.child) {
  219. if (province.child.hasOwnProperty(cityK)) {
  220. const city = province.child[cityK];
  221. if (city.child) {
  222. city.child["00"] = {
  223. region_id: "00",
  224. region_name: this.custom_,
  225. };
  226. }
  227. }
  228. }
  229. }
  230. }
  231. return region;
  232. },
  233. },
  234. };
  235. </script>
  236. <style>
  237. .cu-form-group {
  238. width: 100%;
  239. }
  240. .title {
  241. text-align: center;
  242. }
  243. </style>