9
votes

Fusionner des cellules de table égales avec jQuery

table HTML simple avec des valeurs NXM. L'objectif est de fusionner des cellules égales dans la colonne avec jQuery. Remarque, que dans une rangée, il n'y a pas de doublons.

J'ai la façon de cacher les cellules égales, mais y a-t-il un moyen de combiner une cellule avec des données avec une cellule vide en une?

HTML: xxx

js: xxx

jsfiddle

PS une dernière chose, les données sont extraites à l'origine dans le tableau JSON, puis je fais .parsjson () d'abord et mettez des données dans la table à l'aide de: xxx

upd

a fait une bonne solution pour 2 cellules. ici est sa solution. Mais s'il y aura plus de 2 cellules égales.


2 commentaires

Alors, ici "premier" et "A" devrait être une cellule?


"Première" et la cellule vide sous elle devrait être fusionnée. S'il vous plaît, vérifiez le jsfiddle


6 Réponses :




1
votes

J'ai vraiment aimé la première solution Farid, mais je devais sélectionner la gamme de lignes et quelles colonnes qu'il serait appliquée, j'ai donc fait quelques modifications (y compris la possibilité de plus de 2 cellules fusionnées). http://jsfiddle.net/djhu7/72/ xxx


0 commentaires

4
votes

Voici une version annoncable de La réponse de Carla :

p>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="SummerizeTable('#table1')">Summerize</button>
<table id="table1" border="1" cellspacing="0" >
  <thead>
    <tr>
      <th>State</th>
      <th>City</th>
      <th>Street</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>VT</td>
      <td>Burlington</td>
      <td>Elm</td>
    </tr>
    <tr>
      <td>NY</td>
      <td>Manhattan</td>
      <td>Main</td>
    </tr>
    <tr>
      <td>NY</td>
      <td>Manhattan</td>
      <td>Oak</td>
    </tr>
    <tr>
      <td>NY</td>
      <td>Albany</td>
      <td>State</td>
    </tr>
  </tbody>
</table>


1 commentaires

note : les commentaires disent si le texte est identique , mais la fonction est en train de comparer la sortie de .html () qui pourrait différer façons qui doivent être transparents à l'utilisateur (en raison de différents attributs ou de champs cachés ou non des identifiants textuels). Il pourrait donc être préférable de comparer le TALMMED texte de chaque comme ceci $. Couper ($ this.text ())



1
votes

J'ai étendu la solution de Carla. Avec deux fonctions, nous pouvons fusionner horizontalement ou verticalement et exclure ou inclure des cellules à fusionner. Essayez l'échantillon de travail. https://jsfiddle.net/bn3u63pe

/*
* merge horizontally
* ex) autoMergeByCol('theTable', 2, 0, 0);
*/
function autoMergeByCol(tableId
        , rowStartIndex // zero or positive
        , colStart      // zero or positive
        , colEnd        // equals to colStart or greater than colStart or negative to go to the end of cols
        ) {
    /*
    console.log('autoMergeByCol tableId=' + tableId
        + ', rowStartIndex=' + rowStartIndex
        + ', colStart=' + colStart
        + ', colEnd=' + colEnd
    );
    */
    var trArr = $('#' + tableId).find('tr');        // rows array
    for(var rowIndex = rowStartIndex ; rowIndex < trArr.length ; rowIndex++) {
    var tdArr = $(trArr[rowIndex]).find('td');  // cols array of the row
    if(colEnd < 0) colEnd = tdArr.length - 1;       // if colEnd is negative, process at the end of the cols;
        for(var colIndex = colStart ; colIndex < tdArr.length && colIndex <= colEnd ; colIndex++) {
            var span = 1;
            var theCell = $(tdArr)[colIndex];
            if($(theCell).attr('rowspan')) {continue;}
            var cellNext = $($(theCell).parent().children()[colIndex + span]);      
            while(cellNext != undefined 
                    && $(theCell).text() == $(cellNext).text()
                    && colIndex + span <= colEnd ) {
                span++;
                cellNext.hide();
                cellNext = $($(cellNext).parent().children()[colIndex + span]);     
            }
            if(span > 1) $(theCell).attr('colspan', span);
        }
    }
}

/*
* merge vertically
* ex) autoMergeByCol('theTable', 2, 0, 0);
*/
function autoMergeByRow(tableId
        , rowStartIndex // zero or positive
        , colStart      // zero or positive
        , colEnd        // equals to colStart or greater than colStart or negative
        ) {
    /*
    console.log('autoMergeByRow tableId=' + tableId
        + ', rowStartIndex=' + rowStartIndex
        + ', colStart=' + colStart
        + ', colEnd=' + colEnd
    );
    */
    var trArr = $('#' + tableId).find('tr');            // rows array
    for(var rowIndex = rowStartIndex ; rowIndex < trArr.length ; rowIndex++) {
        var tdArr = $(trArr[rowIndex]).find('td');  // cols array of the row
        if(colEnd < 0) colEnd = tdArr.length - 1;       // if colEnd is negative, process at the end of the cols;
        for(var colIndex = colStart ; colIndex < tdArr.length && colIndex <= colEnd ; colIndex++) {
            var span = 1;
            var theCell = $(tdArr)[colIndex];
            if($(theCell).attr('colspan')) {continue;}
            var cellBelow = $($(theCell).parent().next().children()[colIndex]);         
            while(cellBelow != undefined 
                    && $(theCell).text() == $(cellBelow).text()) {
                span++;
                cellBelow.hide();
                cellBelow = $($(cellBelow).parent().next().children()[colIndex]);           
            }
            if(span > 1) $(theCell).attr('rowspan', span);          
        }
    }
}


0 commentaires

1
votes
  $(document).ready(function () {
        SummerizeTable($('#example'));
      })



      function SummerizeTable(table) {
        $(table).each(function () {
          $(table).find('td').each(function () {
            var $this = $(this);
            var col = $this.index();
            var html = $this.html();
            var row = $(this).parent()[0].rowIndex;
            var span = 1;
            var cell_above = $($this.parent().prev().children()[col]);

            while (cell_above.html() === html) { 
              span += 1; 
              cell_above_old = cell_above; 
              cell_above = $(cell_above.parent().prev().children()[col]); 
            }

            if (span > 1) {
              $(cell_above_old).attr('rowspan', span);
              $this.hide();
            }

          });
        });
      }
See Working Example  here. 

0 commentaires