如何使用来自 Map 的数据在 node.js 中构建动态 HTML 表格

huangapple go评论68阅读模式
英文:

How to build dynamic html table using data from Map for node.js

问题

I have a Map which had key and values, and I just want to store the map key and values in the html table by using the below code snippet.

Code:

let counter = 0;

let htmlTable =  "<table class='table table-hover'>" +
                 "<thead>" +
                 "<tr>" +
                 "<th scope='col'>#</th>" +
                 "<th scope='col'>Network Call</th>" +
                 "<th scope='col'>Duration</th>" +
                 "</tr>" +
                 "</thead>" +
                 "<tbody>" +
                 "<tr>" +
                 myMap.forEach(function(value, key) {
                    let array = key.split(":");
                    "<th scope='row'>" + `${counter++}` + "</th>" +
                    "<td>" + `${array[0]}` + "</td>" +
                    "<td>" + `${array[1]}` + "</td>" +
                    ""
                 }) +
                 "</tr>" +
                 "</tbody>" +
                 "</table>";

Map data:

myMap.forEach (function(value, key) {
        console.log("MAP" + key + ' = ' + value);
      })

Output:

[0-0] MAP /token?re=prime&amp;deviceId=:login = 555
[0-0] MAP /session-context/v1/bootstrap:login = 132
[0-0] MAP /configs/auth:login = 106

I just want to print the map data inside the <tr> row in the above code. But it returned 'undefined'. When I tried to iterate the above map and it had values, but It's not iterated when it's called inside the HTML let variable.

I'm new to JavaScript, is there any other way to make this work?

英文:

I have a Map which had key and values, and I just want to store the map key and values in the html table by using the below code snippet.

Code:

 let counter = 0;

 let htmlTable =  &quot;&lt;table class=&#39;table table-hover&#39;&gt;&quot;+
                             &quot;&lt;thead&gt;&quot;+
                             &quot;&lt;tr&gt;&quot;+
                             &quot;&lt;th scope=&#39;col&#39;&gt;#&lt;/th&gt;&quot;+
                             &quot;&lt;th scope=&#39;col&#39;&gt;Network Call&lt;/th&gt;&quot;+
                             &quot;&lt;th scope=&#39;col&#39;&gt;Duration&lt;/th&gt;&quot;+
                             &quot;&lt;/tr&gt;&quot;+
                             &quot;&lt;/thead&gt;&quot;+
                             &quot;&lt;tbody&gt;&quot;+
                             &quot;&lt;tr&gt;&quot;+
                             myMap.forEach(function(value, key) {
                                let array = key.split(&quot;:&quot;);
                                &quot;&lt;th scope=&#39;row&#39;&gt;&quot;+`${counter++}`+&quot;&lt;/th&gt;&quot;+
                                &quot;&lt;td&gt;&quot;+`${array[0]}`+&quot;&lt;/td&gt;&quot;+
                                &quot;&lt;td&gt;&quot;+`${array[1]}`+&quot;&lt;/td&gt;&quot;+
                                &quot;&quot;
                             })+
                             &quot;&lt;/tr&gt;&quot;+
                             &quot;&lt;/tbody&gt;&quot;+
                             &quot;&lt;/table&gt;&quot;

Map data:

myMap.forEach (function(value, key) {
        console.log( &quot;MAP&quot;+ key + &#39; = &#39; + value);
      })

Output:

[0-0] MAP /token?re=prime&amp;deviceId=:login = 555
[0-0] MAP /session-context/v1/bootstrap:login = 132
[0-0] MAP /configs/auth:login = 106

I just want to print the map data inside the <tr> row in the above code. But it returned 'undefined'. When I tried to iterate the above map and it had values, but Its not iterated when its called inside the html let variable.

I'm new to the .js and Is there any other way to call this or make it work ?

答案1

得分: 1

以下是您要翻译的内容:

我相信这是您正在寻找的内容:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
     const myMap = ['1:1','2:2']

     let htmlTable =  `<table class='table table-hover'>
                                 <thead>
                                 <tr>
                                 <th scope='col'>#</th>
                                 <th scope='col'>Network Call</th>
                                 <th scope='col'>Duration</th>
                                 </tr>
                                 </thead>
                                 <tbody>
                                 <tr>
                                 ${(myMap.map((value, key) => {
                                    const array = value.split(":");
                                    const newKey = key+1
                                    return `<th scope='row'>${newKey}</th>
                                    <td>${array[0]}</td>
                                    <td>${array[1]}</td>`
                                 })).join('')}
                                 </tr>
                                 </tbody>
                                 </table>`
    console.log(htmlTable)

<!-- end snippet -->

使用map函数返回一个字符串数组,join函数将移除连接字符串后出现的逗号。

英文:

I believe this is what you are looking for:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

 const myMap = [&#39;1:1&#39;,&#39;2:2&#39;]

 let htmlTable =  `&lt;table class=&#39;table table-hover&#39;&gt;
                             &lt;thead&gt;
                             &lt;tr&gt;
                             &lt;th scope=&#39;col&#39;&gt;#&lt;/th&gt;
                             &lt;th scope=&#39;col&#39;&gt;Network Call&lt;/th&gt;
                             &lt;th scope=&#39;col&#39;&gt;Duration&lt;/th&gt;
                             &lt;/tr&gt;
                             &lt;/thead&gt;
                             &lt;tbody&gt;
                             &lt;tr&gt;
                             ${(myMap.map((value, key) =&gt; {
                                const array = value.split(&quot;:&quot;);
                                const newKey = key+1
                                return `&lt;th scope=&#39;row&#39;&gt;${newKey}&lt;/th&gt;
                                &lt;td&gt;${array[0]}&lt;/td&gt;
                                &lt;td&gt;${array[1]}&lt;/td&gt;`
                             })).join(&#39;&#39;)}
                             &lt;/tr&gt;
                             &lt;/tbody&gt;
                             &lt;/table&gt;`
console.log(htmlTable)

<!-- end snippet -->

with map you return an array of strings and join will remove the commas that would appear after concatenating the strings.

答案2

得分: 0

这可能适用于您,我尚未测试,但这就是想法。您将第一个静态部分的HTML代码分配给htmlTable,然后将HTML代码与连接的值动态分配,然后是最终的静态部分。
我猜key的值是类似于&quot;FOO:BAR&quot;的字符串。
希望对您有所帮助!

let counter = 0;

let htmlTable = "<table class='table table-hover'>" +
                    "<thead>" +
                        "<tr>" +
                            "<th scope='col'>#</th>" +
                            "<th scope='col'>网络调用</th>" +
                            "<th scope='col'>持续时间</th>" +
                        "</tr>" +
                    "</thead>" +
                    "<tbody>" +
                        "<tr>";

myMap.forEach(function(value, key) => {
  let array = key.split(":");
  htmlTable += 
    "<th scope='row'>"+(counter++) +"</th>"+
    "<td>"+array[0]+"</td>"+
    "<td>"+array[1]+"</td>";
})
htmlTable += "</tr>"+
               "</tbody>"+
               "</table>";
英文:

This might work for you, I have not tested it but that is the idea. You give assign the first static part of the html code to htmlTable, then you dinamically assign the html code with the concatenated values and then final static part.
I am guessing that the key value is a string that goes something like this &quot;FOO:BAR&quot;.
Hope it helps!

let counter = 0;
    
let htmlTable =  &quot;&lt;table class=&#39;table table-hover&#39;&gt;&quot;+
                    &quot;&lt;thead&gt;&quot;+
                    &quot;&lt;tr&gt;&quot;+
                    &quot;&lt;th scope=&#39;col&#39;&gt;#&lt;/th&gt;&quot;+
                    &quot;&lt;th scope=&#39;col&#39;&gt;Network Call&lt;/th&gt;&quot;+
                    &quot;&lt;th scope=&#39;col&#39;&gt;Duration&lt;/th&gt;&quot;+
                    &quot;&lt;/tr&gt;&quot;+
                    &quot;&lt;/thead&gt;&quot;+
                    &quot;&lt;tbody&gt;&quot;+
                    &quot;&lt;tr&gt;&quot;
myMap.forEach(function(value, key) =&gt; {
  let array = key.split(&quot;:&quot;);
  htmlTable += 
    &quot;&lt;th scope=&#39;row&#39;&gt;&quot;+(counter++) +&quot;&lt;/th&gt;&quot;+
    &quot;&lt;td&gt;&quot;+array[0]+&quot;&lt;/td&gt;&quot;+
    &quot;&lt;td&gt;&quot;+array[1]+&quot;&lt;/td&gt;&quot;
})
htmlTable += &quot;&lt;/tr&gt;&quot;+
               &quot;&lt;/tbody&gt;&quot;+
               &quot;&lt;/table&gt;&quot;

huangapple
  • 本文由 发表于 2023年4月20日 02:26:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定