dropdown.js 5.92 KB
// components/dropdown/dropdown.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Object,
      observer(newVal, oldVal) {
        if (newVal) {
          this.setData({
            width: `${100 / newVal.length}%`
          })
        }
      }
    },
    hideDropdown:{
      type:Boolean,
      observer(newVal, oldVal) {
        this.setData({
          parentCount: null
        })
      }
    },
    leave:{
      type: Boolean,
      observer(newVal, oldVal) {
        this.setData({
          parentCount: null
        })
      }
    },
    clear:{
      type:Boolean,
      observer(newVal, oldVal) {
        this.setData({
          value: [],
          count:[]
        })
      }
    },
    titleValue:{
      type:Array,
      value:[],
      observer(newVal, oldVal) {
        if(newVal) {
          this.setData({
            value: newVal,
          })
          let list = this.properties.list
          if (list[0].type == 'time' && newVal[0]) {
            this.setData({
              time: newVal[0],
            })
          }
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    width: null,
    count: null,
    btnFixed:0,
    value: [],
    parentCount: null,
    childCount: null,
    searchValue: '',
    startDate: '',
    endDate: '',
    today: '',
    dateData: null,
    time: null,
    _time_:null,
    hideMore:true,
    productIndex:null,
    statusIndex:null,
    salesmanIndex:null,
    hideSearch:false,
    dropdownName:"",
    type: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 禁止滑动
     */
    noScroll() {},
    showDropdown(e) {
      let index = e.currentTarget.dataset.parentIndex
      let targetName = e.currentTarget.dataset.targetName
      this.setData({
        dropdownName:targetName,
        [targetName]: this.data[targetName] == index ? null : index,
      })
      if (this.data.parentCount == null) this.triggerEvent('close')
    },
    close(e) {
      this.showDropdown(e)
      this.triggerEvent('close')
    },
    choose(e) {
      let value = e.currentTarget.dataset.value
      let parentIndex = e.currentTarget.dataset.parentIndex
      let name = e.currentTarget.dataset.name
      this.setData({
        [`count[${parentIndex}]`]: value,
        [`value[${parentIndex}]`]: name
      })
      this.showDropdown(e)
      this.triggerEvent('choose', {
        parentIndex: parentIndex,
        value: value,
        name:name,
        titleValue: this.data.value
      })
      
    },
    focus() {
      this.setData({
        hideSearch:true
      })
    },
    blur() {
      this.setData({
        hideSearch: false
      })
    },
    input(e) {
      let value = e.detail.value
      this.setData({
        searchValue: value
      })
    },
    confirm(e) {
      let value = e.currentTarget.dataset.type ? '' : e.detail.value
      this.setData({
        searchValue: value
      })
      let data={
        value:value,
        index:this.data[this.data.dropdownName]
      }
      this.triggerEvent('search',data)
    },
    radio(e) {
      let value = e.currentTarget.dataset.value
      let name = e.currentTarget.dataset.name
      this.setData({
        dateData: name,
        startDate: null,
        endDate: null
      })
    },
    pickerChange(e) {
      let date = e.detail.value
      let target = `${e.currentTarget.dataset.target}Date`
      this.setData({
        [target]: date
      })
      this.setData({
        dateData: `${this.data.startDate}—${this.data.endDate}`,
      })
    },
    clearTime() {
      this.setData({
        dateData: null,
        startDate: null,
        endDate: null,
      })
    },
    clearData(){
      this.setData({
        salesmanIndex: null,
        statusIndex: null,
        productIndex: null,
      })
    },
    submit(e) {
      let value = this.data.dateData
      let parentIndex = e.currentTarget.dataset.parentIndex
      if(value) {
        this.setData({
          [`value[${parentIndex}]`]: value.length > 5 ? `${value.substring(0, 10)}...` : value
        })
      } else{
        this.setData({
          value: null
        })
      }
      this.showDropdown(e)
      this.properties.list[parentIndex].data.forEach(el =>{
        if(el.name == value) value = el.value
      })
      this.triggerEvent('choose', {
        parentIndex: parentIndex,
        value: value,
        titleValue: this.data.value
      })
    },
    chooseTime(e) {
      let date = e.detail.value
      let arr = date ? date.split('-') : []
      let parentIndex = e.currentTarget.dataset.parentIndex
      if(date) {
        this.setData({
          time: `${arr[0]}${arr[1]}`,
          _time_: date
        })
      }
      this.showDropdown(e)
      console.log(date)
      let val = this.data.value
      val[0] = this.data.time
      this.triggerEvent('choose', {
        parentIndex: parentIndex,
        value: this.data.time,
        titleValue: val,
        time2:date
      })
    },
    cancel(e) {
      this.showDropdown(e)
    },
    showMore() {
      this.setData({
        hideMore: !this.data.hideMore
      })
    },
    chooseMore(e) {
      let target = e.currentTarget.dataset.target
      let value = e.currentTarget.dataset.value
      this.setData({
        [target]:value
      })
    },
    sumbitMore(e){
      let parentIndex = e.currentTarget.dataset.parentIndex
      this.showDropdown(e)
      this.triggerEvent('choose', {
        parentIndex: parentIndex,
        value: {
          status: this.data.statusIndex,
          product: this.data.productIndex,
          salesman: this.data.salesmanIndex
        },
        titleValue: this.data.value
      })
    }
  },
  ready() {
    
    let now = new Date()
    this.setData({
      today: now.format('yyyy-MM-dd'),
      time: !this.data.time ? now.format('yyyyMM') : this.data.time,
      _time_: !this.data._time_ ? now.format('yyyy-MM') : this.data._time_,
      type: app.globalData.type
    })
  },
  
})