簡體   English   中英

如何將多維數組顯示為表格?

[英]How to display a multi dimensional array as a table?

這個問題困擾了我好幾天了...我嘗試使用Tempo.js將以下JSON呈現為表,但一直沒有呈現任何內容:

[  
   {  
      "id":"xxxxxxxxxxxxxxxxxxxxxxxxxx",
      "name":"Family One",
      "parents":[  
     {  
        "id":"yyyyyyyyyyyyyyyyyyyyyyyyyy",
        "name":"John Doe",
        "children":[  
           {  
              "id":"zzzzzzzzzzzzzzzzzzzzzzzzz",
              "name":"Fabrice A",
              "description":"Good kid",
              "Age":"20",
              "Weight":"60"
           }
        ]
     },
     {  
        "id":"hhhhhhhhhhhhhhhhhhhhhhhhhh",
        "name":"Jane Doe",
        "children":[  
           {
              "id":"wwwwwwwwwwwwwwwwwwwwwwww",
              "name":"Maurice A",
              "description":"Bad kid",
              "Age":"22",
              "Weight":"50"
           }
        ]
     }
  ]
   },
   {  
      "id":"xxxxxxxxxxxxxxxxxxxxxxxxxx2",
      "name":"Family Two",
      "parents":[  
     {  
        "id":"yyyyyyyyyyyyyyyyyyyyyyyyyy2",
        "name":"Sonny Doe",
        "children":[  
           {  
              "id":"zzzzzzzzzzzzzzzzzzzzzzzzz2",
              "name":"Juan B",
              "description":"Good kid",
              "Age":"30",
              "Weight":"70"
           },
           {  
              "id":"zzzzzzzzzzzzzzzzzzzzzzzzz3",
              "name":"Alberto B",
              "description":"Fine kid",
              "Age":"20",
              "Weight":"60"
           },
           {  
              "id":"zzzzzzzzzzzzzzzzzzzzzzzzz4",
              "name":"Roberto B",
              "description":"Medium kid",
              "Age":"10",
              "Weight":"50"
           }
        ]
     }
  ]
   }
]

該表應如下所示:

 _______________________________________________________________________________________
| FAMILY NAME |   PARENTS   | CHILD NAME | CHILD DESCRIPTION | CHILD AGE | CHILD WEIGHT |
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
| Family One  | John Doe    | Fabrice A  | Good kid          | 20        | 60           |
|             |''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
|             | Jane Doe    | Maurice A  | Bad kid           | 22        | 50           |
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
| Family Two  | Sonny Doe   | Juan B     | Good kid          | 30        | 70           |
|             |             | Alberto B  | Fine kid          | 20        | 60           |
|             |             | Roberto B  | Medium kid        | 10        | 50           |
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

請注意,家庭單元如何伸展以容納多於一排的父行,而父單元則伸展以容納多於一排的子行。 我在js中的一個名為familyTree的變量中准備JSON,然后調用Tempo.prepare('family-list').render(familyTree); 不,我已經閱讀了有關tempo.js的所有文檔(時間不太長),但是我仍然沒有找到正確呈現表格的方法。 這是到目前為止我得到的:

<div id="family-list">
   <table id="families">
       <tr data-before-template='data-before-template'>
       <th>
           FAMILY NAME
       </th>
       <th>
           PARENTS
       </th>
       <th>
           CHILD NAME
       </th>
       <th>
           CHILD DESCRIPTION
       </th>
       <th>
           CHILD AGE
       </th>
       <th>
           CHILD WEIGHT
       </th>
   </tr>
   <tr data-template='data-template'>
       <td id="family-column">
           {{name}}
       </td>
       <td id="parent-column" data-template-for='parents'>
           {{name}}
       </td>
       <td colspan="4">
           <table id='children-table' data-template-for="children">
               <tr>
                   <td>
                      {{name}}
                   </td>
                   <td>
                       {{description}}
                   </td>
                   <td>
                      {{age}}
                   </td>
                   <td>
                       {{weight}}
                   </td>
               </tr>
           </table>
       </td>
   </tr>

我以前用過節奏。 我什至將它與wkhtmltopdf混合在一起以呈現一些漂亮的pdf文件。 但是我無法解決這個問題。 如果你們當中有人經歷過類似的事情..請您分享您的經驗嗎? 非常感謝。

使用tempo.js在表中呈現分層數據並不理想。 為了呈現分層數據,tempo.js要求HTML元素也存在於分層結構中。 這在處理表時並不理想,因為在表單元格中創建表最終會導致列對齊問題。 您可以通過固定每列的寬度在一定程度上解決對齊問題,但這並不是一個完美的解決方案。

上面已經說過,我已經修復了您的HTML標記,以便使用tempo.js呈現JSON數據。 請參閱下面的更改( jsfiddle此處 ):

<div id="family-list">
  <table id="families">
    <tr data-before-template='data-before-template'>
      <th width="100px">
        FAMILY NAME
      </th>
      <th width="100px">
        PARENTS
      </th>
      <th width="100px">
        CHILD NAME
      </th>
      <th width="150px">
        CHILD DESCRIPTION
      </th>
      <th width="50px">
        CHILD AGE
      </th>
      <th width="50px">
        CHILD WEIGHT
      </th>
    </tr>
    <tr data-template='data-template'>
      <td id="family-column">
        {{name}}
      </td>
      <td colspan="5">
        <table cellpadding="0">
          <tr data-template-for='parents'>
            <td width="100px">
              {{name}}
            </td>
            <td>
              <table id='children-table' cellpadding="0">
                <tr data-template-for="children">
                  <td width="100px">
                    {{name}}
                  </td>
                  <td width="150px">
                    {{description}}
                  </td>
                  <td width="50px">
                    {{Age}}
                  </td>
                  <td width="50px">
                    {{Weight}}
                  </td>
                </tr>
              </table>
            </td>
          </tr>
        </table>
      </td>   
    </tr>
  </table>
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM