layui表格种单选框中的回显

 let form = layui.form
        layui.table.render({
            id: 'commanderId',
            elem: '#users-list',
            url: '/team-cpt/competition/findUserByUnitId',
            method: 'post',
            request: {pageName: 'pageNum'},
            where: {cptId: cptId, unitId: unitId,realName:realName,phone:phone},
            parseData: convert2LayuiGrid,
            page: true,
            cols: [[
                {field: 'id', title: 'ID', fixed: 'left', hide: true},
                {type: 'radio', fixed: 'left', width: '10%', hide: false},
                {field: 'realName', title: '姓名', align: 'center', fixed: 'left'},
                {field: 'username', title: '手机号', align: 'center', fixed: 'left'}
            ]],
            done: function (res, curr, count) {
                for (let i = 0; i <res.data.length ; i++) {
                    if (res.data[i].id === commanedrId) {
                        $('div.layui-unselect.layui-form-radio')[i].click()//选中
                        //下面三句是通过更改css来实现选中的效果
                        var index = res.data[i]['LAY_TABLE_INDEX'];
                        // $('tr[data-index=' + index + '] ').addClass('layui-table-click');
                        $('tr[data-index=' + index + '] input[type="radio"]').prop('checked', true);
                        $('tr[data-index=' + index + '] input[type="radio"]').next().addClass('layui-form-radioed');
                        $('tr[data-index=' + index + '] i').addClass('layui-anim-scaleSpring');
                    }
                }
                form.render('radio'); //layui表单中改变属性一定要重新渲染
### 实现单选框选择并确保值正确 为了在 `el-table` 组件中实现单选框的选择功能,并确保所选项能够在页面刷新或其他操作后正确,需遵循特定的方法。 通过设置 `el-table-column` 的 `type="radio"` 属性来启用单选模式[^1]。然而,在默认情况下,Element UI 并不直接支持此类型的列定义用于单选;因此,通常的做法是自定义一列表格列并通过作用域插槽(Scoped Slot)引入单选按钮逻辑。对于每一行数据项而言,应为其赋予唯一的标识符以便于追踪当前被选中的项目。当用户点击某一行的单选按钮时,可以通过事件处理函数更新父级组件的状态变量以记录最新的选择情况。 针对的需求,则是在初始化表格或执行分页、筛选等动作之后重新加载数据示之前,依据已保存的选择状态调整相应行内的单选按钮激活与否。这可能涉及到监听某些生命周期钩子或是响应式属性的变化,从而触发必要的DOM更新过程[^2]。 此外,考虑到复杂场景下的应用需求,比如存在展开行(`expand`)的情况下,还需要特别注意维护好内部状态管理机制,确保即使发生诸如切换页面之类的交互行为也不会丢失原有的选择信息[^3]。 ```html <template> <div> <!-- 单选 --> <el-table :data="tableData" @current-change="handleCurrentChange"> <el-table-column type="index"></el-table-column> <el-table-column label="日期" prop="date"></el-table-column> <el-table-column label="姓名" prop="name"></el-table-column> <el-table-column label="地址" prop="address"></el-table-column> <el-table-column fixed="right" width="80px"> <template slot-scope="{ row }"> <el-radio v-model="selectedRowId" :label="row.id">{{ '' }}</el-radio> </template> </el-table-column> </el-table> <!-- ...其他代码... --> </div> </template> <script> export default { data() { return { selectedRowId: null, tableData: [ // 表格数据... ] }; }, methods: { handleCurrentChange(val) { console.log('当前选中:', val); } } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值