123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- import address from '../data/address';
- export default {
- data(){
- return {
- data:{
- province:[],
- city:[],
- area:[],
- },
- province:[],
- city:[],
- area:[],
- vValue:[]
- }
- },
- watch:{
- value:{
- handler:function (value) {
- return this.setValue(value);
- },
- immediate:true
- }
- },
- methods:{
- open:function(){
- if (this.storeValueTarget && this.storeValueTarget.join('') !== this.vValue.join('')) {
- this.vValue = this.storeValueTarget;
- this.changeLevel('0',0);
- }
- },
- /* 点击确认触发 */
- confirm:function(){
- let code = '0';
- let cCode = [];
- let cAddress = [];
- this.vValue.map((item,index)=>{
- code = this.getCode(index,code);
- if (address[code]&&address[code][item]) {
- cAddress.push(address[code][item].name);
- code = address[code][item].code;
- cCode.push(code);
- }
- });
- this.storeValue = cAddress.join('');
- this.storeValueTarget = [...this.vValue];
- if (this.$attrs.input) {
- this.$emit('input',cAddress);
- }
- if (this.$attrs.onChange) {
- this.$emit('change',{
- postcode: code,
- code: cCode,
- value: cAddress
- });
- }
- },
- /* 更改展示数据 */
- changeLevel:function(code = '0',level=0){
- let parentCode = code;
- let nowCode = '';
- for (let i=level;i<this.use.length;i++) {
- nowCode = this.getCode(i,parentCode);
- if (address[nowCode]) {
- this.data[this.use[i]] = address[nowCode] || [];
- if (address[nowCode][this.vValue[i] || 0]){
- parentCode = address[nowCode][this.vValue[i] || 0].code || '';
- }else {
- parentCode = '';
- }
- } else {
- this.data[this.use[i]] = [];
- }
- }
- },
- /* 获取默认选择的地址 */
- getAddressValue:function (value) {
- let newValue = [];
- let key = 0;
- let parentItem = address;
- for(let i=0;i<this.use.length;i++) {
- if (value[i]) {
- key = i===0?this.use[i]:parentItem.code+':'+this.use[i];
- newValue[i] = address[key].indexOf(value[i]);
- if (newValue[i] < 0) newValue[i] = 0;
- } else {
- newValue[i] = 0;
- }
- if (i === 0) {
- parentItem = address['0:0'][newValue[i]];
- } else {
- parentItem = address[this.getCode(i,parentItem.code)][newValue[i]];
- }
- }
- return newValue;
- },
- changeColumn:function (key,index,value) {
- let newValue = [...this.vValue];
- newValue[index] = value;
- let item ={
- code:'0'
- };
- let change = false;
- let storageI = 0;
- let oldValue = [...this.vValue];
- for (let i=0;i<this.use.length;i++) {
- if (change) {
- newValue[i] = 0;
- }else {
- item = this.getItem(this.getCode(i,item.code),newValue[i]);
- }
- if (oldValue[i] !== newValue[i]) {
- if (!change) {
- storageI = i;
- }
- change = true;
- }
- }
- if (change) {
- this.vValue = newValue;
- this.changeLevel(item.code,storageI+1);
- this.confirm();
- }
- },
- getCode:function(level,code){
- return level+':'+code;
- },
- getItem:function (code='0:0',index) {
- if (address[code]) {
- return address[code][index];
- } else {
- return {
- code:-1
- };
- }
- },
- setValue:function (value) {
- let storeValue = value.join('');
- if (this.storeValue !== storeValue) {
- this.vValue = this.getAddressValue(value);
- this.storeValue = storeValue;
- this.changeLevel('0',0);
- this.confirm();
- }
- }
- },
- created(){
- this.open();
- console.log(this);
- }
- }
|