简体   繁体   English

在表格单元格中每行显示JSON数组结果

[英]Displaying JSON array results one per line in a table cell

I have a table which displays the column "air segments". 我有一个表,其中显示“空气分段”列。 The air segments come as a JSON array from the server(see picture), I need to display the contents of the air segments column one per line in a table cell. 空段是来自服务器的JSON数组(请参见图片),我需要在表格单元格中每行显示空段列的内容。 Right now its getting displayed as a complete string with a comma(See picture below). 现在,它显示为带有逗号的完整字符串(请参见下图)。

JSON文件 .

So the HTML code is as follows: 因此,HTML代码如下:

<!-- ko foreach: $parents[0].outputsToDisplay($data) -->
    <td>
        <span data-bind="foreach: $data, visible: $parents[2].outputs()[$parent.index()].internalname ==='air_segments', text: splitAirSegments($data)"><br></span>
   </td>
<!-- /ko -->

The outer 'for each' supplies the $data to the td, which are the results to be populated in the table. 外部的“ for each”将$ data提供给td,这些数据将被填充到表中。

The visible binding property makes sure that only column air_segments gets populated. 可见的绑定属性可确保仅填充air_segments列。

The outputs() is an observable array in the js file. output()是js文件中的一个可观察数组。

I need to code a JS method "splitAirSegments" such that it returns the air segments one by one and with help of 'br' I break the line which would eventually make the air segments appear one per line in a single table cell. 我需要编写一个JS方法“ splitAirSegments”,使其一一返回空段,并在“ br”的帮助下中断行,最终使空段在单个表单元格中每行出现一个。

What I tried: 我试过的

self.splitAirSegments = function (arr) {
    return arr;
}

The way I understood it was, the 'for each' from the 'span' would send each of the air segment to this function and the HTML will display it with line breaks as soon as the function returns the value. 我的理解是,“ span”中的“ for each”会将每个空段发送到此函数,并且只要函数返回值,HTML就会使用换行符显示它。

What can be the correct approach? 正确的方法是什么? Are there any other ways to split the air segments(with delimiter as a comma) which is now displaying all the air segments together with a comma. 是否有其他方法可以分割空段(以定界符作为逗号),现在将所有空段显示为一个逗号。

在此处输入图片说明

Kindly help. 请帮助。

On the lines of user @Origineil's fiddle the below worked for me: 在用户@Origineil的摆弄下 ,以下内容对我有用:

<!-- ko foreach: $parents[0].outputsToDisplay($data) -->
   <td>

    <span data-bind="if: $parents[1].Outputs()[$index()].InternalName === 'air_segments'">
        <span data-bind="foreach: $data">
            <span data-bind="text: $data"></span>
            <br/>
        </span>           
     </span>

  </td>
<!-- /ko -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM