Afficher un tableau imbriqué d'objets dans un tableau HTML en itérant
P粉638343995
P粉638343995 2024-04-05 14:11:42
0
1
1365

J'ai ce qui suit input contenant un tableau d'objets imbriqués.

summary是父级对象数组,run_type est un tableau imbriqué d'objets.

let input = {
      "summary": [
          {
              "name": "Release",
              "run_type": [
                {
                  "environment": "6nc",
                  "type": "QA1"
                },
                {
                  "environment": "3nc",
                  "type": "QA2"
                }
              ]
          }
      ]
  }

Je souhaite afficher le tableau comme ci-dessous. Puisque chaque summary有2个run_type,所以Name字段的rowspan vaut 2.

------------------------------------
   Name    | Environment | RunType |
------------------------------------
  Release  |     6nc     |  QA1    |
           |     3nc     |  QA2    |
------------------------------------

Pour afficher un tableau comme celui-ci de manière statique, je peux faire ceci

<table>
  <thead>
    <tr>
      <th>Vertical</th>
      <th>Environment</th>
      <th>RunType</th>
    </tr>
  </thead>
  <tbody>
  <tr>
    <td rowspan="2">Release</td>
    <td>6nc</td>
    <td>QA1</td>
  </tr>
  <tr>
    <td>3nc</td>
   <td>QA2</td>
  </tr>
  </tbody>
</table>

Quelqu'un peut-il me dire comment afficher dynamiquement ce tableau ? J'ai essayé de cette façon mais sans succès. Le problème est que je peux définir l'étendue des lignes de la colonne Nom sur 2 lignes, mais toutes les autres colonnes ne sont pas placées dans les deux lignes de la même section Nom.

<table>
  <thead>
    <tr>
      <th>Vertical</th>
      <th>Environment</th>
      <th>RunType</th>
    </tr>
  </thead>
  <tbody>
     {input?.summary?.map((project, indx) => {
       return (
         <tr>
           <td rowspan="2">{project?.name}</td>
             {project?.run_type?.map((runType, indx) => {
                return (
                  <>
                    <td>{runType.environment}</td>
                    <td>{runType.type}</td>
                  </>
                );
             })}
         </tr>
       );
     })}
  </tbody>
</table>

P粉638343995
P粉638343995

répondre à tous(1)
P粉431220279

Le problème est que vous utilisez un <tr>元素来迭代run_typeenvironnement et un type distincts. Cela entraîne un rendu incorrect de la structure de la table.

Voici comment modifier votre code pour y parvenir :

<tbody>
    {input?.summary?.map((project, projectIndex) => (
      <>
        {project?.run_type?.map((runType, runTypeIndex) => (
          <tr key={`${projectIndex}-${runTypeIndex}`}>
            {runTypeIndex === 0 ? (
              <td rowspan={project.run_type.length}>{project.name}</td>
            ) : null}
            <td>{runType.environment}</td>
            <td>{runType.type}</td>
          </tr>
        ))}
      </>
    ))}
  </tbody>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!