tm-pickersCity.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <template>
  2. <view class="tm-pickersCity d-inline-block fulled">
  3. <view @click.stop.prevent="openPoup"><slot></slot></view>
  4. <tm-poup @change="toogle" ref="pop" v-model="showpop" :height="750" :bg-color="black_tmeme?'grey-darken-5':bgColor">
  5. <view class="tm-pickersCity-title pa-32 pb-16">
  6. <view class="text-size-g text-align-center" style="min-height: 48rpx;">{{title}}</view>
  7. <view class="tm-pickersCity-close rounded flex-center " :class="black_tmeme?'grey-darken-3':'grey-lighten-3'">
  8. <tm-icons @click="close" name="icon-times" size="24" :color="black_tmeme?'white':'grey'"></tm-icons>
  9. </view>
  10. </view>
  11. <tm-pickersView v-if="showpop" @aniStart="aniisTrue=false" @aniEnd="aniisTrue=true" :default-value="dataValue" :bg-color="bgColor" :black="black_tmeme" :disabled="disabled" ref="tmPicKersTest" :list="list" ></tm-pickersView>
  12. <view class="pa-32">
  13. <tm-button @click="confirm" block itemeClass="round-24" :black="black_tmeme" :theme="btnColor" fontSize="32">{{btnText}}</tm-button>
  14. </view>
  15. </tm-poup>
  16. </view>
  17. </template>
  18. <script>
  19. import provinceData from '@/tm-vuetify/tool/util/province.js';
  20. import cityData from '@/tm-vuetify/tool/util/city.js';
  21. import areaData from '@/tm-vuetify/tool/util/area.js';
  22. /**
  23. * 地区选择器(弹层试)
  24. * @description 地区选择器(弹层试)
  25. * @property {String} title = [] 弹层层标题
  26. * @property {String} btn-text = [] 底部按钮确认的文字
  27. * @property {String} btn-color = [primary|green|orange|red|blue|bg-gradient-blue-lighten] 默认:bg-gradient-blue-lighten底部按钮确认的背景颜色仅支持主题色名称
  28. * @property {String} bg-color = [white|blue] 默认:white,白色背景;请填写背景的主题色名称。
  29. * @property {Function} confirm = [] 返回当前选中的数据
  30. * @property {String} level = [province|city|area] ,默认area,显示的级别province:仅显示省,city仅显示省市,area:显示省市区。
  31. * @property {Array} default-value = [] 同tm-pckerView格式,可以是数组内:序列,对象,字符串赋值。
  32. * @property {String|Boolean} black = [true|false] 是否开启暗黑模式。
  33. * @property {String|Boolean} disabled = [true|false] 是否禁用
  34. * @example <tm-pickersCityView ref="city" :defaultValue='["上海市", "市辖区", "徐汇区"]'></tm-pickersCityView>
  35. *
  36. *
  37. */
  38. import tmButton from "@/tm-vuetify/components/tm-button/tm-button.vue"
  39. import tmPoup from "@/tm-vuetify/components/tm-poup/tm-poup.vue"
  40. import tmIcons from "@/tm-vuetify/components/tm-icons/tm-icons.vue"
  41. import tmPickersView from "@/tm-vuetify/components/tm-pickersView/tm-pickersView.vue"
  42. export default {
  43. components:{tmButton,tmPoup,tmIcons,tmPickersView},
  44. name:"tm-pickersCity",
  45. model:{
  46. prop:'value',
  47. event:'input'
  48. },
  49. props: {
  50. defaultValue:{
  51. type:Array,
  52. default:()=>{return []}
  53. },
  54. // 显示的级别。province,city,area。
  55. level:{
  56. type:String,
  57. default:'area'
  58. },
  59. black:{
  60. type:String|Boolean,
  61. default:null
  62. },
  63. // 是否禁用
  64. disabled:{
  65. type:String|Boolean,
  66. default:false
  67. },
  68. // 等同v-model,或者value.sync
  69. value: {
  70. type: String | Number,
  71. default: false
  72. },
  73. // 背景颜色,主题色名称。
  74. bgColor:{
  75. type:String,
  76. default:'white'
  77. },
  78. show:{
  79. type:Boolean,
  80. default:true
  81. },
  82. // 顶部标题。
  83. title:{
  84. type:String,
  85. default:'请选择地址'
  86. },
  87. // 底部按钮文件
  88. btnText:{
  89. type:String,
  90. default:'确认'
  91. },
  92. // 底部按钮背景主题色名称
  93. btnColor:{
  94. type:String,
  95. default:'primary'
  96. },
  97. },
  98. data() {
  99. return {
  100. showpop:false,
  101. dataValue:[],
  102. list:[],
  103. aniisTrue:true,
  104. };
  105. },
  106. computed: {
  107. black_tmeme: function() {
  108. if (this.black !== null) return this.black;
  109. return this.$tm.vx.state().tmVuetify.black;
  110. }
  111. },
  112. mounted() {
  113. this.showpop = this.value;
  114. this.$nextTick(function(){
  115. this.chili_level();
  116. uni.hideKeyboard();
  117. })
  118. },
  119. watch:{
  120. value:function(val){
  121. this.showpop = val;
  122. }
  123. },
  124. methods: {
  125. // 获取选中的资料。
  126. getSelectedValue(){
  127. let d = this.$refs.tmPicKersTest.getSelectedValue();
  128. let p = [];
  129. if(this.level=='province'){
  130. p = [d[0].data.text]
  131. }else if(this.level=='city'){
  132. p = [d[0].data.text,d[1].data.text]
  133. }else{
  134. p = [d[0].data.text,d[1].data.text,d[2].data.text]
  135. }
  136. //返回对象数组
  137. if(typeof this.defaultValue[0] === 'object'){
  138. this.$emit("update:defaultValue",[d[0].data,d[1].data,d[2].data])
  139. //返回索引数组
  140. }else if(typeof this.defaultValue[0] === 'number'){
  141. this.$emit("update:defaultValue",[d[0].index,d[1].index,d[2].index])
  142. //返回字符串数组
  143. }else{
  144. this.$emit("update:defaultValue",p)
  145. }
  146. return p;
  147. },
  148. chili_level(){
  149. if(this.level=='province'){
  150. this.chiliFormatCity_pro();
  151. }else if(this.level=='city'){
  152. this.chiliFormatCity_city();
  153. }else{
  154. this.chiliFormatCity_area();
  155. }
  156. },
  157. chiliFormatCity_area() {
  158. let list = [];
  159. provinceData.forEach((item,index)=>{
  160. list.push({
  161. id:item.value,
  162. text:item.label,
  163. children:[]
  164. })
  165. })
  166. cityData.forEach((item,index)=>{
  167. item.forEach((citem,cindex)=>{
  168. list[index].children.push({
  169. id:citem.value,
  170. text:citem.label,
  171. children:[]
  172. })
  173. })
  174. })
  175. list.forEach((item,index)=>{
  176. item.children.forEach((citem,cindex)=>{
  177. areaData[index][cindex].forEach(jitem=>{
  178. list[index].children[cindex].children.push({
  179. id:jitem.value,
  180. text:jitem.label
  181. })
  182. })
  183. })
  184. })
  185. this.list = list;
  186. },
  187. chiliFormatCity_pro() {
  188. let list = [];
  189. provinceData.forEach((item,index)=>{
  190. list.push({
  191. id:item.value,
  192. text:item.label
  193. })
  194. })
  195. this.list = list;
  196. },
  197. chiliFormatCity_city() {
  198. let list = [];
  199. provinceData.forEach((item,index)=>{
  200. list.push({
  201. id:item.value,
  202. text:item.label,
  203. children:[]
  204. })
  205. })
  206. cityData.forEach((item,index)=>{
  207. item.forEach((citem,cindex)=>{
  208. list[index].children.push({
  209. id:citem.value,
  210. text:citem.label
  211. })
  212. })
  213. })
  214. this.list = list;
  215. },
  216. confirm() {
  217. if(!this.aniisTrue){
  218. console.log('no');
  219. return ;
  220. }
  221. this.$emit('confirm',this.getSelectedValue())
  222. this.$refs.pop.close();
  223. },
  224. close(){
  225. this.$refs.pop.close();
  226. },
  227. openPoup(){
  228. if(this.disabled==true) return;
  229. this.showpop=!this.showpop
  230. },
  231. toogle(e){
  232. if(e){
  233. this.$nextTick( function(){
  234. if(this.dataValue != this.defaultValue){
  235. this.dataValue = this.defaultValue;
  236. this.$refs.tmPicKersTest.setDefaultValue(this.dataValue)
  237. }else{
  238. if(!this.dataValue || this.dataValue?.length==0){
  239. this.$refs.tmPicKersTest.setDefaultValue([0,0,0])
  240. }else{
  241. this.$refs.tmPicKersTest.setDefaultValue(this.dataValue)
  242. }
  243. }
  244. this.$emit('input',e);
  245. this.$emit('update:value',e);
  246. })
  247. }
  248. }
  249. },
  250. }
  251. </script>
  252. <style lang="scss" scoped>
  253. .tm-pickersCity-title {
  254. position: relative;
  255. .tm-pickersCity-close {
  256. position: absolute;
  257. top: 32upx;
  258. right: 32upx;
  259. width: 50upx;
  260. height: 50upx;
  261. }
  262. }
  263. </style>