admin管理员组文章数量:1425799
I've been working with dataTables for a short period of time and I have a DataTable with two columns:
TableVehUsage = $("#TableVehUsage ").DataTable({
data: [],
ordering: true,
paginate: false,
"info": false,
fixedHeader: {header: true},
columns: [
{ data: "Vehicle", title: "Vehicle" },
{ data: "Serial", title: "Serial" }
],
"columnDefs": [
{
"targets": 0,
"render": function (data, type, full, meta) {
// If it is rendering the cell contents
if (type === 'display') {
switch (data) {
case "-":
return "-";
default:
if (full.IsOnSale == true)
return '<span style="color:red" onclick="ToParentTab(' + full.Id + ')">' + data + '</span>';
else
return '<span onclick="ToParentTab(' + full.Id + ')">' + data + '</span>';
}
}
return (isNaN(data)) ? -1 : +data;
}
} }]
});
I have some situation when all the data don't fit in the page and the user needs to scroll down to see all the info. I've tried to use Fixed Header by adding to my javascript the line fixedHeader: {header: true}
and in the html:
<script src=".1.2/js/dataTables.fixedHeader.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href=".1.2/css/fixedHeader.dataTables.min.css">
But is not working for me :(
What am I doing wrong?
I've been working with dataTables for a short period of time and I have a DataTable with two columns:
TableVehUsage = $("#TableVehUsage ").DataTable({
data: [],
ordering: true,
paginate: false,
"info": false,
fixedHeader: {header: true},
columns: [
{ data: "Vehicle", title: "Vehicle" },
{ data: "Serial", title: "Serial" }
],
"columnDefs": [
{
"targets": 0,
"render": function (data, type, full, meta) {
// If it is rendering the cell contents
if (type === 'display') {
switch (data) {
case "-":
return "-";
default:
if (full.IsOnSale == true)
return '<span style="color:red" onclick="ToParentTab(' + full.Id + ')">' + data + '</span>';
else
return '<span onclick="ToParentTab(' + full.Id + ')">' + data + '</span>';
}
}
return (isNaN(data)) ? -1 : +data;
}
} }]
});
I have some situation when all the data don't fit in the page and the user needs to scroll down to see all the info. I've tried to use Fixed Header by adding to my javascript the line fixedHeader: {header: true}
and in the html:
<script src="https://cdn.datatables/fixedheader/3.1.2/js/dataTables.fixedHeader.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables/fixedheader/3.1.2/css/fixedHeader.dataTables.min.css">
But is not working for me :(
What am I doing wrong?
Share Improve this question asked Jan 21, 2017 at 8:09 LAffairLAffair 1,9986 gold badges33 silver badges64 bronze badges4 Answers
Reset to default 4I found a DataTable plugin that may be of help. Information on it is located at https://datatables/extensions/scroller/
Using this, my definition looks like this:
var table1 = $('#example').DataTable({ paging: true,
scrollY: 200,
deferRender: true,
scroller: true });
I made a jsFiddle at https://jsfiddle/bindrid/oywvh1ek/6/
Try adding your data in a different variable with only the 'value' part of the key-value pair in it. For example, in the case of {"vehicle":"Audi"}, your 'data' variable should have only ["Audi"] in it.
The below code worked perfectly for me.
$("<your_table_name>").DataTable({
data:data,
fixedHeader:true,
"scrollX": true,
"scrollY": "200px",
"scrollCollapse": true,
columns: [
{ title: 'Vehicle' },
{ title: 'Serial' }
]
});
I had a problem where my fixedHeader was working until I got about halfway down the page, then it disappeared. Here is what I did to get it working:
At the top of my JS file:
$(document).ready(function () {
DrawDataTable();
//This enables the table header to stick to the top of the page when the user is scrolling
var employeeData = $('#employee-table').DataTable();
employeeData.fixedHeader.adjust();
});
Then in the same JS file, in my jQuery ajax call, I had to add the line of code that says "fixedHeader": true
Finally, in my HTML file, I had to include the following in my <head>
section:
<link rel="stylesheet" type="text/css" href="https://cdn.datatables/v/dt/dt-1.12.1/fh-3.2.4/r-2.3.0/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables/v/dt/dt-1.12.1/fh-3.2.4/r-2.3.0/datatables.min.js"></script>
Do not forget that you will also need to include whatever Bootstrap, JavaScript or jQuery scripts or style sheets in the head as well.
If you have a navbar it may be covering the fixed header try doing this:
fixedHeader: {
header: true,
headerOffset: $('.app-header').outerHeight() //your header id or class name
},
本文标签: javascriptDataTable fixed header not working for scrollable tableStack Overflow
版权声明:本文标题:javascript - DataTable fixed header not working for scrollable table - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745451003a2658882.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论