gpt4 book ai didi

javascript - 如何将其转换为DataTable?

转载 作者:行者123 更新时间:2023-12-02 22:03:50 29 4
gpt4 key购买 nike

我想使用 DataTable 一次显示 5 行类型的布局所以,请帮我解决这个问题。我尝试了很多事情,但我无法做到。

<!DOCTYPE html>
<html>
<head>
<title>CSV File to HTML Table Using AJAX jQuery</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css"></style>
<script type="text/javascript" src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>

</head>
<body>
<div class="container">
<div class="table-responsive">
<h1 align="center">CSV File to HTML Table Using AJAX jQuery</h1>
<br />
<div align="center">
<button type="button" name="load_data" id="load_data" class="btn btn-info">Load Data</button>
</div>
<br />
<div id="employee_table">
</div>
</div>
</div>
</body>
</html>

<script>


$(document).ready(function(){
$('#load_data').click(function(){
$.ajax({
url:"iguana.csv",
dataType:"text",
success:function(data)
{
var employee_data = data.split(/\r/);
var table_data = '<table class="table table-bordered table-sm table-responsive table-striped table-hover ">';
for(var count = 0; count<employee_data.length; count++)
{
var cell_data = employee_data[count].split(",");
table_data += '<tr>';
for(var cell_count=0; cell_count<cell_data.length; cell_count++)
{
if(count === 0)
{
table_data += '<th>'+cell_data[cell_count]+'</th>';
}
else
{
table_data += '<td>'+cell_data[cell_count]+'</td>';
}
}
table_data += '</tr>';
}
table_data += '</table>';
$('#employee_table').html(table_data);
},
complete: function (data) {
$('#employee_table').DataTable();
alert("AJAX request successfully completed");
}
});
});

});


</script>

我想使用 DataTable 一次显示 5 行类型的布局所以,请帮我解决这个问题。我尝试了很多事情但我无法做到。有什么办法可以实现这个目标吗?

最佳答案

您正在尝试将 div 转换为数据表。

在表标签中添加一个类,该类将代表您的表。让我们在表标记中添加 myDataTable

var table_data = '<table class="myDataTable table table-bordered table-sm table-responsive table-striped table-hover">';

现在,通过替换代码将表转换为数据表

 $('#employee_table').DataTable(); // employee_table is actually an id of div

$('.myDataTable').DataTable(); // class that added in table tag

关于javascript - 如何将其转换为DataTable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59785115/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com