// template/selectMore/selectMore.js Component({ /** * 组件的属性列表 */ properties: { img: String, title: String, list: Object, locationValue: Array, schoolValue: String, school: Object, verifiedList: Array }, /** * 组件的初始数据 */ data: { arr1: [], value1: '', valueSingle: 0, valueArr: [], show: false, name: '', select1: 'location', selectSchool: '', selectarr: null }, lifetimes: { attached: function () { this.attachedIn() }, }, attached: function () { this.attachedIn() }, observers: { "list": function (e) { this.attachedIn() }, "locationValue": function (e) { // console.log('locationValue:', e) if (Array.isArray(e) && e.length > 0) { if (e.length > 1) { // 地区多选 let selectarr = {} for (let i = 0; i < e.length; i++) { selectarr[e[i]] = true } this.setData({ value1: Math.floor(e[0]) + '', selectarr, name: this.properties.list[Math.floor(e[0])] + '/...', select1: 'location', }) } else { // 地区单选 let selectarr = {}; if (Math.floor(e[0]) == e[0]) { selectarr = null; } else { selectarr[e[0]] = true } this.setData({ selectarr, value1: Math.floor(e[0]) + '', select1: 'location', name: this.properties.list[Math.floor(e[0])] + '/...' }) } } else { // 地区清空 this.setData({ selectarr: null, value1: '', }) } }, "schoolValue": function (e) { // console.log('schoolValue:', e) if (!e) { return false } this.setData({ selectSchool: e, name: e ? '学校' : this.data.name, select1: 'school', }) }, }, /** * 组件的方法列表 */ methods: { catchmove() {}, attachedIn() { // console.log('attachedInattachedInattachedIn') this.setList() }, showAlert(e) { this.setData({ show: e.currentTarget.dataset.show == 1 ? true : false }) }, setList() { if (this.data.arr1.length > 0) { return false } for (let i in this.properties.list) { if (i == Math.floor(i)) { this.data.arr1.push({ name: this.properties.list[i], value: i }) } } this.setData({ arr1: this.data.arr1 }) }, // 选择一级选项 selectItem(e) { var value = e.currentTarget.dataset.value; // 清空二级选项数据 this.setData({ value1: value, selectarr: null }) }, // 选择二级选项 selectItem2(e) { let { value } = e.currentTarget.dataset; console.log(value); if (value == 0) { // 如果选的是‘不限’-‘不限’; this.data.selectarr = null } else { if (this.data.selectarr === null) { this.data.selectarr = {} } if (this.data.selectarr[value]) { delete this.data.selectarr[value]; } else { this.data.selectarr[value] = true; } } this.setData({ selectarr: this.data.selectarr }) if (value == "0") { this.submit() } }, submit() { let name = '' if (this.data.select1 == 'school') { name = this.data.selectSchool ? '学校' : ''; this.triggerEvent('submit', { school: this.data.selectSchool, location: [] }) } else if (this.data.select1 == 'location') { name = this.data.value1 > 0 ? this.properties.list[this.data.value1] + '/...' : ''; let arr = []; // 如果有选区域细分 if (this.data.selectarr) { for (let i in this.data.selectarr) { arr.push(i) } } else if(this.data.value1 != "" && this.data.value1 != 0) { arr = [this.data.value1] } this.triggerEvent('submit', { location: arr, school: '' }) } // console.log(name) this.setData({ show: false, name }) }, // 选择区域,学校 selectArea(e) { let value = e.currentTarget.dataset.value; if (this.data.select1 == value) { return false } if (value == 'school') { this.setData({ select1: value, selectSchool: '', }) } else { this.setData({ select1: value, selectarr: null }) } }, selectItemSchool(e) { this.setData({ selectSchool: e.currentTarget.dataset.value }) console.log("e", e); this.submit() } } })