tm-pickersCityView.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <view class="tm-pickersCityView">
  3. <tm-pickersView :bg-color="bgColor" :black="black_tmeme" :disabled="disabled" ref="cityApp" :default-value="defaultValue" :list="list" ></tm-pickersView>
  4. </view>
  5. </template>
  6. <script>
  7. import provinceData from '@/tm-vuetify/tool/util/province.js';
  8. import cityData from '@/tm-vuetify/tool/util/city.js';
  9. import areaData from '@/tm-vuetify/tool/util/area.js';
  10. /**
  11. * 地区选择器(内嵌式)
  12. * @description 地区选择器(内嵌式),使用$refs 方式调用getSelectedValue取得选中的数据。
  13. * @property {String} level = [province|city|area] ,默认area,显示的级别province:仅显示省,city仅显示省市,area:显示省市区。
  14. * @property {Array} default-value = [] 同tm-pckerView格式,可以是数组内:序列,对象,字符串赋值。
  15. * @property {String|Boolean} black = [true|false] 是否开启暗黑模式。
  16. * @property {String|Boolean} disabled = [true|false] 是否禁用
  17. * @property {String} bg-color = [white|blue] 默认:white,白色背景;请填写背景的主题色名称。
  18. * @example <tm-pickersCityView ref="city" :defaultValue='["上海市", "市辖区", "徐汇区"]'></tm-pickersCityView>
  19. *
  20. */
  21. import tmPickersView from "@/tm-vuetify/components/tm-pickersView/tm-pickersView.vue"
  22. export default {
  23. components:{tmPickersView},
  24. name:'tm-pickersCityView',
  25. props:{
  26. defaultValue:{
  27. type:Array,
  28. default:()=>{return []}
  29. },
  30. // 显示的级别。province,city,area。
  31. level:{
  32. type:String,
  33. default:'area'
  34. },
  35. black:{
  36. type:String|Boolean,
  37. default:null
  38. },
  39. // 是否禁用
  40. disabled:{
  41. type:String|Boolean,
  42. default:false
  43. },
  44. // 背景颜色,主题色名称。
  45. bgColor:{
  46. type:String,
  47. default:'white'
  48. },
  49. },
  50. data() {
  51. return {
  52. list:[],
  53. };
  54. },
  55. computed: {
  56. black_tmeme: function() {
  57. if (this.black !== null) return this.black;
  58. return this.$tm.vx.state().tmVuetify.black;
  59. }
  60. },
  61. mounted() {
  62. this.$nextTick(function(){
  63. this.chili_level();
  64. })
  65. },
  66. methods: {
  67. // 获取选中的资料。
  68. getSelectedValue(){
  69. let d = this.$refs.cityApp.getSelectedValue();
  70. let p = [];
  71. if(this.level=='province'){
  72. p = [d[0].data.text]
  73. }else if(this.level=='city'){
  74. p = [d[0].data.text,d[1].data.text]
  75. }else{
  76. p = [d[0].data.text,d[1].data.text,d[2].data.text]
  77. }
  78. return p;
  79. },
  80. chili_level(){
  81. if(this.level=='province'){
  82. this.chiliFormatCity_pro();
  83. }else if(this.level=='city'){
  84. this.chiliFormatCity_city();
  85. }else{
  86. this.chiliFormatCity_area();
  87. }
  88. },
  89. chiliFormatCity_area() {
  90. let list = [];
  91. provinceData.forEach((item,index)=>{
  92. list.push({
  93. id:item.value,
  94. text:item.label,
  95. children:[]
  96. })
  97. })
  98. cityData.forEach((item,index)=>{
  99. item.forEach((citem,cindex)=>{
  100. list[index].children.push({
  101. id:citem.value,
  102. text:citem.label,
  103. children:[]
  104. })
  105. })
  106. })
  107. list.forEach((item,index)=>{
  108. item.children.forEach((citem,cindex)=>{
  109. areaData[index][cindex].forEach(jitem=>{
  110. list[index].children[cindex].children.push({
  111. id:jitem.value,
  112. text:jitem.label
  113. })
  114. })
  115. })
  116. })
  117. this.list = list;
  118. },
  119. chiliFormatCity_pro() {
  120. let list = [];
  121. provinceData.forEach((item,index)=>{
  122. list.push({
  123. id:item.value,
  124. text:item.label
  125. })
  126. })
  127. this.list = list;
  128. },
  129. chiliFormatCity_city() {
  130. let list = [];
  131. provinceData.forEach((item,index)=>{
  132. list.push({
  133. id:item.value,
  134. text:item.label,
  135. children:[]
  136. })
  137. })
  138. cityData.forEach((item,index)=>{
  139. item.forEach((citem,cindex)=>{
  140. list[index].children.push({
  141. id:citem.value,
  142. text:citem.label
  143. })
  144. })
  145. })
  146. this.list = list;
  147. }
  148. },
  149. }
  150. </script>
  151. <style lang="scss">
  152. </style>