[英]Displaying a javascript variable array
I have a variable array that results in a table filtering depending on the user input. 我有一个变量数组,该数组根据用户输入进行表过滤。 I get the array result in a
console.log
now I want to display it in HTML. 我现在想在HTML中显示数组结果到
console.log
中。
I tried : document.getElementById("Tableau").innerHTML = res.toString();
我尝试过:
document.getElementById("Tableau").innerHTML = res.toString();
where "res" is the array result, but that only shows elements separated with commas when I want to have a somewhat well presented simple HTML table. 其中“ res”是数组结果,但是当我想要一个表现得很好的简单HTML表时,它仅显示用逗号分隔的元素。
var res = station.filter( i => i[0] >= gval1 && i[0] <= gval2 );
document.getElementById("Tableau").innerHTML = res.toString();
console.log(res);
This is the HTML table : 这是HTML表格:
<table>
<thead>
<tr>
<th> Id </th>
<th> Station </th>
<th> Line </th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
This is my javascript table : 这是我的javascript表格:
var station = [
[0,'JAMAA EL FNA','L1'],
[1,'KOUTOUBIA','L1'],
[2,'HOTEL DE VILLE','L1'],
[3,'R.P BERDII','L1'],
[4,'GRAND POSTE','L1'],
[5,'CAREE EDEN','L1'],
[6,'PL ABDELMOUMEN','L1'],
[7,'PLACE D ARMES','L1'],
[8,'FST','L1'],
[9,'SEMIRAMIS','L1'],
[10,'DR KUDIA','L1'],
[11,'MCDO','L1'],
[12,'CAFE AMINE','L1'],
[13,'FAC SEMLALIA','L1'],
[14,'ROUIDATE','L1'],
[15,'CLUB MINISTRE JUSTICE','L1'],
[16,'BEN TBIB','L1'],
[17,'ASWAK SALAM','L1'],
[18,'BAB DOUKALA','L1'],
[19,'JAMAA EL FNA','L2'],
[20,'KOUTOUBIA','L2'],
[21,'PH KOUTOUBIA','L2'],
[22,'RIAD SHEBA','L2'],
[23,'DAR LBACHA','L2'],
[24,'RIAD LAAROUSSE','L2'],
[25,'BAB TAGHZOUT','L2'],
[26,'BIN LMAASAR','L2'],
[27,'ARSET EL MELLAK','L2'],
[28,'HOPITAL ANTAKI','L2'],
[29,'AVENUE ANTAKI','L2'],
[30,'QCHICH','L2'],
[31,'RUE BAB KHACHICH','L2'],
[32,'AIN ITTI','L2']];
How can I loop through the array to display it on the table? 如何遍历数组以将其显示在表上?
use .join() to construct your html
and put the array elements in td
tags : 使用.join()构造您的
html
并将数组元素放在td
标签中:
const res = ['a', 'b', 'c']; const cells = '<tr><td>' + res.join('</td><td>') + '</td></tr>' ; document.getElementById("Tableau").innerHTML = cells;
td{ border: 1px solid; padding: 5px; }
<table id="Tableau"> </table>
EDIT : based on the edited question, you'll need a loop to go through the array eleemnts and wrap each line in a <tr>
and append the result to the <tbody>
, use .map() for this : 编辑:根据已编辑的问题,您将需要循环遍历数组元素并将每行包装在
<tr>
并将结果附加到<tbody>
,为此请使用.map() :
var station = [ [0, 'JAMAA EL FNA', 'L1'], [1, 'KOUTOUBIA', 'L1'], [2, 'HOTEL DE VILLE', 'L1'], [3, 'RP BERDII', 'L1'] ] var rows = station.map(e => { return '<tr><td>' + e.join('</td><td>') + '</td></tr>'; }) document.getElementById("tBody").innerHTML = rows.join(' ');
th, td { border: 1px solid; padding: 5px; }
<table id="Tableau"> <thead> <tr> <th> Id </th> <th> Station </th> <th> Line </th> </tr> </thead> <tbody id="tBody"> </tbody> </table>
Since you have a list and you need to make the delimiter table like, I would suggest inserting a <br />
tag between table items. 由于您有一个列表,并且需要使定界符表像这样,因此建议在表项之间插入一个
<br />
标记。 Like so: 像这样:
Tableau.innerHTML = res.join('<br />')
As Taki mentioned you can use res.join()
to covert array into a string. 正如Taki提到的,您可以使用
res.join()
将数组res.join()
转换为字符串。
Speaking about styling it, may be the following would help. 谈到样式,以下内容可能会有所帮助。
Assuming you have an array as follows, 假设您有一个如下数组,
var statements=['1st statement','2nd statement','3rd statement','4th statement','5th statement'];
console.log(statements.join('-'));
would yield the following 将产生以下
1st statement-2nd statement-3rd statement-4th statement-5th statement
and say you do the following 说你要做以下
var styledRes ='<tr class="your-class-name"><td>' + statements.join('</td><td>')+' </td></tr>'
would yield 会屈服
<tr class="your-class-name">
<td>1st statement</td>
<td>2nd statement</td>
<td>3rd statement</td>
<td>4th statement</td>
<td>5th statement</td>
</tr>
And you can do your style definitions in your css file and they will get applied here. 您可以在css文件中进行样式定义,它们将在此处应用。
When you do 当你做
document.getElementById("Tableau").innerHTML = styledRes;
you would see something like the following on screen 您会在屏幕上看到类似以下的内容
<table>
<tr class="your-class-name">
<td>1st statement</td>
<td>2nd statement</td>
<td>3rd statement</td>
<td>4th statement</td>
<td>5th statement</td>
</tr>
</table>
And assume you have styles defined as follows, 并假设您定义了以下样式,
.your-class-name td{
border:2px solid green; 边框:2px纯绿色; }
}
you would be able to see something like the following on screen 您将可以在屏幕上看到类似以下内容的信息
.your-class-name td{ border:2px solid green; }
<table> <tr class="your-class-name"> <td>1st statement</td> <td>2nd statement</td> <td>3rd statement</td> <td>4th statement</td> <td>5th statement</td> </tr> </table>
Is this what you are looking for? 这是你想要的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.