如何使用HTML / CSS创建第一列固定的表?

时间:2017-02-02 07:17:55

标签: html css html-table css-tables

我想创建一个可以水平滚动的表格。列数是动态的。我想在水平滚动表格时实现第一列需要修复/冻结。我试过以下css。第一列固定不错,但第二列隐藏在固定列下。

   .mydiv {
      overflow-y: scroll;
   }.headcol {
    position:absolute;
    width: 100px;
    text-align:center;
    background-color: #4CAF50;
    color: white;
    font-family:arial;
    font-size:13px;
    padding-left:15px;
    padding-right:15px;
    padding-top:10px;
    padding-bottom:10px;
    border: 1px solid #c4c0c0;
}

.tablemy {
    border-collapse: collapse;
    border: 1px solid #c4c0c0;
}

.thmy {
    text-align:center;
    background-color: #4CAF50;
    color: white;
    font-family:arial;
    font-size:13px;
    padding-left:15px;
    padding-right:15px;
    padding-top:10px;
    padding-bottom:10px;
    border: 1px solid #c4c0c0;
}

.tdmy {
    white-space: nowrap;
    padding:8px; border-left:1px solid #ccc; border-top:1px solid #ccc;
    padding-left:15px;
    padding-right:15px;
    padding-top:10px;
    padding-bottom:10px;
    font-size:12px;
    font-family:arial;
    border: 1px solid #c4c0c0;
    color:#585858;
}
.trmy:nth-child(odd) {
    background-color:#F5F5F5;
}
tr:nth-child(even) {
    background-color:#ffffff;
}

我的桌子是 enter image description here

我正在用javascript创建我的表。

  $(window).load(function(){
var customers = new Array();
customers.push(["TN04 AH 0253", "55"]);
customers.push(["TN04 AF 8830", "67"]);
customers.push(["TN37 CM 1249", "33"]);
customers.push(["TN19 F 9175", "47"]);
customers.push(["TN37CM 1932", "0"]);

var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

var dateArray = getDates(firstDay, lastDay);

// Create a HTML Table element.
var table = document.createElement("TABLE");
table.setAttribute('class', 'tablemy');
table.border = "1";

// Get the count of columns.
var columnCount = customers[0].length;

var row = table.insertRow(-1);
for (var i = 0; i < dateArray.length; i++) {
    var headerCell = document.createElement("TH");
    if (i === 0) {
        headerCell.setAttribute('class', 'headcol');
    } else {
        headerCell.setAttribute('class', 'thmy');
    }
    //headerCell.setAttribute('class', 'thmy');
    headerCell.innerHTML = dateArray[i];
    row.appendChild(headerCell);
}

// Add the data rows.
for (var i = 0; i < customers.length; i++) {
    row = table.insertRow(-1);
    row.setAttribute('class', 'trmy');
    for (var j = 0; j < columnCount; j++) {
        var cell = row.insertCell(-1);
        if (j === 0) {
            cell.setAttribute('class', 'headcol');
        } else {
            cell.setAttribute('class', 'tdmy');
        }
        //cell.setAttribute('class', 'tdmy');
        cell.innerHTML = customers[i][j];
    }
}

var dvTable = document.getElementById("dvTable");
dvTable.innerHTML = "";
dvTable.appendChild(table);
})
function getDates(date1, date2) {
var day = 1000 * 60 * 60 * 24;
var diff = (date2.getTime() - date1.getTime())/day;
var dateArray = new Array();
dateArray.push("Unit/Count");
for(var i=0; i<=diff; i++)
{
    var xx = date1.getTime() + day * i;
    var yy = new Date(xx);

    var strDate = yy.getDate() + "/" + (yy.getMonth() + 1);
    dateArray.push(strDate);
}
return dateArray;
}

1 个答案:

答案 0 :(得分:0)

我认为您需要为第一列设置position: fixed并使所有其他列可见,您需要为padding-left: 130px;设置.tablemy(等于第一列的宽度):

function getDates(date1, date2) {
var day = 1000 * 60 * 60 * 24;
var diff = (date2.getTime() - date1.getTime())/day;
var dateArray = new Array();
dateArray.push("Unit/Count");
for(var i=0; i<=diff; i++)
{
    var xx = date1.getTime() + day * i;
    var yy = new Date(xx);

    var strDate = yy.getDate() + "/" + (yy.getMonth() + 1);
    dateArray.push(strDate);
}
return dateArray;
}

var customers = new Array();
customers.push(["TN04 AH 0253", "55"]);
customers.push(["TN04 AF 8830", "67"]);
customers.push(["TN37 CM 1249", "33"]);
customers.push(["TN19 F 9175", "47"]);
customers.push(["TN37CM 1932", "0"]);

var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

var dateArray = getDates(firstDay, lastDay);

// Create a HTML Table element.
var table = document.createElement("TABLE");
table.setAttribute('class', 'tablemy');
table.border = "1";

// Get the count of columns.
var columnCount = customers[0].length;

var row = table.insertRow(-1);
for (var i = 0; i < dateArray.length; i++) {
    var headerCell = document.createElement("TH");
    if (i === 0) {
        headerCell.setAttribute('class', 'headcol');
    } else {
        headerCell.setAttribute('class', 'thmy');
    }
    //headerCell.setAttribute('class', 'thmy');
    headerCell.innerHTML = dateArray[i];
    row.appendChild(headerCell);
}

// Add the data rows.
for (var i = 0; i < customers.length; i++) {
    row = table.insertRow(-1);
    row.setAttribute('class', 'trmy');
    for (var j = 0; j < columnCount; j++) {
        var cell = row.insertCell(-1);
        if (j === 0) {
            cell.setAttribute('class', 'headcol');
        } else {
            cell.setAttribute('class', 'tdmy');
        }
        //cell.setAttribute('class', 'tdmy');
        cell.innerHTML = customers[i][j];
    }
}

var dvTable = document.getElementById("dvTable");
dvTable.innerHTML = "";
dvTable.appendChild(table);
html, body {
  margin: 0;
  padding: 0;
}
.mydiv {
      overflow-y: scroll;
   }
  .headcol {
    position: fixed;
    left: 0;
    width: 100px;
    text-align:center;
    background-color: #4CAF50;
    color: white;
    font-family:arial;
    font-size:13px;
    padding-left:15px;
    padding-right:15px;
    padding-top:10px;
    padding-bottom:10px;
    border: 1px solid #c4c0c0;
}

.tablemy {
    border-collapse: collapse;
    border: 1px solid #c4c0c0;
    margin-left: 130px;
}

.thmy {
    text-align:center;
    background-color: #4CAF50;
    color: white;
    font-family:arial;
    font-size:13px;
    padding-left:15px;
    padding-right:15px;
    padding-top:10px;
    padding-bottom:10px;
    border: 1px solid #c4c0c0;
}

.tdmy {
    white-space: nowrap;
    padding:8px; border-left:1px solid #ccc; border-top:1px solid #ccc;
    padding-left:15px;
    padding-right:15px;
    padding-top:10px;
    padding-bottom:10px;
    font-size:12px;
    font-family:arial;
    border: 1px solid #c4c0c0;
    color:#585858;
}
.trmy:nth-child(odd) {
    background-color:#F5F5F5;
}
tr:nth-child(even) {
    background-color:#ffffff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="dvTable"></table>