layui表格设置单选框
第一步,引入layui的包
<script src="/layui/layui.js"></script>
<link rel="stylesheet" href="/layui/css/layui.css" media="all">
第二步,js中激活layui.use,数据表格id绑定
<table id="demo" lay-filter="test"></table>
<script>
layui.use('table', function(){
var table = layui.table;
//第一个实例
table.render({
elem: '#demo'
,height: 312
,url: '/demo/table/user/' //数据接口
,page: true //开启分页
,cols: [[ //表头
{title: '单选框',width:100,type:'radio'}
{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
,{field: 'username', title: '用户名', width:80}
,{field: 'sex', title: '性别', width:80, sort: true}
,{field: 'city', title: '城市', width:80}
,{field: 'sign', title: '签名', width: 177}
,{field: 'experience', title: '积分', width: 80, sort: true}
,{field: 'score', title: '评分', width: 80, sort: true}
,{field: 'classify', title: '职业', width: 80}
,{field: 'wealth', title: '财富', width: 135, sort: true}
]]
});
});
</script>
注意;filed其他列里面不能有左浮动,不然会导致单选框在其他行,并且不显示。
如果还有问题,可以在col中只写一个单选框试试。
给单选框设置监听事件
table.on('radio(test)', function(obj){
}
注意:radio表示单选框,其他列入checkbox复选框等
test表示<table id="demo" lay-filter="test"></table>
中的lay-filter的值。
单选框支持layui版本为 :layui 2.4.0
如对你有帮助请点个赞吧。