<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="./src/css/layui.css">
</head>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.slim.js"></script>
<body>
<div id="table"></div>
</body>
</html>
<script>
treeTabel({
elem:'#table'
,cols: [[
{checkbox: true,}
,{field:'id', title: 'ID', width:80, fixed: true}
,{field:'username', title: '用户名', width:80}
,{field:'sex', title: '性别', width:80,}
,{field:'city', title: '城市', width:80}
,{field:'sign', title: '签名', width:177}
,{field:'experience', title: '积分', width:80}
,{field:'score', title: '评分', width:80}
,{field:'classify', title: '职业', width:80}
,{field:'wealth', title: '财富', sort: true, width:135}
]]
})
function treeTabel(data){
var elem = $(data.elem);
var cols = data.cols; //json
var header= function(options){
options = options || {};
return ['<table cellspacing="0" cellpadding="0" border="0" class="layui-table">',
'<thead><tr>',
''+ ($.each(cols,function(){'hello'})) +'',
'</tr></thead>',
'</table>' ].join("");
}
elem.append(header())
}
</script>
就这些代码其实我可以new一个对象each 好。然后添加进这里 但是为了代码精简
我用这种方法写怎么让 return 里 $.each(cols,function(){'hello'}) 执行花括号里的值 而不是直接输入数组里的对象?
代码量少和代码精简应该不是等价的