Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

Please help me to implement the Fixed table header with MVC 3 application .

I am already using one jQuery script for this, it' working but i am facing table header and body aignment issue.

Does anyone know the best way to implement this without adding any extra css .

NOTE:- Currently i am using jQuery 1.5.1 version for this .

Thank you

What I have tried:

below is the code :-

function scrolify(tblAsJQueryObject, height) {

var oTbl = tblAsJQueryObject;

// for very large tables you can remove the four lines below
// and wrap the table with in the mark-up and assign
// height and overflow property
var oTblDiv = $("");
oTblDiv.css('height', height);
oTblDiv.css('overflow', 'auto');
oTblDiv.css('overflow-x', 'hidden');
oTblDiv.css('margin-right', "2px");

oTbl.wrap(oTblDiv);

// save original width
oTbl.attr("data-item-original-width", oTbl.width());
oTbl.find('thead tr td').each(function () {
$(this).attr("data-item-original-width", $(this).width());
});
oTbl.find('tbody tr td').each(function () {
$(this).attr("data-item-original-width", $(this).width());
});


// clone the original table
var newTbl = oTbl.clone();

// remove table header from original table
oTbl.find('thead tr').remove();
// remove table body from new table
newTbl.find('tbody tr').remove();

oTbl.parent().parent().prepend(newTbl);
newTbl.wrap("");

// replace ORIGINAL COLUMN width
newTbl.width(newTbl.attr('data-item-original-width'));
newTbl.find('thead tr td').each(function () {
$(this).width($(this).attr("data-item-original-width"));
});
oTbl.width(newTbl.attr('data-item-original-width'));
oTbl.find('thead tr td').each(function () {
$(this).width($(this).attr("data-item-original-width"));
});
newTbl.width(newTbl.attr('data-item-original-width'));
newTbl.find('tbody tr td').each(function () {
$(this).width($(this).attr("data-item-original-width"));
});
oTbl.width(oTbl.attr('data-item-original-width'));
oTbl.find('tbody tr td').each(function () {
$(this).width($(this).attr("data-item-original-width"));
});
}

$(document).ready(function () {
scrolify($('#invitedTable'), 200);// 160 is height
scrolify($('#pdartTable'), 200);
});
Posted
Updated 20-Nov-19 4:33am

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900