I am trying to fix the header and columns and to change their background color of Table by using Jquery. i have created table in jquery from some Json data.
I have tried 'translate()' function to freeze.
var translate = "translate(5px,0)";
"This helps me to fix column or header but the main problem is that, when i scrolled horizontal first and then vertical , the first <th/> of header getting scrolled and rest of headers are staying fixed.(see output image as a mention question)"
Let me show you the code:
HTML:
This is my HTML , where i am calling 'MyJS' from <script/>.
<div class="row" >
<div class="tblMain" id="tblMainDiv">
</div>
</div>
<script>
$("#tblMainDiv").MyJS({
Data: data.Data,
ColoredColumns:["Sarika"]
});
<script/>
JS:
'_params' is the parameters and '_params.Data' is the Json data of table. '_myTableTr ' is the tr of table.
var _collection = JSON.parse(_params.Data);
var _myTableColumns = [];
var _collection = JSON.parse(_params.Data); // Parsing the Json Data.
for (var i in _collection[0]) {
_myTableColumns.push(i); // grabing column headers and pushing into collection.
}
// Adding table heads
if (_myTableColumns.length > 0) {
for (var i = 0; i < _myTableColumns.length; i++) {
var _myTableTh = document.createElement("th");
_myTableTh.innerHTML = _myTableColumns[i];
_headerTr.appendChild(_myTableTh);
}
}
// adding table data.
for (var i = 0; i < _collection.length; i++) {
_myTableTr = _myDatatable.insertRow(-1);
for (var j = 0; j < _myTableColumns.length; j++) {
var cell = _myTableTr.insertCell(-1);
cell.innerHTML = _collection[i][_myTableColumns[j]];
}
}
And here is the code for Freeze:
Here i am trying to fix only one first column ,but in future i want to fix multiple random columns .
//For Header and Specific columns to be fixed .
var lastPosY = 0;
var lastPosX = 0;
$("#"+$(this).attr('id')).scroll(function () {
var currPosX = this.scrollLeft; // get current position of Column.
var currPosY = this.scrollTop;
if (lastPosY != currPosY && (lastPosY > 0 || currPosY>0)) {
$('#tblMyDataTable' + $(this).attr('id') + ' thead th:first-child').removeAttr('style');
var translate = "translate(0," + (this.scrollTop) + "px)"; // getting translate pixels on the basis of current position of Header bar.
$($('#tblMyDataTable' + $(this).attr('id')).find("thead").eq(0)).css('transform', translate);
}
else {
translate = "translate(" + (this.scrollLeft) + "px,0)"; // getting translate pixels on the basis of current position of Column.
if (lastPosX != currPosX) {
$('#tblMyDataTable' + $(this).attr('id') + ' thead th:first-child').css('transform', translate);
$('#tblMyDataTable' + $(this).attr('id') + ' tbody tr td:first-child').css('transform', translate);
}
}
lastPosX = currPosX;
lastPosY = currPosY;
});
CSS:
.myDataTable {
width: 100%;
overflow-x: scroll;
border-collapse: separate !important;
border: 1px solid #000;}
.tblMain {
margin:20px;}
.myDataTable > tbody > tr:nth-child(even)>td {
background-color: #e6e6e6;}
.myDataTable > tbody > tr:nth-child(odd) > td {
background-color: #fff;}
.tblMain {
height: 50vh;
overflow-y: auto;}
Please help me to fix it and change the background color of fixed ones. Thank you.
