CarType2.vue 5.23 KB
<!-- components navbar-->
<template>
  <div class="com-car-type-container">
    <van-cell :value="comValue" is-link readonly :title="label" @click="show = true" />
    <van-popup v-model="show" round position="bottom">
      <van-cascader
        v-model="cascaderValue"
        :options="options"
        :closeable="false"
        @close="show = false"
        @change="onChange"
        @finish="onFinish"
        active-color="#3975C6"
        style="height:90%"
        @click.native="onTabChange"
      >
        <template #title>
          <van-search v-model="txtSearch" placeholder="搜索" input-align="center" @input="onSearch" />
          <span class="reset" @click="reset">
            <van-icon name="revoke" />
            &nbsp;重置
          </span>
        </template>
      </van-cascader>
    </van-popup>
  </div>
</template>

<script>
import { debounce } from 'utils'
export default {
  name: 'CarType2',
  props: {
    label: {
      type: String,
      default: '意向车型'
    },
    title: {
      type: String,
      default: '请选择车型'
    },
    value: {
      type: String
    }
  },
  data() {
    return {
      txtSearch: '',
      show: false,
      fieldValue: '',
      cascaderValue: '',
      options: [],
      curOptions: [],
      tabIndex: -1,
      bak: null,
      tagIndex: 0,
      datas: []
    }
  },
  computed: {
    comValue() {
      return this.value || this.title
    }
  },

  mounted() {
    // let pop = document.getElementsByClassName("com-car-type-container")[0];
    // pop.addEventListener('click', function(e) {
    //   let _this = e.target;
    //   if(_this.className.includes('van-tab')){
    //     console.log(e);
    //   }
    // })
    this.getCarBrands()
    setTimeout(() => {
      this.options = [
        {
          text: '宝马',
          value: '330000',
          children: []
        },
        {
          text: '奔驰',
          value: '330001',
          children: []
        }
      ]
      // this.bak = this.tools.deepClone(this.options)
      this.datas[0] = this.options
    }, 1000)
  },

  methods: {
    onTabChange(e) {
      const _this = e.target
      if (_this.className.includes('van-tab--active') || _this.parentNode.className.includes('van-tab--active')) {
        Array.from(document.getElementsByClassName('van-tab')).forEach((item, index) => {
          if (item.getAttribute('aria-selected')) {
            this.tagIndex = index
            this.s = []
            console.log(this.s[0])
            // this.s[index]=this.datas[index];
            // this.curOptions=this.datas[index]
          }
        })
      }
    },
    getCarBrands() {
      this.api.common.getCarBrands().then(res => {
        console.log(res)
      })
    },
    getCarLines() {},
    getCarModels() {},
    onChange({ value, selectedOptions, tabIndex }) {
      console.log(value, selectedOptions)
      this.txtSearch = ''
      if (tabIndex > 1) return
      this.tagIndex = tabIndex + 1
      // if (value === this.options[0].value) {
      setTimeout(() => {
        this.curOptions = selectedOptions[tabIndex]
        this.s = selectedOptions
        this.tabIndex = tabIndex
        const value = [
          { text: 'X5', value: '330100' },
          { text: 'X6', value: '330200' }
        ]
        this.curOptions.children = this.datas[this.tagIndex] = value.map(item => {
          if (tabIndex == 0) {
            item.children = []
          }
          return item
        })
        console.log(this.datas)
        // this.bak = this.tools.deepClone(this.curOptions.children)
      }, 500)
      // }
    },
    onFinish({ selectedOptions }) {
      this.show = false
      this.fieldValue = selectedOptions.map(option => option.text).join('/')
      this.$emit('update:value', this.fieldValue)
    },
    onSearch: debounce(function(v) {
      // let items = this.tabIndex == -1 ? this.options : this.curOptions.children
      // items = this.bak.filter(item=>item.text.includes(v))
      console.log(this.tagIndex)
      if (this.tagIndex == 0) {
        this.options = this.datas[this.tagIndex].filter(item => item.text.toUpperCase().includes(v.toUpperCase()))
      } else {
        this.curOptions.children = this.datas[this.tagIndex].filter(item => item.text.toUpperCase().includes(v.toUpperCase()))
      }
    }),
    reset() {
      setTimeout(() => {
        this.options = [
          {
            text: '宝马',
            value: '330000',
            children: []
          },
          {
            text: '奔驰',
            value: '330001',
            children: []
          }
        ]
        this.bak = this.tools.deepClone(this.options)
        this.tabIndex = -1
      }, 1000)
    }
  }
}
</script>
<style lang="scss" scoped>
.com-car-type-container {
  /deep/.van-cascader {
    &__header {
      margin-top: 10px;
      display: flex;
    }
    &__title {
      flex: 1;
      @include flexcenter();
      .van-search {
        flex: 1;
        padding: 0;
      }
      .reset {
        // flex: 0 0 50px;
        // text-align: right;
        position: absolute;
        right: 14px;
        top: 72px;
        z-index: 111;
        // transform: translateX(20px);
        @include font(15px, 800, $main-color);
      }
    }
    &__close-icon {
      flex: 0 0 30px;
      top: -50px;
    }
  }
}
</style>