原生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>
相关推荐
Moment8 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
爱敲代码的小鱼9 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax
吹牛不交税9 小时前
admin.net-v2 框架使用笔记-netcore8.0/10.0版
vue.js·.netcore
MZ_ZXD00110 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
铅笔侠_小龙虾11 小时前
Flutter 实战: 计算器
开发语言·javascript·flutter
大模型玩家七七11 小时前
梯度累积真的省显存吗?它换走的是什么成本
java·javascript·数据库·人工智能·深度学习
2501_9447114311 小时前
JS 对象遍历全解析
开发语言·前端·javascript
发现一只大呆瓜12 小时前
虚拟列表:支持“向上加载”的历史消息(Vue 3 & React 双版本)
前端·javascript·面试
阔皮大师12 小时前
INote轻量文本编辑器
java·javascript·python·c#
lbb 小魔仙12 小时前
【HarmonyOS实战】React Native 表单实战:自定义 useReactHookForm 高性能验证
javascript·react native·react.js