search
HomeWeb Front-enduni-appUse uniapp to implement the city selector function

Use uniapp to implement the city selector function

Using uniapp to implement the city selector function requires specific code examples

With the rapid development of the mobile Internet, people’s demand for convenient city selector functions is also increasing. Come higher and higher. This function can be easily implemented using uniapp. A specific code example is given below.

First, we need to prepare a city selector component. In uniapp, we can use the Picker selector component to implement the city selector function. Add the following code to the template:

<template>
  <view class="container">
    <picker mode="multiSelector" :value="defaultCity" @change="onChange">
      <view class="picker">
        {{city[0][defaultCity[0]]}}-{{city[1][defaultCity[1]]}}-{{city[2][defaultCity[2]]}}
      </view>
    </picker>
  </view>
</template>

In the script section, we need to define city data and the default selected city. You can add the following code to data:

<script>
export default {
  data() {
    return {
      defaultCity: [0, 0, 0], // 默认选中城市
      city: [['北京市', '上海市', '广州市', '深圳市'], ['朝阳区', '徐汇区', '天河区', '福田区'], ['三里屯', '淮海路', '天河城', '华强北']]
    }
  },
  methods: {
    onChange(e) {
      this.defaultCity = e.detail.value; // 更新选择的城市
      console.log('选择的城市:', this.city[0][this.defaultCity[0]], this.city[1][this.defaultCity[1]], this.city[2][this.defaultCity[2]]);
    }
  }
}
</script>

As you can see, we have defined a two-dimensional array city of city data, where each dimension represents a level of selection. For example, the first dimension represents provinces, the second dimension represents urban areas, and the third dimension represents specific streets. The city we select by default is defaultCity, and we use the onChange method to update the selected city and print it out.

Finally, beautify our city selector by adding styles. Add the following code in style:

<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.picker {
  font-size: 32rpx;
  color: #333;
  padding: 20rpx;
  background-color: #fff;
  border-radius: 10rpx;
}
</style>

The above code defines the style of the container and selector, making the city selector look more beautiful.

Through the above code example, we successfully implemented a simple city selector function using uniapp. Of course, city data can be modified according to actual needs, such as dynamically obtaining city data from the interface. Hopefully this example will help you implement more complex city selector functionality.

The above is the detailed content of Use uniapp to implement the city selector function. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you debug issues on different platforms (e.g., mobile, web)?How do you debug issues on different platforms (e.g., mobile, web)?Mar 27, 2025 pm 05:07 PM

The article discusses debugging strategies for mobile and web platforms, highlighting tools like Android Studio, Xcode, and Chrome DevTools, and techniques for consistent results across OS and performance optimization.

What debugging tools are available for UniApp development?What debugging tools are available for UniApp development?Mar 27, 2025 pm 05:05 PM

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

How do you perform end-to-end testing for UniApp applications?How do you perform end-to-end testing for UniApp applications?Mar 27, 2025 pm 05:04 PM

The article discusses end-to-end testing for UniApp applications across multiple platforms. It covers defining test scenarios, choosing tools like Appium and Cypress, setting up environments, writing and running tests, analyzing results, and integrat

What are the different types of testing that you can perform in a UniApp application?What are the different types of testing that you can perform in a UniApp application?Mar 27, 2025 pm 04:59 PM

The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

What are some common performance anti-patterns in UniApp?What are some common performance anti-patterns in UniApp?Mar 27, 2025 pm 04:58 PM

The article discusses common performance anti-patterns in UniApp development, such as excessive global data use and inefficient data binding, and offers strategies to identify and mitigate these issues for better app performance.

How can you use profiling tools to identify performance bottlenecks in UniApp?How can you use profiling tools to identify performance bottlenecks in UniApp?Mar 27, 2025 pm 04:57 PM

The article discusses using profiling tools to identify and resolve performance bottlenecks in UniApp, focusing on setup, data analysis, and optimization.

How can you optimize network requests in UniApp?How can you optimize network requests in UniApp?Mar 27, 2025 pm 04:52 PM

The article discusses strategies for optimizing network requests in UniApp, focusing on reducing latency, implementing caching, and using monitoring tools to enhance application performance.

How can you optimize images for web performance in UniApp?How can you optimize images for web performance in UniApp?Mar 27, 2025 pm 04:50 PM

The article discusses optimizing images in UniApp for better web performance through compression, responsive design, lazy loading, caching, and using WebP format.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment