- 单元格内容使用layui的
templet
功能。如: {field:'jtcyGxmc', title: '关系名称',templet: '#selectGxmc',sort:false}
- 代码:
layui.use(['table', 'layer', 'element', 'jquery', 'form'], function(table, layer, element, $, form){
});
- table.render渲染done回调
table.render({
elem: '#grid'
,cols: [[
{field: 'id', title: 'ID', width: 80, sort: true}
,{field: 'username', title: '用户名', width: 120}
,{field: 'email', title: '邮箱', minWidth: 150}
,{field: 'city', title: '城市', width: 100}
,{field: 'selectName', title: '下拉框', templet: '#selectGxmc'}
,{field: 'layuiData', title: '日期框', templet: '#layuiDataGxmc'}
,{field: 'editText', title: '编辑框', edit:'text'}
]]
,data: [{
"id": "10001"
,"username": "杜甫"
,"email": "xianxin@layui.com"
,"city": "浙江杭州"
,"layuiData":''
,'editText':'1'
},{"id": "10002"
,"username": "杜甫2"
,"email": "xianxin@layui.com"
,"city": "浙江杭州"
}, {
"id": "10003"
,"username": "杜甫3"
,"email": "xianxin@layui.com"
,"city": "浙江杭州"
}]
,skin: 'line'
,even: true
,done: function (res, curr, count) {
count || this.elem.next('.layui-table-view').find('.layui-table-header').css('overflow', 'auto');
layui.each($('select'), function (index, item) {
var elem = $(item);
elem.val(elem.data('value')).parents('div.layui-table-cell').css('overflow', 'visible');
});
}
});
- 模版
<script type="text/html" id="selectGxmc" >
<select name='selectName' lay-verify='selectVerify' lay-search=''">
<option value=""></option>
<option value="父子">父子</option>
<option value="母子">母子</option>
<option value="姐弟">姐弟</option>
<option value="弟弟">弟弟</option>
</select>
</script>
- 监听
form.on('select', function (data) {
debugger;
var elem = $(data.elem);
var trElem = elem.parents('tr');
var tableData = table.cache['grid'];
tableData[trElem.data('index')][elem.attr('name')] = data.value;
});
- 样式调整
.layui-table-cell { overflow: visible !important; }
.layui-table-box {
overflow: visible;
}
.layui-table-body {
overflow: visible;
}
td .layui-form-select{
margin-top: -10px;
margin-left: -15px;
margin-right: -15px;
}