vue3中,tabs标签页的使用、调用方法实现动态style

vue3中,tabs标签页的使用、调用方法实现动态style

效果

1

在这里插入图片描述

2

在这里插入图片描述

代码

src\views\myCenter\projectCenter\projectSettings.vue

<!--
@Description 项目中心 - 项目基础设置
@author wdd
@date 2023/11/9
-->
<template>
    <centerHead title="项目基础设置"></centerHead>
    <tab :tabList="tabList" :defaultState="tabIndex" @changeTab="tabChange"></tab>
    <Milestone v-show="tabIndex === '0'"  />
    <RiskGrade v-show="tabIndex === '1'"  />
</template>
<script lang="ts" setup>
import Milestone from './components/Milestone.vue'
import RiskGrade from './components/RiskGrade.vue'
import {ref} from 'vue'
const tabList = ref([
    {state: "0", name: '里程碑节点'},
    {state: "1", name: '风险阈值设置'},
  ]);
  const tabIndex = ref('0');
    // tab切换
    const tabChange = (val:any) => {
    tabIndex.value = val;
  }
</script>

src\views\myCenter\projectCenter\components\Milestone.vue

<!--
@Description 项目中心 - 项目基础设置 - 里程碑节点
@author wdd
@date 2023/11/10
-->
<template>
    <div class="content">
        <div class="left">
            <h4>项目类型区</h4>
            <el-button type="primary" @click="add(1)">新增</el-button>
            <el-button type="primary" @click="saveType">保存类型</el-button>
            <el-table :header-cell-style="{ background: '#eef1f6' }" ref="tableRef" :data="financeTableData"
                class="table">
                <el-table-column type="index" width="55" label="序号"></el-table-column>
                <el-table-column label="项目类型" width="180">
                    <template #default="scope">
                        <el-input v-if="scope.row.editType" v-model="scope.row.description" maxlength="100"
                            οninput="value=value.replace(/[^\d.]/g,'')" />
                        <span v-else>{{ scope.row.description }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="right">
                    <template #default="scope">
                        <el-button v-if="scope.row.editType" link type="primary"
                            @click="handleSave('项目类型', scope.row,scope.$index)">
                            保存
                        </el-button>
                        <el-button v-if="!scope.row.editType" link type="danger"
                            @click="handleDelete('项目类型',scope.$index)">
                            删除
                        </el-button>
                        <el-button v-if="!scope.row.editType" link type="primary"
                            @click="handleEdit( '项目类型',scope.row,scope.$index)">
                            编辑
                        </el-button>
                        <el-button v-if="!scope.row.editType" link type="primary"
                            @click="nodeClick( scope.row, scope.$index)">
                            里程碑节点管理
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="segment"></div>
        <div class="right">
            <div class="title">
                <h4>里程碑节点区</h4>
                <el-button type="primary" @click="add(2)">新增</el-button>
                <el-button type="primary" @click="saveNode">保存节点</el-button>
            </div>
            <el-table :header-cell-style="{ background: '#eef1f6' }" ref="tableRef" :data="nodeList" class="table">
                <el-table-column type="index" width="55" label="序号"></el-table-column>
                <el-table-column label="项目节点" width="180">
                    <template #default="scope">
                        <el-input v-if="scope.row.editType" v-model="scope.row.description" maxlength="100"
                            οninput="value=value.replace(/[^\d.]/g,'')" />
                        <span v-else>{{ scope.row.description }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="right">
                    <template #default="scope">
                        <el-button v-if="scope.row.editType" link type="primary"
                            @click="handleSave('节点', scope.row,scope.$index)">
                            保存
                        </el-button>
                        <el-button v-if="!scope.row.editType" link type="danger"
                            @click="handleDelete('节点',scope.$index)">
                            删除
                        </el-button>
                        <el-button v-if="!scope.row.editType" link type="primary"
                            @click="handleEdit('节点', scope.row,scope.$index)">
                            编辑
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
    </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus'
const financeTableData = ref([{
    id: 1,
    description: '创新类',
    editType: true,
}, {
    id: 2,
    description: '创意类',
    editType: true,
}])
const nodeList = ref([{
    id: 1,
    description: '内容征集',
    editType: true,
}, {
    id: 2,
    description: '作品提交',
    editType: true,
}])
// 新增
const add = (val: any) => {
    if (val == 1) {
        financeTableData.value.push({
            description: '',
            editType: true
        })
    } else {
        nodeList.value.push({
            description: '',
            editType: true
        })
    }
}
const saveType = () => { }
const saveNode = () => { }
// 里程碑节点
const nodeClick = (row: any, index: any) => { }
// 编辑类型
const handleEdit = (type: any, row: any, index: any) => {
    if (type == '项目类型') {
        financeTableData.value[index].editType = true
    } else {
        nodeList.value[index].editType = true
    }
}
const handleDelete = (type: any, index: any) => {
    ElMessageBox.confirm(`此操作将删除该${type}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        if (type == '项目类型') {
            financeTableData.value.splice(index, 1)
            financeTableData.value[index].editType = false
        } else {
            nodeList.value.splice(index, 1)
            nodeList.value[index].editType = false
        }
        ElMessage.success('删除成功!')
    }).catch(() => {
        ElMessage.error('删除失败!')
    })
}
const handleSave = (type: any, row: any, index: any) => {
    if (type == '项目类型') {
        financeTableData.value[index].editType = false
        financeTableData.value[index] = row
    } else {
        nodeList.value[index].editType = false
        nodeList.value[index] = row
    }
}
</script>
<style lang="scss" scoped>
.content {
    margin-top: 20px;
    margin-left: 16px;
    display: flex;
    width: 60%;
    .left {
        flex: 1;
        border-radius: 4px;
        padding: 10px;
        margin-right: 10px;
    }
    .segment {
        border-left: 1px dashed #ccc;
    }
    .right {
        flex: 1;
        margin-left: 10px;
        border-radius: 4px;
        padding: 10px;
        .title {
            width: 100%;
            height: 46px;
        }
    }
    h4 {
        float: left;
    }
    .el-button {
        float: right;
        margin-bottom: 10px;
        margin-left: 10px;
    }
}
</style>

src\views\myCenter\projectCenter\components\RiskGrade.vue

<!--
@Description 项目中心 - 项目基础设置 - 风险阈值设置
@author wdd
@date 2023/11/9
-->
<template>
    <div class="content">
        <div class="set-box">
            <el-button type="primary" @click="add">新增</el-button>
            <el-table :header-cell-style="{ background: '#eef1f6' }" ref="tableRef" :data="tableData" class="table">
                <el-table-column prop="riskLevel" align="center" label="风险等级"></el-table-column>
                <el-table-column prop="minNumber" align="center" label="超出天数"></el-table-column>
                <el-table-column prop="describe" align="center" label="描述"></el-table-column>
                <el-table-column label="颜色表示" align="center" width="80">
                    <template #default="scope" >
                        <div :style="{backgroundColor:scope.row.color,paddingLeft:'50px',textAlign:'center',width:'50px',height:'20px'}"></div>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="right" width="90">
                    <template #default="scope">
                        <span class="text-btn" @click="edit(scope.row)">编辑</span>
                        <span class="del-btn" @click="onDelete(scope.row)">删除</span>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="save">
            <el-button type="primary" @click="onSave">保存</el-button>
        </div>
    </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus'
const tableData = ref([{
    riskLevel: '一级',
    minNumber: '60',
    color:'#da7171',
    describe: '严重风险',
}, {
    riskLevel: '二级',
    minNumber: '30',
    color:'#f56c6c',
    describe: '高风险',
}, {
    riskLevel: '三级',
    minNumber: '15',
    color:'#e6a23c',
    describe: '中风险',
}, {
    riskLevel: '四级',
    minNumber: '7',
    color:'#67c23a',
    describe: '低风险',
}, {
    riskLevel: '五级',
    minNumber: '3',
    color:'#67c23a',
    describe: '无风险',
}])
const add = () => { }
const edit = (row: any) => {
    console.log(row);
}
const onDelete = (row: any) => {
    console.log(row);
    ElMessageBox.confirm('此操作将删除该费率, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        ElMessage.success('删除成功!')
    }).catch(() => {
        ElMessage.error('删除失败!')
    })
}
const onSave = () => { }
</script>
<style lang="scss" scoped>
.content {
    margin-top: 20px;
    margin-right: 20px;
    width: 100%;
    .set-box {
        margin: 20px 0;
        width: 60%;
        padding-left: 16px;
    }
    .el-button {
        float: right;
        margin-bottom: 10px;
    }
    .el-table {
        margin: 20px 0;
    }
    .text-btn {
        font-size: 12px;
        color: #409EFF;
        cursor: pointer;
    }
    .del-btn {
        font-size: 12px;
        color: #F56C6C;
        margin-left: 6px;
        cursor: pointer;
    }
    .save {
        margin-top: 40px;
        padding-left: 16px;
        .el-button {
            float: left;
            width: 120px;
        }
    }
}
</style>
  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
vue3动态组件实现tabs标签的步骤如下: 1. 创建tabs组件,包含tabs和tab两个子组件。tabs组件负责显示当前选的tab和切换tab,tab组件则负责显示tab的内容。 2. 在tabs组件定义一个数组,用于存储所有的tab信息。每个tab信息包括tab的名称和对应的组件。 3. 在tabs组件使用v-for指令循环渲染所有的tab,同时使用动态组件来渲染对应的组件。 4. 在tab组件定义一个插槽,用于显示tab的内容。 5. 在tabs组件定义一个方法,用于切换tab。该方法接受一个index参数,表示要切换到的tab的索引。该方法根据索引更新当前选的tab,并将对应的组件渲染出来。 6. 在tabs组件使用v-on指令绑定点击事件,调用切换tab的方法。 7. 在父组件引入tabs组件,并传入所有的tab信息。 8. 使用tabs组件来显示tab标签。 示例代码如下: // Tabs.vue <template> <div class="tabs"> <div class="tab-list"> <div class="tab" v-for="(tab, index) in tabs" :key="tab.name" :class="{ active: isActiveTab(index) }" @click="selectTab(index)" >{{ tab.name }}</div> </div> <div class="tab-content"> <component :is="tabs[currentTab].component"></component> </div> </div> </template> <script> export default { name: "Tabs", props: ["tabs"], data() { return { currentTab: 0, }; }, methods: { isActiveTab(index) { return this.currentTab === index; }, selectTab(index) { this.currentTab = index; }, }, }; </script> // Tab.vue <template> <div class="tab-content"> <slot></slot> </div> </template> // App.vue <template> <div class="app"> <Tabs :tabs="tabs"></Tabs> </div> </template> <script> import Tabs from "./Tabs.vue"; import Tab from "./Tab.vue"; import Tab1 from "./Tab1.vue"; import Tab2 from "./Tab2.vue"; export default { name: "App", components: { Tabs, Tab, Tab1, Tab2, }, data() { return { tabs: [ { name: "Tab 1", component: "Tab1" }, { name: "Tab 2", component: "Tab2" }, ], }; }, }; </script>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值