原生Table写一行保证两条数据

javascript 复制代码
<template>
    <el-dialog v-model="visible" width="80%" :show-close="false" :style="{ padding: 0, height: '70%' }">
        <template #title>
            <div class="section-header" style="background-color: #2b3a4a; padding: 10px; color: white;">
                {{ t('user_recom.会员分成设置') }}
            </div>
        </template>

        <!-- 表格 -->
        <table class="order_write order_table_top">
            <colgroup>
                <col width="12%" />
                <col />
                <col width="12%" />
                <col />
            </colgroup>

            <tbody>
                <tr v-for="(row, index) in tableRows" :key="index">
                    <!-- 左 -->
                    <th class="parrent_menu">
                        <el-checkbox v-if="row[0]" v-model="row[0].isChecked" />
                    </th>
                    <td>
                        <template v-if="row[0]">
                            {{ row[0].type }} →
                            {{ row[0].surcharge_name }}
                        </template>
                    </td>

                    <!-- 右 -->
                    <th class="parrent_menu">
                        <el-checkbox v-if="row[1]" v-model="row[1].isChecked" />
                    </th>
                    <td>
                        <template v-if="row[1]">
                            {{ row[1].type }} →
                            {{ row[1].surcharge_name }}
                        </template>
                    </td>
                </tr>
            </tbody>
        </table>
        <table class="order_write order_table_top">
            <colgroup>
                <col width="12%" />
                <col />
                <col width="12%" />
                <col />
            </colgroup>

            <tbody>
                <tr v-for="(row, index) in tableRows" :key="index">
                    <!-- 左 -->
                    <th class="parrent_menu">
                        <el-checkbox v-if="row[0]" v-model="row[0].isChecked" />
                    </th>
                    <td>
                        <template v-if="row[0]">
                            {{ row[0].type }} →
                            {{ row[0].surcharge_name }}
                        </template>
                    </td>

                    <!-- 右 -->
                    <th class="parrent_menu">
                        <el-checkbox v-if="row[1]" v-model="row[1].isChecked" />
                    </th>
                    <td>
                        <template v-if="row[1]">
                            {{ row[1].type }} →
                            {{ row[1].surcharge_name }}
                        </template>
                    </td>
                </tr>
            </tbody>
        </table>


        <!-- 弹窗底部 -->
        <template #footer>
            <div class="footer-btn">
                <el-button type="primary" @click="handleSave" :disabled="saveLoading">{{ t('btn.enter') }}</el-button>
                <el-button type="primary" plain @click="visible = false">
                    {{ t('btn.close') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
import { ref, computed, reactive } from 'vue'
import { useI18n } from 'vue-i18n'
import { toast } from '~/composables/util'
import { formatAmount } from '~/composables/validate'
import { recomdService } from '~/api/user'
const { t } = useI18n()
const visible = ref(false)
const tableList = ref([])
const saveLoading = ref(false)

const tableRows = computed(() => {
    const rows = []
    for (let i = 0; i < tableList.value.length; i += 2) {
        rows.push([
            tableList.value[i],
            tableList.value[i + 1] || null
        ])
    }
    return rows
})


const getSettingList = async () => {
    const res = await recomdService.getCommissionInit()
    if (res.code === 200) {
        let resData = res.data
        if (resData.length > 0) {
            tableList.value = resData.map(item => {
                return {
                    ...item,
                    isChecked: Boolean(item.isChecked)
                }
            })
        }

    } else {
        tableList.value = []
        setTimeout(() => {
            toast(res.msg, 'error')
            visible.value = false
        }, 2000)
    }

}


const handleSave = async () => {
    const checkedCodes = tableList.value
        .filter(item => item.isChecked)
        .map(item => item.surcharge_code)

    if (!checkedCodes.length) {
        toast(t('sys.choose-one-item'), 'warning')
        return
    }

    saveLoading.value = true
    try {
        const res = await recomdService.setMemberCommission({
            : checkedCodes
        })
        if (res.code === 200) {
            visible.value = false
        }
    } finally {
        saveLoading.value = false
    }
}




const open = () => {
    visible.value = true
    getSettingList()
}

defineExpose({ open })


</script>

<style scoped>
.select-box {
    width: 150px;
    margin-right: 3px;
}

.search-input {
    width: 120px;
    margin-right: 3px;
}

.main-table {
    margin-top: 10px;
    max-height: 480px;
    overflow-y: auto;
}


.charge-wrap {
    display: flex;
    flex-wrap: wrap;
}

.charge-item {
    width: 30%;
    margin-right: 1px;
    white-space: nowrap;
}
</style>
相关推荐
竹林8182 小时前
在Web3前端用Node.js子进程批量校验钱包,我踩了这些性能与安全的坑
javascript·node.js
Kel3 小时前
深入剖析 openai-node 源码:一个工业级 TypeScript SDK 的架构之美
javascript·人工智能·架构
SuperEugene4 小时前
Vue3 模板语法规范实战:v-if/v-for 不混用 + 表达式精简,避坑指南|Vue 组件与模板规范篇
开发语言·前端·javascript·vue.js·前端框架
Luna-player4 小时前
Vue 3 + Vue Router 的路由配置,简单示例
前端·javascript·vue.js
敲代码的约德尔人4 小时前
JavaScript 设计模式完全指南
javascript·设计模式
angerdream4 小时前
最新版vue3+TypeScript开发入门到实战教程之Vue3详解props
javascript·vue.js
~欲买桂花同载酒~5 小时前
项目优化-vite打包优化
前端·javascript·vue.js
kyriewen5 小时前
JavaScript 继承的七种姿势:从“原型链”到“class”的进化史
前端·javascript·ecmascript 6
wangfpp6 小时前
性能优化,请先停手:为什么我劝你别上来就搞优化?
前端·javascript·面试