Amine Mostefaoui
Amine Mostefaoui

Reputation: 63

jQuery : Merge column of table

I want to merge cells of the last column of table of thé two next off each other have the same text:

enter image description here

I have this code but it check cells of rows next to each other:

<script language="javascript" type="text/javascript">
    $(document).ready(function () {

        $('#sample_1 tbody tr:nth-child(4) td:nth-child(4)').each(function(){
            alert( $(this).text());

            var colSpan=1;
            while( $(this).text() == $(this).next().text() ){

                $(this).next().remove();
                colSpan++;
            }
            $(this).attr('colSpan',colSpan);
        });
    });
</script>

This the code source of the table HTML :

<table class="table table-striped table-bordered dataTable TF" id="sample_1" aria-describedby="sample_1_info">
    <tbody role="alert" aria-live="polite" aria-relevant="all">
        <tr class="fltrow">
            <td colspan="4">
                <input id="flt0_sample_1" type="text" ct="0" class="single_flt">
            </td>
        </tr>
        <tr class="gradeX odd">
        </tr>
        <tr role="row">
            <th class="sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label="Username: activate to sort column ascending" style="width: 231px;">Nom</th>
            <th class="sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label="Username: activate to sort column ascending" style="width: 231px;">Prénom</th>
            <th class="sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label="Username: activate to sort column ascending" style="width: 231px;">Numéro Teléphone</th>
            <th class="sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label="Username: activate to sort column ascending" style="width: 231px;">heure de rdv</th>
        </tr>
        <tr role="row">
            <td class="hidden-phone ">
                MOSTEFAOui                                                    
            </td>
            <td class="hidden-phone ">
                MAiNE                                                    
            </td>
            <td class="hidden-phone ">
                0555122844                                                    
            </td>
             <td class="hidden-phone " colspan="1">
                09:00                                                    
            </td>
        </tr>
        <tr role="row">
            <td class="hidden-phone ">
                ADJOUT                                                    
            </td>
            <td class="hidden-phone ">
                ABDELKADER                                                   
            </td>

            <td class="hidden-phone ">
                0775522355                                                   
            </td>
            <td class="hidden-phone ">
                10:00                                                   
            </td>
        </tr>
        <tr role="row">
            <td class="hidden-phone ">
                SANAD                                                   
            </td>
            <td class="hidden-phone ">
                MILOUD                                                    
            </td>

            <td class="hidden-phone ">
                0661225412                                                    
            </td>
             <td class="hidden-phone ">
                10:00                                                    
            </td>
        </tr>
        <tr role="row">
            <td class="hidden-phone ">
                ACHAOUR                                                   
            </td>
            <td class="hidden-phone ">
                MOHAMED                                                    
            </td>

            <td class="hidden-phone ">
                0555122265                                                    
            </td>
             <td class="hidden-phone ">
                10:00                                                    
            </td>
        </tr>
        <tr role="row">
            <td class="hidden-phone ">
                TARFAUI                                                    
            </td>
            <td class="hidden-phone ">
                MORAD                                                    
            </td>

            <td class="hidden-phone ">
                0788745211                                                   
            </td>
             <td class="hidden-phone ">
                10:00                                                   
            </td>
        </tr>
        <tr role="row">
            <td class="hidden-phone ">
                TANEM                                                    
            </td>
            <td class="hidden-phone ">
                LAKHDAR                                                    
            </td>

            <td class="hidden-phone ">
                0666115487                                                    
            </td>
             <td class="hidden-phone ">
                11:00                                                    
            </td>
        </tr>
    </tbody>
</table>

Upvotes: 2

Views: 2685

Answers (1)

Bill Young
Bill Young

Reputation: 139

Not the most elegant solution but it works.

I added a class to your 4th column called "heure"

<td class="heure">11:00</td>

and the following changes to your JS

<script language="javascript" type="text/javascript">
    $(document).ready(function () {

        var topMatchTd;
        var previousValue = "";
        var rowSpan = 1;

        $('.heure').each(function(){

            if($(this).text() == previousValue)
            {
              rowSpan++;
              $(topMatchTd).attr('rowspan',rowSpan);
              $(this).remove();
            }
            else
            {
              topMatchTd = $(this);
              rowSpan = 1;
            }

            previousValue = $(this).text();
        });
    });
</script>

Upvotes: 1

Related Questions