vv-select-area.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <!-- @format -->
  2. <template>
  3. <!-- v-model -->
  4. <!-- 使用组件 -->
  5. <!-- <vvSelect placeholder="请选择所属类别" :list="categorys" label="name" valueKey="id" @change="pickerChange"></vvSelect> -->
  6. <picker
  7. mode="multiSelector"
  8. @change="bindMultiPickerChange"
  9. @columnchange="bindMultiPickerColumnChange"
  10. range-key="name"
  11. :value="multiIndex"
  12. :disabled="disabled"
  13. :range="multiArray"
  14. >
  15. <view
  16. class="vv-input__body text-right"
  17. style="
  18. border: 1px solid #ccc;
  19. border-radius: 4px;
  20. padding-left: 10px;
  21. height: 38px;
  22. line-height: 38px;
  23. font-size: 28rpx;
  24. "
  25. >
  26. <view class="vv-input__control" style="text-align: left">
  27. <text v-if="checkLabels" :class="{ disabled: disabled }">{{
  28. checkLabels
  29. }}</text>
  30. <text class="vv-input__placeholder" v-else>{{ placeholder }}</text>
  31. </view>
  32. <!-- <uni-icons type="arrowright" size="14" color="#808080"></uni-icons> -->
  33. </view>
  34. </picker>
  35. </template>
  36. <script>
  37. /**
  38. * vv-select 选择
  39. * @description picker再封装
  40. * @property {String} valueKey 绑定的key
  41. * @property {String} label 绑定的label
  42. * @property {Array} list 数据
  43. * @property {Array} level 层级 默认3级 即:省-市-区
  44. * @property {Boolean} disabled 是否禁用
  45. * @event {Function} change
  46. */
  47. export default {
  48. name: "vv-select-area",
  49. props: {
  50. value: {
  51. type: [Array],
  52. },
  53. placeholder: {
  54. type: String,
  55. default: "请选择",
  56. },
  57. list: {
  58. type: [Array],
  59. },
  60. level: {
  61. type: Number,
  62. default: 3,
  63. },
  64. disabled: {
  65. type: Boolean,
  66. default: false,
  67. },
  68. },
  69. data() {
  70. return {
  71. city: [],
  72. multiArray: [],
  73. multiIndex: [],
  74. checkLabels: "",
  75. checkValues: [],
  76. };
  77. },
  78. watch: {
  79. value: {
  80. handler: function (newVal) {
  81. if (this.list.length > 0 && this.value.length > 0) {
  82. this.init();
  83. }
  84. },
  85. immediate: true,
  86. },
  87. list: {
  88. handler: function (newVal) {
  89. if (this.list.length > 0) {
  90. this.init();
  91. }
  92. },
  93. immediate: true,
  94. },
  95. },
  96. created() {},
  97. methods: {
  98. init: function () {
  99. this.city = [...this.list];
  100. this.multiArray[0] = this.city;
  101. this.setMultiData(0, 0); // 默认值
  102. if (this.value && this.value.length > 0) {
  103. // 有初始值
  104. this.setCheck(this.value);
  105. }
  106. },
  107. setCheck: function (codes) {
  108. if (codes.length == 0) return false;
  109. let indexs = [];
  110. let _labes = [];
  111. for (let i = 0, l = codes.length; i < l; i++) {
  112. let index = this.multiArray[i].findIndex((item) => {
  113. return item.id == codes[i];
  114. });
  115. indexs.push(index);
  116. let item = this.multiArray[i].find((item) => {
  117. return item.id == codes[i];
  118. });
  119. _labes.push(item.name);
  120. this.setMultiData(i, index);
  121. }
  122. this.multiIndex = indexs;
  123. this.checkValues = codes;
  124. this.checkLabels = _labes.join("-");
  125. },
  126. setMultiData: function (column, index) {
  127. var data = {
  128. multiArray: this.multiArray,
  129. multiIndex: this.multiIndex,
  130. };
  131. data.multiIndex[column] = index; //
  132. switch (column) {
  133. case 0: //拖动第1列
  134. if (this.city[index].children) {
  135. data.multiArray[1] = this.city[index].children;
  136. if (data.multiArray[1][0] && data.multiArray[1][0].children) {
  137. data.multiArray[2] = data.multiArray[1][0].children;
  138. if (this.level > 3) {
  139. if (data.multiArray[2][0] && data.multiArray[2][0].children) {
  140. data.multiArray[3] = data.multiArray[2][0].children;
  141. } else {
  142. data.multiArray[3] = [];
  143. }
  144. }
  145. } else {
  146. data.multiArray[2] = [];
  147. if (this.level > 3) {
  148. data.multiArray[3] = [];
  149. }
  150. }
  151. } else {
  152. data.multiArray[1] = [];
  153. data.multiArray[2] = [];
  154. if (this.level > 3) {
  155. data.multiArray[3] = [];
  156. }
  157. }
  158. this.multiIndex.splice(1, 1, 0);
  159. this.multiIndex.splice(2, 1, 0);
  160. if (this.level > 3) {
  161. this.multiIndex.splice(3, 1, 0);
  162. }
  163. break;
  164. case 1: //拖动第2列
  165. if (data.multiArray[1][index] && data.multiArray[1][index].children) {
  166. data.multiArray[2] = data.multiArray[1][index].children;
  167. if (this.level > 3) {
  168. if (data.multiArray[2][0] && data.multiArray[2][0].children) {
  169. data.multiArray[3] = data.multiArray[2][0].children;
  170. } else {
  171. data.multiArray[3] = [];
  172. }
  173. }
  174. } else {
  175. data.multiArray[2] = [];
  176. if (this.level > 3) {
  177. data.multiArray[3] = [];
  178. }
  179. }
  180. this.multiIndex.splice(2, 1, 0);
  181. if (this.level > 3) {
  182. this.multiIndex.splice(3, 1, 0);
  183. }
  184. break;
  185. case 2: //拖动第3列
  186. if (this.level < 4) {
  187. break;
  188. }
  189. if (data.multiArray[2][index] && data.multiArray[2][index].children) {
  190. data.multiArray[3] = data.multiArray[2][index].children;
  191. } else {
  192. data.multiArray[3] = [];
  193. }
  194. this.multiIndex.splice(3, 1, 0);
  195. break;
  196. }
  197. this.multiArray = data.multiArray;
  198. this.multiIndex = data.multiIndex;
  199. this.$forceUpdate();
  200. },
  201. bindMultiPickerColumnChange: function (e) {
  202. this.setMultiData(e.detail.column, e.detail.value);
  203. },
  204. bindMultiPickerChange: function (e) {
  205. let _labes = [];
  206. let _values = [];
  207. for (let i = 0, l = this.multiArray.length; i < l; i++) {
  208. let item = this.multiArray[i];
  209. let index = this.multiIndex[i];
  210. if (item.length === 0) {
  211. break;
  212. }
  213. _labes.push(item[index].name);
  214. _values.push(item[index].id);
  215. }
  216. console.log(_labes, _values);
  217. // console.log(
  218. // this.multiArray[0][e.detail.value[0]],
  219. // this.multiArray[1][e.detail.value[1]]
  220. // ); // {value: "431000", label: "郴州市", level: 2}
  221. // this.multiIndex = e.detail.value;
  222. // this.$apply();
  223. this.checkLabels = _labes.join("-");
  224. this.checkValues = _values;
  225. // 返回这个值到父级
  226. this.$emit("change", _values);
  227. this.$emit("input", _values);
  228. this.$emit("on-change", _values);
  229. },
  230. },
  231. };
  232. </script>
  233. <style lang="scss" scoped></style>