javascript - 如何在作为列表推送的 HTML 页面上构建表格?
问题描述
我正在尝试将.csv
页面上的内容显示为表格。该列表包括取决于用户的未知行和列。它不是像 2x2 或 3x4 这样的固定类型。但我得到了类似以下的东西;
[
[
&
#
x
2
7
;
x
x
.......
我正在重定向一个列表,也尝试过json
。列表的内容不固定。长度和柱边是可靠的。我正在尝试正确传递数据并显示为表格返回列表;
return render(request, 'yuklenen.html', {'veriler': yuklenen, 'file': listx })
我想将其显示为
<div id="contain"></div>
这是代码:
<script>
var str = '<ul>';
var data1 = "{{file}}" ;
for(var x in at){
str+='<li>' + at[x] + '<li>';
}
str += '</ul>';
document.getElementById("contain").innerHTML = str;
</script>
解决方案
希望这会让你开始:
function rand(min, max)
{
return Math.round(Math.random() * (max - min) + min);
}
function update()
{
const at = [];
for(let x = 0; x < rand(3, 10); x++)
{
const c = [];
for(let y = 0; y < rand(1, 10); y++)
{
c[y] = rand(0, 100);
}
at[x] = c;
}
var str = '<ul>';
for(var x in at){
str+='<li>';
for(var y in at[x])
str += "<span>" + at[x][y] + "</span>";
str+='</li>';
}
str += '</ul>';
document.getElementById("contain").innerHTML = str;
}
update();
ul
{
display: table;
border-collapse: collapse;
}
li
{
display: table-row;
}
li > span
{
border: 1px solid black;
vertical-align: middle;
text-align: center;
display: table-cell;
width: 2em;
height: 2em;
}
<button onclick="update()">update</button>
<div id="contain"></div>
推荐阅读
- rust - 如何创建具有两个 condvar 的互斥锁?
- lua - 在 Lua 中,为什么一个函数或表达式会在一次更新调用中计算多次?
- javascript - 根据 api 响应对不同内容进行 React 渲染
- java - ArrayList 作为构造函数中的参数
- python - 增加 qtreewidget 的大小
- asp.net-core - .NET Core 中的程序集绑定重定向
- r - 如何将 3 个不同表中的 3 个不同列合并为一个大列。R
- flutter - 如何在 Flutter Web 中的 Blob 和 Image 之间来回转换?
- javascript - 如何在 Jest 中仅模拟 Class 的构造函数
- sql - 总和返回正确答案......有时