3
votes

In my dataTable, I have a hidden column has contain the name of current Status of the data in closest row but when press the input checkbox, change text of that hidden column from this way:

$("#rBuscarPerfil").on('click','input[type="checkbox"]',function() {
    if($(this).is(':checked')) {
        $(this).closest('tr').find('td:last').html("Activo");
    } else {
        $(this).closest('tr').find('td:last').html("Desactivado");
    }
........
}

And when press the buttons of top, for example:

Active, in the input search of dataTable placed text "Activo" and table show only results has contain in the last td of each row, "Activo" word.

Inactivo, placed text in the input search of dataTable and display only results has contain in the last td of each row, "Desactivado" word.

The problem is this: The search works only when open the page or refresh browser but if I check or uncheck any checkbox in each row, the text in last TD the text in the last row, change correctly. But it seems that the table is in a current state and does not update its attributes and the search does not show results with those changes.

enter image description here

Html code table

<table cellspacing="1" id="rBuscarPerfil" class="tableResultsSearch" name="rBuscarPerfil">
<thead>
    <tr>
        <th>NOMBRE</th>
        <th>DESCRIPCIÓN</th>
        <th>ACCIÓN</th>
        <th>STATUS</th>
        <th style="display:none;">STATUS2</th>
    </tr>
</thead>
<tbody>
</tbody>
</table>

Js to load content in table

if(response.success == "success") {
if(area == "perfil") {
    $('#rBuscarPerfil tbody tr').remove();
    $.each(response, function (index,record){
        if(valNumber(index)) {
            var row = $("<tr class='"+record.IdPerfil+"'/>");
            $("<td nombre='"+record.NomPerfil+"'/>").text(record.NomPerfil).appendTo(row);
            $("<td />").text(record.DesPerfil).appendTo(row);
            $("<td />").html("<img src='media/icons/edit_item.png' class='bModifyProfile' cve='"+record.DesPerfil+"' alt='Editar' title='Editar'></img><img src='media/icons/delete_item.png' class='bDeleteProfile' cve='"+record.IdPerfil+"' alt='Eliminar' title='Eliminar'></img>").appendTo(row);

           if (record.EdoPerfil == 1) {
                $("<td />").html("<input type='checkbox' checked/>").appendTo(row);
                row.css("backgroundColor","#b0f2b1");
            } else {
                $("<td />").html("<input type='checkbox' />").appendTo(row);
                row.css("backgroundColor","#ffddec");
            }

            $("<td style='display:none;' />").text(record.nomStatus).appendTo(row);
            row.appendTo("#rBuscarPerfil tbody");
        }
    });
}
}

var oTable = $('#rBuscarPerfil').dataTable({
    "bJQueryUI": true,
    "bRetrieve" : true,
    "bDestroy" : true,
    "aaSorting": [[ 0, "desc" ]],
    "sPaginationType" : "full_numbers"
});

oTable.fnDraw();

I hope I have explained.

2
Can you provide jsfiddle?Dom
It's too much code, maybe I'll put the most important parts in the post, or wait, i'll try create a jsfiddleSoldierCorp
Add the event listener code you bound the logic tokidwon
See the edited post please!SoldierCorp

2 Answers

2
votes

Well I had the same problem as you and I fixed it by using the "cell" API object. Basically you have to make the changes with the API.

If you use the "oldschool" DataTable constructor you have to use dataTable().api() , but if you're using the new constructor, it becomes implicitly DataTable() with all the API

// 1. Get the referenced table with its API 
   var tableAPI = $("#rBuscarPerfil").dataTable().api();

// 2. Get the row nodes, because you have to apply the changes to the plugin data
   var nodes    = tableAPI.rows().nodes();

// 3. Let's do the magic!
   $('input[type="checkbox"]', nodes).on('click','input[type="checkbox"]', function () 
   {
     // Let's Store the reference
        var checkBox = this;

     // Could you convert it to a valid DataTable cell, please?
        var cell     = tableAPI.cell( $(checkBox, nodes).closest("tr").find("td:last") );
     // Thanks!

     // Finally change the sweet and tasty data
        cell.data( 
             $(checkBox).is(":checked") ? "Activo" : "Desactivado" 
        ).draw(); // Don't forget to call this beauty
    });

PS. Is my first answer so I hope it helped you.

UPDATED Well I realized about the draw() call at the end -- it refresh the data --, but also draws the data from the begining; so if you are in other page, after you make the change with the checboxes it will return you to the first page in the DataTable.

0
votes

My example:

<td id="H105" class="locker-bg-selected-td spacer" title="118242"><a class="lnkLocker locker-bg-selected-a cls0" href="javascript:$(`#modalLockStudent`).modal(`show`);$(`#`+$(`#H105`).find(`.span-coverScreen`).text()).fadeOut(5000);void(0);">H105</a><span class="span-student-name" style="display:none">My student 1/span><span id="spanH105" class="span-student-id" style="display:none">0101010</span><span class="span-ada" style="display:none"></span><span class="span-broken" style="display:none"></span><span class="span-reserved" style="display:none"></span><span class="span-serial" style="display:none">A1A1A1</span><span class="span-combo" style="display:none">18-02-32</span><span class="span-coverScreen" style="display:none">coverScreen</span></td>

<script>
        function updateDataTablesSearch(locker, studentId, studentName, coverScreen) {
            if (locker != null && locker != '') {                
                var tableAPI = $("#tbAD1").dataTable().api();
                var cell = tableAPI.cell('#' + locker);

                $(cell.node()).attr('title', studentId);
                $(cell.node()).find('.span-student-id').text(studentId);
                $(cell.node()).find('.span-student-name').text(studentName);
                $(cell.node()).find('.span-coverScreen').text(coverScreen);

                cell.data( 
                    cell.node().innerHTML
                );
                cell.draw();            
            }
        }
</script>