修复由ajax填充的html表中的标头

时间:2016-10-24 07:37:38

标签: javascript jquery html ajax

我使用ajax调用以下列方式填充html表 -

$.ajax({
    type: "POST",
    url: "my_url",
    data: JSON.stringify(result),
    async: true,
    dataType: "json",
    contentType: "application/json; charset= Shift-JIS",
    success: function (response) {
        glResp = response;
        populateTable(glResp);
    },
    error: function (error) {
        console.log(error);
        //alert("Error!!");
    }
});

我用来填充表的函数如下 - JS -

function populateTable(finalObject) {
    var obj = finalObject;

    var headers1 = ['Name', 'City', 'Job', 'Salary'];
    var table = $("<table id='my-table' />");
    var columns = headers1;
    columns.unshift('');
    var columnCount = columns.length;
    var row = $(table[0].insertRow(-1));

    for (var i = 0; i < columnCount; i++) {
        if (i == 0) {
            var headerCell = $("<th><input type='button' id='sort'></th>");
            row.append(headerCell);
        }
        else {
            var headerCell = $("<th/>");
            headerCell.html([columns[i]]);
            row.append(headerCell);
        }
    }

    $.each(obj, function (i, obj) {
        $row = '<tr><td><input type="checkbox"></td><td>' + obj.Name + '</td><td>' + obj.City + '</td><td>' + obj.Job + '</td><td>' + obj.Salary + '</td></tr>';
        table.append(row);
    });

    var dvTable = $("#dvCSV");
    dvTable.html("");
    dvTable.append(table);
}

现在我希望在滚动数据时将表头固定在顶部。知道如何实现这个吗?

2 个答案:

答案 0 :(得分:3)

您可以使用插件为标题列添加固定位置, http://mkoryak.github.io/floatThead/

希望这个链接对您有所帮助。

答案 1 :(得分:1)

为了修复标题,您可以为标题元素thead添加th,为chlid tr元素添加tbody,为display:block添加overflow:auto } for tbody element。

&#13;
&#13;
var tab  = '[{"Name":"check","City":"check","job":"check","Salary":"check"},{"Name":"check","City":"check","job":"check","Salary":"check"},{"Name":"check","City":"check","job":"check","Salary":"check"},{"Name":"check","City":"check","job":"check","Salary":"check"},{"Name":"check","City":"check","job":"check","Salary":"check"},{"Name":"check","City":"check","job":"check","Salary":"check"}]'
tab = JSON.parse(tab);
populateTable(tab);

function populateTable(finalObject) {
    var obj = finalObject;

    var headers1 = ['Name', 'City', 'Job', 'Salary'];
    var table = $("<table id='my-table' />");
    var columns = headers1;
    columns.unshift('');
    var columnCount = columns.length;
    var th = ""
    
    for (var i = 0; i < columnCount; i++) {
        if (i == 0) {
            var headerCell = "<th><input type='button' id='sort'></th>";
            th = th + headerCell;
        }
        else {
            var headerCell = "<th>"+[columns[i]]+"</th>";
           // headerCell.html();
            th = th + headerCell;
        }
    }
  
    table.prepend("<thead>"+th+"</thead>")
    
    $.each(obj, function (i, obj) {
        row = '<tr><td><input type="checkbox"></td><td>' + obj.Name + '</td><td>' + obj.City + '</td><td>' + obj.job + '</td><td>' + obj.Salary + '</td></tr>';
        table.append(row);
    });

    var dvTable = $("#dvCSV");
    dvTable.html("");
    dvTable.append(table);
}
&#13;
table{
  width:100%;
  }

td, th {
  min-width: 100px;
}
tbody{
  overflow: auto; 
  display: block; 
  height: 100px;
}
thead{
  display:block;
  text-align:left;
  background:green;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dvCSV"><div>
&#13;
&#13;
&#13;