Hi @RAVI,
I want to fix header while scrolling
You can try using the CSS below.
table {
margin: 0 auto;
margin-top: 20px;
width: 100%;
position: relative;
overflow: auto;
}
th, thead {
position: sticky;
top: 0;
border: 1px solid #dddddd;
background-color: #1f2d54;
text-align: center;
table-layout: fixed;
word-break: break-word;
height: 45px;
}
On button click the data should export to excel with perfect alignment
Just add a button to your current code.
<asp:Button ID="btnExport" Text="Export" runat="server" OnClientClick="return Export();" />
All Code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.13.7/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.7/css/jquery.dataTables.min.css" />
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var rowCount = $('table#list tbody tr').length;
sessionStorage.setItem("rowCount", rowCount);
// Remove the formatting to get integer data for summation
var intVal = function (i) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '') * 1 :
typeof i === 'number' ?
i : 0;
};
$('#list').DataTable({
"ordering": false,
lengthMenu: [
[2000, 1500, 1000, 500, -1],
[2000, 1500, 1000, 500, 'All'],
],
initComplete: function () {
this.api().columns([2]).every(function () {
var title = this.header();
//replace spaces with dashes
title = $(title).html().replace(/[\W]/g, '-');
var column = this;
var select = $('<select id="' + title + '" class="select2" ></select>')
.appendTo($(column.header()).empty())
.on('change', function () {
//Get the "text" property from each selected data
//regex escape the value and store in array
var data = $.map($(this).select2('data'), function (value, key) {
return value.text ? '^' + $.fn.dataTable.util.escapeRegex(value.text) + '$' : null;
});
//if no data selected use ""
if (data.length === 0) {
data = [""];
}
//join array into string with regex or (|)
var val = data.join('|');
//search for the option(s) selected
column
.search(val ? val : '', true, false)
.draw();
});
column.data().unique().sort().each(function (d, j) {
select.append('<option value="' + d + '">' + d + '</option>');
});
//use column title as selector and placeholder
$('#' + title).select2({
multiple: true,
closeOnSelect: false,
width: '100%',
placeholder: "" + title
});
//initially clear select otherwise first option is selected
$('.select2').val(null).trigger('change');
});
},
footerCallback: function (tfoot, data, start, end, display) {
let api = this.api();
api.column(0).footer().innerHTML = "GRAND TOTAL";
// Total over all pages
total = api
.column(2, { search: 'applied' })
.data()
.reduce((a, b) => intVal(a) + intVal(b), 0);
// Update footer
api.column(2).footer().innerHTML = total;
}
});
});
</script>
<style>
.select2-results__options[aria-multiselectable="true"] li {
padding-left: 30px;
position: relative;
}
.select2-results__options[aria-multiselectable="true"] li:before {
position: absolute;
left: 8px;
opacity: .6;
top: 6px;
font-family: "FontAwesome";
content: "\f0c8";
}
.select2-results__options[aria-multiselectable="true"] li[aria-selected="true"]:before {
content: "\f14a";
}
table {
margin: 0 auto;
margin-top: 20px;
width: 100%;
position: relative;
overflow: auto;
}
th, thead {
position: sticky;
top: 0;
border: 1px solid #dddddd;
background-color: #1f2d54;
text-align: center;
table-layout: fixed;
word-break: break-word;
height: 45px;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/jquery-table2excel@1.1.1/dist/jquery.table2excel.min.js"></script>
<script type="text/javascript">
function Export() {
$("[id*=list]").table2excel({
filename: "LEATHER_STOCK.xls"
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnExport" Text="Export" runat="server" OnClientClick="return Export();" />
<div class="container">
<br />
<table cellspacing="0" class="myClass" id="list" style="width: 400px; font-family: Calibri; font-size: 11px; border-collapse: collapse; border: 1px solid black;">
<thead>
<tr>
<th style="width: 150px;">FIELD 1 </th>
<th style="width: 150px;">FIELD 2 </th>
<th style="width: 150px;">FIELD 3 </th>
<th style="width: 150px;">FIELD 4 </th>
</tr>
</thead>
<tbody style="border-collapse: collapse; border: 1px solid black;">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td style="border-collapse: collapse; border: 1px solid black; font-size: larger;"><%#Eval("FIELD1")%></td>
<td style="border-collapse: collapse; border: 1px solid black; font-size: larger;"><%#Eval("FIELD2")%></td>
<td style="border-collapse: collapse; border: 1px solid black; font-size: larger;"><%#Eval("FIELD3")%></td>
<td style="border-collapse: collapse; border: 1px solid black; font-size: larger;"><%#Eval("FIELD4")%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
<tfoot>
<tr>
<td style="border-collapse: collapse; background: sandybrown; border: 1px solid black; font-size: larger; font-weight: bold;"></td>
<td style="border-collapse: collapse; background: sandybrown; border: 1px solid black; font-size: larger; font-weight: bold;"></td>
<td style="border-collapse: collapse; background: sandybrown; border: 1px solid black; font-size: larger; font-weight: bold;"></td>
</tr>
</tfoot>
</table>
</div>
</div>
</form>
</body>
</html>
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.