2016-07-12 26 views
5

Link to JSFiddlerowspan dinamico e complesso nella tabella HTML

Ecco il mio formato JSON

{ 
    "result": { 
     "buildname1": [{ 
      "table1": ["xxx","yyy"] 
     }, { 
      "table2": ["xxx","yyy"] 
     }, { 
      "table3": ["xxx","yyy"] 
     }], 
     "buildname2": [{ 
      "table1": ["xxx","yyy"] 
     }, { 
      "table2": ["xxx","yyy"] 
     }, { 
      "table3": ["xxx","yyy"] 
     }] 
    }, 
    "Build sets": "yyy", 
    "destinationPath": "xxx", 
    "status": 1 
} 

Questa è la funzione che sto usando per creare dinamicamente la tabella.

function generateTable(data){ //data is the parsed JSON Object from an ajax request 
    $("#test-table tbody").empty();//Empty the table first 
    if(data.result != null){ 
     $.each(data.result,function(key,value){ 
      var buildName ="<tr><td rowspan='"+value.length+"'>"+key+"<span class='cyan darken-1 white-text badge'>"+value.length+" base tables</span></td>"; 
      var baseTable =""; 
      for(i=0;i<value.length;i++){ 
       if(i == 0){ 
        for(var k in value[0]){ 
         baseTable ="<td rowspan='"+value[0][k].length+"'>"+k+"</td></tr>"; 
        } 
       } 
       else{ 
        for(var key in value[i]){ 
         baseTable = baseTable + "<tr><td rowspan='"+value[i][key].length+"'>"+key+"</td></tr>"; 
        } 
       } 
      } 
      $("#test-table").append(buildName + baseTable); 
     }); 
    } 
} 

Ecco quello che sto cercando di realizzare

enter image description here

HTML

<table id="test-table" class="bordered responsive-table"> 
    <thead> 
    <tr> 
     <th>Build Name</th><th>Base Table</th><th>Query List</th> 
    </tr> 
    </thead> 
</table> 

Domanda:

ho creato con successo le prime due colonne (se un po ' brutto, ho pensato di perfezionarlo più tardi), sono bloccato alla terza colonna. Il codice che ho postato crea correttamente le prime due colonne ma la logica per il rowspan all'interno del rowspan (terza colonna) sembra sfuggirmi. Per favore guidami.

+2

Hai un problema con questo? In tal caso, fornire i dettagli del problema. Al momento non ci sono domande qui –

+0

@RoryMcCrossan mi dispiace. Ho postato la domanda troppo presto senza averla prima verificata correttamente –

+0

Sarebbe anche utile avere il tuo html, anche se possiamo indovinarlo in questo caso, prendi l'abitudine di postarlo nella tua domanda o in una codepen/jsfiddle – RDardelet

risposta

4

Sinceramente non avevo mai usato rowspan prima, ma dopo aver letto this stack answer l'ho capito molto meglio - consiglio vivamente di fare lo stesso. Dopodiché, era solo questione di capire l'ordine degli elementi da JSON nel DOM.

Ecco una demo funzionante:

var data = '{"result":{"buildname1":[{"table1":["xxx","yyy", "zzz"]},{"table2":["xxx","yyy"]}],"buildname2":[{"table1":["xxx","yyy", "zzz"]},{"table2":["xxx","yyy"]},{"table3":["xxx","yyy"]}]},"Build sets":"yyy","destinationPath":"xxx","status":1}'; 
 

 
function generateTable(data) { //data is the parsed JSON Object from an ajax request 
 
    data = JSON.parse(data); 
 
    $("#test-table tbody").empty(); //Empty the table first 
 
    $.each(data.result, function(key, elem) { 
 
    var baseHtml = ""; 
 
    var childrenHtml = ""; 
 
    var maxRowSpan = 0; 
 
    $.each(elem, function(index, inner_elem) { 
 
     var innerElemKey = Object.keys(inner_elem)[0]; 
 
     var arr = inner_elem[innerElemKey]; 
 
     var elemRowSpan = arr.length; 
 
     maxRowSpan += arr.length; 
 

 
     if (index === 0) { 
 
     childrenHtml += ('<td rowspan="' + elemRowSpan + '">' + innerElemKey + '</td>'); 
 
     $.each(arr, function(indx, child) { 
 
      if (indx === 0) { 
 
      childrenHtml += ('<td rowspan="1">' + child + '</td>' + '</tr>'); 
 
      } else { 
 
      childrenHtml += ('<tr><td rowspan="1">' + child + '</td>' + '</tr>'); 
 
      } 
 
     }); 
 
     } else { 
 
     childrenHtml += ('<tr><td rowspan="' + elemRowSpan + '">' + innerElemKey + '</td>'); 
 
     $.each(arr, function(indx, child) { 
 
      if (indx === 0) { 
 
      childrenHtml += ('<td rowspan="1">' + child + '</td>' + '</tr>'); 
 
      } else { 
 
      childrenHtml += ('<tr><td rowspan="1">' + child + '</td>' + '</tr>'); 
 
      } 
 
     }); 
 
     } 
 
    }); 
 
    baseHtml += ('<tr><td rowspan="' + maxRowSpan + '">' + key + '</td>'); 
 
    $("#test-table").append(baseHtml + childrenHtml); 
 
    }); 
 
} 
 

 
$(function() { 
 
    generateTable(data); 
 
});
td { 
 
    border: 1px solid black; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="test-table" class="bordered responsive-table"> 
 
    <thead> 
 
    <tr> 
 
     <th>Build Name</th> 
 
     <th>Base Table</th> 
 
     <th>Query List</th> 
 
    </tr> 
 
    </thead> 
 
</table>

HTML statico

<table id="test-table" class="bordered responsive-table"> 
    <thead> 
    <tr> 
     <th>Build Name</th><th>Base Table</th><th>Query List</th> 
    </tr> 
    </thead> 
</table> 

HTML generato

<table id="test-table" class="bordered responsive-table"> 
    <thead> 
    <tr> 
     <th>Build Name</th> 
     <th>Base Table</th> 
     <th>Query List</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td rowspan="5">buildname1</td> 
     <td rowspan="3">table1</td> 
     <td rowspan="1">xxx</td> 
    </tr> 
    <tr> 
     <td rowspan="1">yyy</td> 
    </tr> 
    <tr> 
     <td rowspan="1">zzz</td> 
    </tr> 
    <tr> 
     <td rowspan="2">table2</td> 
     <td rowspan="1">xxx</td> 
    </tr> 
    <tr> 
     <td rowspan="1">yyy</td> 
    </tr> 
    <tr> 
     <td rowspan="7">buildname2</td> 
     <td rowspan="3">table1</td> 
     <td rowspan="1">xxx</td> 
    </tr> 
    <tr> 
     <td rowspan="1">yyy</td> 
    </tr> 
    <tr> 
     <td rowspan="1">zzz</td> 
    </tr> 
    <tr> 
     <td rowspan="2">table2</td> 
     <td rowspan="1">xxx</td> 
    </tr> 
    <tr> 
     <td rowspan="1">yyy</td> 
    </tr> 
    <tr> 
     <td rowspan="2">table3</td> 
     <td rowspan="1">xxx</td> 
    </tr> 
    <tr> 
     <td rowspan="1">yyy</td> 
    </tr> 
    </tbody> 
</table> 
+1

Grazie mille :). Il link che hai pubblicato è stato molto utile per aiutarmi a capire questa soluzione. –

+0

https://jsfiddle.net/6xytngz7/ Si prega di dare un'occhiata a questo violino. Cosa succede se voglio evitare voci duplicate come 'xxx, xxx' e' unique' loro ma anche mantenere il conteggio come ' xxx' –

+0

Si prega di vedere questa domanda http: // StackOverflow.com/domande/38343211/valori sottolineatura-js-to-filtro-out-the-unico- –

Problemi correlati