【问题标题】:display javascript array in html table在 html 表中显示 javascript 数组
【发布时间】:2020-01-16 22:42:49
【问题描述】:

我正忙于通过database 的数据发送的HTML 页面。我让数组正常工作并且过滤正确,然后我使用 for 循环在 el 函数中获取对象。控制台日志是正确的,但是我想显示的页面上的表格在一行上写了一个字母。

JAVASCRIPT

function go(){
  var startd = document.getElementById('startdate').value+" 00:00";
  var stopd = document.getElementById('stopdate').value;
  var array = [<%items.forEach(function(item, index){%>{date:"<%= item.Expr1000%>", inout:"<%= item.CHECKTYPE%>", sn:"<%= item.sn%>"},<% });%>{date:"stop"}];
var check = array.filter(function(el){
return el.date >= startd &&
       el.date <= stopd &&
       el.inout &&
       el.sn;

});

check.forEach(function(el){
console.log(el.date);
var table = $('#set');
 var row, cell;
 for(var i=0; i<el.date.length; i++){
 row = $( '<tr />' );
 table.append( row );
 for(var j=0; j<el.date[i].length; j++){
 cell = $('<td>'+el.date[i][j]+'</td>');
 row.append( cell );
 }
 }
});



}

目前显示的日期如:

2
0
1
9
-
0
8
-
1
5

0
7
:
0
0
:
2
1
2
0
1
9
-
0
8
-
1
5

0
7
:
0
0
:
3
0

我想在哪里显示为:

 yyyy-mm-dd hh:mm:ss
 yyyy-mm-dd hh:mm:ss

【问题讨论】:

    标签: javascript html arrays html-table


    【解决方案1】:

    您的第二个循环是问题所在:

    for(var j=0; j<el.date[i].length; j++){
      cell = $('<td>'+el.date[i][j]+'</td>');
      row.append( cell );
    }
    

    循环遍历日期中的每个字符。您根本不需要第二个循环——只需将日期直接添加到row

    cell = $('<td>'+el.date[i]+'</td>');
    row.append( cell );
    

    【讨论】:

    • 谢谢你。它现在正在工作,但表格正在复制数组对象并显示每个项目 33 次?
    • 如果您更新您的问题,并举例说明数据的实际外观,我可能会为您提供更好的帮助。
    猜你喜欢
    • 2016-12-11
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 2021-09-26
    • 2019-10-07
    • 2022-07-26
    • 2021-02-19
    • 2017-11-11
    相关资源
    最近更新 更多