vue 双层tab切换

tab用自定义model方式封装,两个tab都会给默认值从1开始

第一层tab切换时, 加上change事件,触发事件时给第二层tab赋值 1(这里是因为第一层tab切换,第二层tab也要从第1个开始),掉接口方法

第二层tab切换时, 加上change事件,触发事件时,掉接口方法,传入1 ,(这里是因为接口方法中如果传入值,则页码默认从1开始)这样第二层tab切换时,页码也是从1开始

第一层tab代码

<template>
  <div class="pagetable">
    <div class="item" @click="getValue(item.value)" v-for="item in option" :key="item.value" :class="value == item.value ? 'on' : '' ">
      {{item.label}}
    </div>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      default: ""
    },
    option: {
      default: () => []
    }
  },
  model: {
    prop: "value",
    event: 'getValue'
  },
  methods: {
    getValue (val) {
      this.$emit('getValue', val)
    }
  }
}
</script>

<style lang="less" scoped>
.pagetable {
  background: #ffff;
  width: 100%;
  height: 40px;
  display: flex;
  align-items: center;
  padding-left: 20px;
}
.item {
  cursor: pointer;
  height: 40px;
  line-height: 40px;
  padding: 0 20px;
}
.on {
  background: skyblue;
  border-radius: 6px 6px 0 0;
  color: green;
}
</style>

第二层tab

<template>
  <div class="pagetable">
    <div class="item" @click="getValue(item.value)" v-for="item in option" :key="item.value" :class="value == item.value ? 'on' : '' ">
      {{item.label}}
    </div>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      default: ""
    },
    option: {
      default: () => []
    }
  },
  model: {
    prop: "value",
    event: 'getValue'
  },
  methods: {
    getValue (val) {
      this.$emit('getValue', val)
      this.$emit('change', val)
    }
  }
}
</script>

<style lang="less" scoped>
.pagetable {
  height: 40px;
  display: flex;
  border-bottom: 1px solid #dcdee6;
  margin-bottom: 20px;
  background: #fff;
  .item {
    height: 40px;
    line-height: 40px;
    margin-right: 50px;
    cursor: pointer;
    position: relative;
    &.on {
      color: #104fa7;
      &::after {
        content: "";
        position: absolute;
        left: 0;
        bottom: 0;
        width: 100%;
        height: 3px;
        background: #104fa7;
      }
    }
  }
}
</style>

父页面,引入 2层tab组件

<template>
  <div class="box">
    <!-- 第一层tab -->
    <PageTable :option='pageList' v-model="search.costType" />
    <div class="search-bg">
      <SearchInput v-model="search.name" />
      <Searchage v-model="search.age" />
      <el-button type="primary" @click="btn(1)">查询</el-button>
    </div>
    <!-- 第二层tab -->
    <GoloTable @change="btn(1)" :option='tableList' v-model="search.bookingStatus" />
  </div>
</template>

<script>
const option = {
  costType: 1, // 第一层tab
  bookingStatus: 1, // 第二层tab
  name: '',
  age: '',
  num: '', // 页码
}
export default {
  data () {
    return {
      pageList, // 第一层数据
      tableList, // 第二层数据
      search: { // 查询条件
        ...option
      }
    }
  },
  // 刚进页面掉接口
  created () {
    this.btn()
  },
  watch: {
    // 监听第一层tab改变时,掉接口方法
    'search.costType': {
      deep: true,
      handler (val) {
        this.search.bookingStatus = 1
        // 掉接口方法
        this.btn(1)
      },
    },
  },
  methods: {
    // 接口方法
    btn (page) {
      // 页码从1 开始
      if (page) this.search.num = page
      const params = JSON.parse(JSON.stringify(this.search))
      console.log(params);
    }
  }
}
// 第一层tab 数据
const pageList = [
  {
    label: "组织性筛查",
    value: 1,
  },
  {
    label: "机会性筛查",
    value: 2,
  },
]
// 第二层tab 数据
const tableList = [
  {
    label: "待签到",
    value: 1,
  },
  {
    label: "已签到",
    value: 2,
  },
  {
    label: "已取消",
    value: 3,
  },
]
</script>

<style lang="less" scoped>
.box {
  width: 100%;
  height: 100%;
}
.search-bg {
  margin-top: 20px;
  padding-left: 20px;
  display: flex;
}
</style>

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学不会•

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值