ionic+AnjularJs实现省市县三级联动效果
- 建议对ionic和AnjularJs有一定了解的人可以用到,很多时候我们要用到选择省份、城市、区县的功能,现在就跟着我来实现这个功能吧,用很少的代码(我这里是根据客户的要求,只显示想要显示的部分省份和其相对应的城市、区县,并且这些数据将通过后台放入数据库,并没有引用完整的城市js)
1.首先在所需要的HTML页面需要这些代码,其中的样式都是自己定义的,不喜欢的可以按照自己喜好来写:
- <div class="list">
- <div class="list" style="border: 0;">
- <label class="item item-input item-select" style="border: 0">
- <div class="input-label">
- <span><span style="color: #6e6e6e"> </span></span>
- </div>
- <select style="font-size: 16px" ng-model="data.currentProvinceId"
- ng-options="pp.Id as pp.RegionName for pp in allProvinces"
- ng-change="switchProvince(data.currentProvinceId)">
- </select>
- </label>
- </div>
- <div class="list" style="border: 0">
- <label class="item item-input item-select" style="border: 0">
- <div class="input-label">
- <span><span style="color: #6e6e6e"> </span></span>
- </div>
- <select style="font-size: 16px" ng-options="cc.Id as cc.RegionName for cc in cities"
- ng-model="data.currentCityId" ng-change="switchCity(data.currentCityId)">
- </select>
- </label>
- </div>
- <div class="list" style="border: 0">
- <label class="item item-input item-select" style="border: 0">
- <div class="input-label">
- <span style="color: #6e6e6e"> </span>
- </div>
- <select style="font-size: 16px" ng-options="aa.Id as aa.RegionName for aa in areas"
- ng-model="data.currentAreaId" ng-change="switchArea(data.currentAreaId)">
- </select>
- </label>
- </div>
- </div>
2.相应的控制器controller.js里:
- .controller('selectCityCtrl', function ($rootScope, $scope, $state, $filter, $ionicHistory, selectCitySvc, storageSvc, scollImageSvc, classIficationItemSvc) {
- $scope.currentCity = selectCitySvc.getCurrentCity();
- $scope.allRegions = selectCitySvc.getCacheAllAreas();
- $scope.allProvinces = [
- {Id: 0, RegionName: '请选择省份'}
- ];
- $scope.cities = [
- {Id: 0, RegionName: '请选择城市'}
- ];
- $scope.areas = [
- {Id: 0, RegionName: '请选择区/县'}
- ];
- $scope.data = {
- selectName: "",
- currentProvinceId: 0,
- currentCityId: 0,
- currentAreaId: 0
- };
- function getSelectedRegionId() {
- var regionId = $scope.data.currentAreaId;
- if (regionId == 0) {
- regionId = $scope.data.currentCityId;
- }
- if (regionId == 0) {
- regionId = $scope.data.currentProvinceId;
- }
- return regionId;
- }
- function updateSelectRegionName() {
- var currentRegion = $filter('filter')($scope.allRegions, {Id: getSelectedRegionId()}, true)[0];
- if (currentRegion) {
- $scope.data.selectName = currentRegion.RegionName
- } else {
- $scope.data.selectName = '';
- }
- }
- $scope.switchProvince = function (currentProvinceId) {
- $scope.data.currentCityId = 0;
- $scope.data.currentAreaId = 0;
- $scope.cities.splice(1);
- $scope.areas.splice(1);
- var cities = $filter('filter')($scope.allRegions, {RegionType: 1, ParentId: currentProvinceId});
- for (var i in cities) {
- $scope.cities.push(cities[i]);
- }
- updateSelectRegionName();
- };
- $scope.switchCity = function (currentCityId) {
- $scope.data.currentAreaId = 0;
- $scope.areas.splice(1);
- var areas = $filter('filter')($scope.allRegions, {RegionType: 2, ParentId: currentCityId});
- for (var i in areas) {
- $scope.areas.push(areas[i]);
- }
- updateSelectRegionName();
- };
- //增加当切换县区的时候更换服务区名
- $scope.switchArea = function (currentAreaId) {
- updateSelectRegionName();
- };
- var allProvinces = $filter('filter')($scope.allRegions, {RegionType: 0});
- for (var i in allProvinces) {
- $scope.allProvinces.push(allProvinces[i]);
- }
- if ($scope.currentCity.RegionType == 0) {
- // 如果上次选择省份
- $scope.data.currentProvinceId = $scope.currentCity.Id;
- $scope.switchProvince($scope.currentCity.Id);
- } else if ($scope.currentCity.RegionType == 1) {
- var province = $filter('filter')($scope.allRegions, {Id: $scope.currentCity.ParentId}, true)[0];
- $scope.data.currentProvinceId = province.Id;
- //省份
- $scope.switchProvince(province.Id);
- $scope.data.currentCityId = $scope.currentCity.Id;
- $scope.switchCity($scope.currentCity.Id);
- } else if ($scope.currentCity.RegionType == 2) {
- // 如果上次选择县区
- var city = $filter('filter')($scope.allRegions, {Id: $scope.currentCity.ParentId}, true)[0];
- var province = $filter('filter')($scope.allRegions, {Id: city.ParentId}, true)[0];
- $scope.data.currentProvinceId = province.Id;
- $scope.switchProvince(province.Id);
- $scope.data.currentCityId = city.Id;
- $scope.switchCity(city.Id);
- $scope.data.currentAreaId = $scope.currentCity.Id;
- }
- $scope.user = {
- province: "" || storageSvc.load('province.RegionName'),
- city: "" || storageSvc.load('city.RegionName'),
- area: "" || storageSvc.load('area.RegionName'),
- currentCity: "" || storageSvc.load('selectCitySvc.getCurrentCity()')
- };
- });
3.效果如图:
ionic+AnjularJs实现省市县三级联动效果的更多相关文章
- jQuery - 全国省市县三级联动
最近有空用jquery做了一个全国省市县的三级联动,在以后或许可以用的到 ,遗憾的是我还没用封装,等有空看能不能封装成一个插件 废话不多说,贴上代码: <!doctype html> &l ...
- wex5 实战 省市县三级联动与地址薄同步
无论是商城,还是快递,都要用到省市县三级联动,和地址薄,今天就以实战来制作,难点有3个: 1:三级联动,有wex5组件实现,相对简单,实战里对行数据进行了拼接 2: 地址薄选项,利用inputSel ...
- php仿经典省市县三级联动
之前有个需求要写个类似省市县三级联动的页面,于是,网上找了点资料看了下,其实原理很简单: 当我们选择一级栏目中某条记录的时候,会获取该栏目的vaule值,并发起ajax请求,后台根据这个vaule值, ...
- 项目总结01:JSP mysql SpringMvc下中国省市县三级联动下拉框
JSP mysql SpringMvc下中国省市县三级联动下拉框 关键词 JSP mysql数据库 SpringMvc ajax Controller层 Service层 中国地区 省 ...
- 省市县三级联动 sql语句
发现在网上的省市县三级联动大部分是mysql的.就算是sqlserver的,也不准确.于是就把mysql的给改了下,适用sqlserver.sql语句如下: CREATE TABLE Dic_Area ...
- Android 省市县 三级联动(android-wheel的使用)[转]
转载:http://blog.csdn.net/lmj623565791/article/details/23382805 今天没事跟群里面侃大山,有个哥们说道Android Wheel这个控件,以为 ...
- Android 省市县 三级联动(android-wheel的使用)
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/23382805 今天没事跟群里面侃大山,有个哥们说道Android Wheel这个 ...
- PyQt5--基础篇:用eric6工具实现三级联动效果
今天给大家介绍下python gui界面的三级联动效果,我们用工具eric6来实现,先看下效果图. 首先我们先创建项目linkage,再新建窗体进入到Qt设计师工具开始设计界面,完成后保存并退出. 在 ...
- js之省市区(县)三级联动效果
省市区(县)三级联动效果,是我们软件开发比较常用的,特别是对一些crm,erp之类,当然也包括其他的后台管理系统,基本都涉及到,今天贴出这个常用的,方便个人复用和大家使用 <!DOCTYPE h ...
随机推荐
- 解决 jQuery UI datepicker z-index默认为1 的问题
最近碰到页面日期选择控件被页头挡住的问题,我们这个客户的电脑是宽屏的,上下窄,屏幕又小,导致他点击日期选择控件时,无法选择到月份.如图: 分析造成这个问题的原因: 我们页头部分的z-index设置为1 ...
- 如何用jQuery实现div随鼠标移动而移动(详解)?----2017-05-12
重点是弄清楚如何获取鼠标现位置与移动后位置,div现在位置与移动后位置: 用jQuery实现div随鼠标移动而移动,不是鼠标自身的位置!!而是div相对于之前位置的移动 代码如下:(注意看绿色部分的解 ...
- 服务器数据库挂掉 Can 't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock '(2) ";
刚刚遇到这个报错,我无语了,我这么个菜逼,咋解决,还好师兄(付付)解决了,付付真棒,在此记录一下,以供学习 Can 't connect to local MySQL server through s ...
- Nginx http和https 共存
nginx https 配置,添加证书启动https server { listen default_server; listen ; server_name _; ssl on; ssl_certi ...
- How to change current process to background process
Situation: there is a script or command is running, but we need to close current box/windows to do o ...
- Linux C 程序的开发环境
1.开发环境的构成 编辑器 vim,vi 编译器 gcc 调试器 gdb 函数库glibc 系统头文件glibc_header 2.gcc编译器 功能强大.性能优越的多平台编译器,gcc可以将c.c+ ...
- Ehcache 整合Spring 使用页面、对象缓存(1)
转自:http://www.cnblogs.com/hoojo/archive/2012/07/12/2587556.html Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以 ...
- Lua学习(3)——控制结构
Lua提供了一组传统的,小巧的控制结构,包括用于条件执行的if,用于迭代的while.repeat和for.所有的控制结构都有一个现实的终止符号:if for while 都以end结尾,repeat ...
- Angular Route导航
我们用Angular cli创建带有路由的新项目 ng new router --routing Angular Routes API文档 Angular的文档中有详细的解释: 1)https://a ...
- 【java8】慎用java8的foreach循环
虽然java8出来很久了,但是之前用的一直也不多,最近正好学习了java8,推荐一本书还是不错的<写给大忙人看的javase8>.因为学习了Java8,所以只要能用到的地方都会去用,尤其是 ...