【发布时间】:2020-12-01 08:20:52
【问题描述】:
我的网页有超过 3 个制表符,表格之间的图表也很少。
根据要求,我们不固定表格的大小。因此,根据数据,表格高度会有所不同。
我想修复 UI 中表格的标题。这样在垂直滚动时,标题将保持固定,仅滚动数据。
var tabledata = [
{id:1, name:"Oli Bob", progress:12, gender:"male", rating:1, col:"red", dob:"19/02/1984", car:1},
{id:2, name:"Mary May", progress:1, gender:"female", rating:2, col:"blue", dob:"14/05/1982", car:true},
{id:3, name:"Christine Lobowski", progress:42, gender:"female", rating:0, col:"green", dob:"22/05/1982", car:"true"},
{id:4, name:"Brendon Philips", progress:100, gender:"male", rating:1, col:"orange", dob:"01/08/1980"},
{id:5, name:"Margret Marmajuke", progress:16, gender:"female", rating:5, col:"yellow", dob:"31/01/1999"},
{id:6, name:"Frank Harbours", progress:38, gender:"male", rating:4, col:"red", dob:"12/05/1966", car:1},
{id:1, name:"Oli Bob", progress:12, gender:"male", rating:1, col:"red", dob:"19/02/1984", car:1},
{id:2, name:"Mary May", progress:1, gender:"female", rating:2, col:"blue", dob:"14/05/1982", car:true},
{id:3, name:"Christine Lobowski", progress:42, gender:"female", rating:0, col:"green", dob:"22/05/1982", car:"true"},
{id:4, name:"Brendon Philips", progress:100, gender:"male", rating:1, col:"orange", dob:"01/08/1980"},
{id:5, name:"Margret Marmajuke", progress:16, gender:"female", rating:5, col:"yellow", dob:"31/01/1999"},
{id:6, name:"Frank Harbours", progress:38, gender:"male", rating:4, col:"red", dob:"12/05/1966", car:1},
{id:1, name:"Oli Bob", progress:12, gender:"male", rating:1, col:"red", dob:"19/02/1984", car:1},
{id:2, name:"Mary May", progress:1, gender:"female", rating:2, col:"blue", dob:"14/05/1982", car:true},
{id:3, name:"Christine Lobowski", progress:42, gender:"female", rating:0, col:"green", dob:"22/05/1982", car:"true"},
{id:4, name:"Brendon Philips", progress:100, gender:"male", rating:1, col:"orange", dob:"01/08/1980"},
{id:5, name:"Margret Marmajuke", progress:16, gender:"female", rating:5, col:"yellow", dob:"31/01/1999"},
{id:6, name:"Frank Harbours", progress:38, gender:"male", rating:4, col:"red", dob:"12/05/1966", car:1},
{id:1, name:"Oli Bob", progress:12, gender:"male", rating:1, col:"red", dob:"19/02/1984", car:1},
{id:2, name:"Mary May", progress:1, gender:"female", rating:2, col:"blue", dob:"14/05/1982", car:true},
{id:3, name:"Christine Lobowski", progress:42, gender:"female", rating:0, col:"green", dob:"22/05/1982", car:"true"},
{id:4, name:"Brendon Philips", progress:100, gender:"male", rating:1, col:"orange", dob:"01/08/1980"},
{id:5, name:"Margret Marmajuke", progress:16, gender:"female", rating:5, col:"yellow", dob:"31/01/1999"},
{id:6, name:"Frank Harbours", progress:38, gender:"male", rating:4, col:"red", dob:"12/05/1966", car:1},
{id:1, name:"Oli Bob", progress:12, gender:"male", rating:1, col:"red", dob:"19/02/1984", car:1},
{id:2, name:"Mary May", progress:1, gender:"female", rating:2, col:"blue", dob:"14/05/1982", car:true},
{id:3, name:"Christine Lobowski", progress:42, gender:"female", rating:0, col:"green", dob:"22/05/1982", car:"true"},
];
//initialize table
var table = new Tabulator("#tblData1", {
data:tabledata, //assign data to table
autoColumns:false, //create columns from data field names
printAsHtml:true,
columns:[ //define the table columns
{title:"Name", field:"name", editor:"input", width:"15%"},
<!-- {title:"Task Progress", field:"progress", hozAlign:"left", formatter:"progress", editor:true}, -->
{title:"Gender", field:"gender", width:95, editor:"select", editorParams:{values:["male", "female"]}, width:"15%"},
<!-- {title:"Rating", field:"rating", formatter:"star", hozAlign:"center", width:100, editor:true}, -->
{title:"Color", field:"col",headerFilter:true, width:130, editor:"input", width:"15%"},
{title:"Date Of Birth", field:"dob", width:130, sorter:"date", hozAlign:"center", width:"15%"},
{title:"Driver", field:"car", width:90, hozAlign:"center", formatter:"tickCross", sorter:"boolean", editor:true, width:"16%"},
{title:"Test",width:"24%",
columns:[
{title:"Test1",width:"12%"},
{title:"Test2",width:"12%"},
]},
],
});
var table = new Tabulator("#tblData2", {
data:tabledata, //assign data to table
autoColumns:false, //create columns from data field names
printAsHtml:true,
columns:[ //define the table columns
{title:"Name", field:"name", editor:"input", width:"15%"},
<!-- {title:"Task Progress", field:"progress", hozAlign:"left", formatter:"progress", editor:true}, -->
{title:"Gender", field:"gender", width:95, editor:"select", editorParams:{values:["male", "female"]}, width:"15%"},
<!-- {title:"Rating", field:"rating", formatter:"star", hozAlign:"center", width:100, editor:true}, -->
{title:"Color", field:"col",headerFilter:true, width:130, editor:"input", width:"15%"},
{title:"Date Of Birth", field:"dob", width:130, sorter:"date", hozAlign:"center", width:"15%"},
{title:"Driver", field:"car", width:90, hozAlign:"center", formatter:"tickCross", sorter:"boolean", editor:true, width:"16%"},
{title:"Test",width:"24%",
columns:[
{title:"Test1",width:"12%"},
{title:"Test2",width:"12%"},
]},
],
});
<html>
<head>
<script src="js/tabulator.min.js"></script>
<link href="css/tabulator.min.css" rel="stylesheet"/>
</head>
<body>
<div id="tblData1" style="width:550px;"></div>
<br><br><br><br>
<div id="tblData2" style="width:550px;"></div>
<script type="text/javascript">
</script>
</body>
</html>
类似示例:https://datatables.net/extensions/fixedheader/examples/options/columnFiltering.html
【问题讨论】: