原生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>
相关推荐
UIUV3 分钟前
RAG技术学习笔记(含实操解析)
javascript·langchain·llm
SuperEugene5 分钟前
Vue状态管理扫盲篇:如何设计一个合理的全局状态树 | 用户、权限、字典、布局配置
前端·vue.js·面试
阿懂在掘金32 分钟前
defineModel 是进步还是边界陷阱?双数据源组件的选择逻辑
vue.js·源码阅读
李剑一39 分钟前
要闹哪样?又出现了一款新的格式化插件,尤雨溪力荐,速度提升了惊人的45倍!
前端·vue.js
阿虎儿1 小时前
React Context 详解:从入门到性能优化
前端·vue.js·react.js
颜酱2 小时前
理解二叉树最近公共祖先(LCA):从基础到变种解析
javascript·后端·算法
FansUnion2 小时前
我如何用 Next.js + Supabase + Cloudflare R2 搭建壁纸销售平台——月成本接近 $0
javascript
左夕3 小时前
分不清apply,bind,call?看这篇文章就够了
前端·javascript
滕青山4 小时前
文本行过滤/筛选 在线工具核心JS实现
前端·javascript·vue.js
时光不负努力4 小时前
编程常用模式集合
前端·javascript·typescript