vue项目中Tab切换,以及不同状态如何显示

6 篇文章 0 订阅

前言

例如:这里主要讲到项目中我们会遇到的Tab切换,以及订单状态的显示。
在这里插入图片描述

一、Tab切换要如何去写?

代码如下(示例):

<el-tabs type="border-card">
      <el-tab-pane label="全部">
        <el-table
          :data="auditData"
          style="width: 100%; margin-top: 1%"
          :header-cell-style="{
            width: '100%',
            textAlign: 'center',
            color: '#999999',
            background: '#F8F9FC'
          }"
          :cell-style="{ textAlign: 'center', color: '#666666' }"
        >
          <el-table-column prop="planId" label="订单ID">
            <template slot-scope="scope">
              {{ scope.row.planId }}
            </template>
          </el-table-column>
          <el-table-column
            prop="bindStatus"
            label="状态"
            filter-placement="bottom-end"
          >
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.bindStatus | getBindStatus"
                close-transition
                >{{ scope.row.bindStatus | getBindText }}</el-tag
              >
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="待发货">
        <el-table
          :data="waitAuditData"
          style="width: 100%; margin-top: 1%"
          :header-cell-style="{
            width: '100%',
            textAlign: 'center',
            color: '#999999',
            background: '#F8F9FC'
          }"
          :cell-style="{ textAlign: 'center', color: '#666666' }"
        >
          <el-table-column prop="planId" label="订单ID">
            <template slot-scope="scope">
              {{ scope.row.planId }}
            </template>
          </el-table-column>
          <el-table-column
            prop="bindStatus"
            label="状态"
            filter-placement="bottom-end"
          >
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.bindStatus | getBindStatus"
                close-transition
                >{{ scope.row.bindStatus | getBindText }}</el-tag
              >
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="已发货">
        <el-table
          :data="throughData"
          style="width: 100%; margin-top: 1%"
          :header-cell-style="{
            width: '100%',
            textAlign: 'center',
            color: '#999999',
            background: '#F8F9FC'
          }"
          :cell-style="{ textAlign: 'center', color: '#666666' }"
        >
          <el-table-column prop="planId" label="订单ID">
            <template slot-scope="scope">
              {{ scope.row.planId }}
            </template>
          </el-table-column>
          <el-table-column
            prop="bindStatus"
            label="状态"
            filter-placement="bottom-end"
          >
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.bindStatus | getBindStatus"
                close-transition
                >{{ scope.row.bindStatus | getBindText }}</el-tag
              >
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="待评价">
        <el-table
          :data="unthroughData"
          style="width: 100%; margin-top: 1%"
          :header-cell-style="{
            width: '100%',
            textAlign: 'center',
            color: '#999999',
            background: '#F8F9FC'
          }"
          :cell-style="{ textAlign: 'center', color: '#666666' }"
        >
          <el-table-column prop="planId" label="计划ID">
            <template slot-scope="scope">
              {{ scope.row.planId }}
            </template>
          </el-table-column>
          <el-table-column prop="name" label="机构名称">
            <template slot-scope="scope">
              {{ scope.row.name }}
            </template>
          </el-table-column>
          <el-table-column
            prop="bindStatus"
            label="状态"
            filter-placement="bottom-end"
          >
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.bindStatus | getBindStatus"
                close-transition
                >{{ scope.row.bindStatus | getBindText }}</el-tag
              >
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>

二、加入data数据,以及状态设置

代码如下(示例):

<script>
export default {
  data() {
    return {
      auditData: [
        {
          planId: "201515231515",
          bindStatus: "待发货"
        },
        {
          planId: "4651341564",
          bindStatus: "已发货"
        },
        {
          planId: "41341431893416",
          bindStatus: "待评价"
        }
      ],
      waitAuditData: [
        {
          planId: "1613113516561",
          bindStatus: "待发货"
        }
      ],
      throughData: [
        {
          planId: "1613113516561",
          bindStatus: "已发货"
        }
      ],
      unthroughData: [
        {
          planId: "1613113516561",
          bindStatus: "待评价"
        }
      ]
    };
  },
  filters: {
    //tag类型
    getBindStatus(bindStatus) {
      const bindColor = {
        待评价: "danger",
        待发货: "",
        已发货: "success"
      };
      return bindColor[bindStatus];
    },
    //颜色名称
    getBindText(bindStatus) {
      const bindColor = {
        待评价: "待评价",
        待发货: "待发货",
        已发货: "已发货"
      };
      return bindColor[bindStatus];
    }
  },
  methods: {
   
  }
};
</script>

总结

提示:这里先介绍项目中我们会遇到的需求,仅供参考

例如:以上就是今天要讲的内容,本文仅仅简单介绍了Tab的使用,如有问题请私信。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue 实现 tab 切换内容有多种方法,其比较常用的有以下几种: 1. 使用 `v-show` 或 `v-if` 指令 这是最简单的一种方法,通过 `v-show` 或 `v-if` 指令来判断当前选tab,然后对应显示或隐藏对应的内容。例如: ```html <div> <button @click="activeTab = 'tab1'">Tab1</button> <button @click="activeTab = 'tab2'">Tab2</button> <button @click="activeTab = 'tab3'">Tab3</button> </div> <div v-show="activeTab === 'tab1'">Tab1 Content</div> <div v-show="activeTab === 'tab2'">Tab2 Content</div> <div v-show="activeTab === 'tab3'">Tab3 Content</div> ``` 2. 使用 `v-bind:class` 指令 通过 `v-bind:class` 指令来给选tab 添加一个激活状态的 CSS 类,然后根据这个类来控制对应的内容的显示或隐藏。例如: ```html <div> <button :class="{ 'active': activeTab === 'tab1' }" @click="activeTab = 'tab1'">Tab1</button> <button :class="{ 'active': activeTab === 'tab2' }" @click="activeTab = 'tab2'">Tab2</button> <button :class="{ 'active': activeTab === 'tab3' }" @click="activeTab = 'tab3'">Tab3</button> </div> <div :class="{ 'show': activeTab === 'tab1' }">Tab1 Content</div> <div :class="{ 'show': activeTab === 'tab2' }">Tab2 Content</div> <div :class="{ 'show': activeTab === 'tab3' }">Tab3 Content</div> ``` 3. 使用组件 将每个 tab 对应的内容封装成一个组件,然后在父组件通过切换组件来实现 tab 切换。这种方法相对于前两种方法更加灵活和可维护,但需要进行组件的拆分和引用。例如: ```html <div> <button @click="activeTab = 'tab1'">Tab1</button> <button @click="activeTab = 'tab2'">Tab2</button> <button @click="activeTab = 'tab3'">Tab3</button> </div> <tab1 v-if="activeTab === 'tab1'"></tab1> <tab2 v-if="activeTab === 'tab2'"></tab2> <tab3 v-if="activeTab === 'tab3'"></tab3> ``` 以上三种方法均可实现 tab 切换内容,选择哪种方法最好取决于具体的业务场景和需求。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值