3

在我的数据表中,我有一个隐藏列包含最近行中数据的当前状态的名称,但是当按下输入复选框时,通过这种方式更改该隐藏列的文本:

$("#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");
    }
........
}

并且当按下顶部的按钮时,例如:

活动的,在 dataTable 的输入搜索中放置了文本“Activo”,并且表显示仅结果在每行的最后一个 td 中包含“Activo”字样。

Inactivo,在 dataTable 的输入搜索中放置文本并仅显示结果,在每行的最后一个 td 中包含“Desactivado”字样。

问题是这样的:搜索仅在打开页面或刷新浏览器时有效,但如果我选中或取消选中每一行中的任何复选框,则最后一行中的文本最后一行中的文本会正确更改。但似乎该表处于当前状态并且不会更新其属性,并且搜索不会显示具有这些更改的结果。

在此处输入图像描述

html代码表

<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加载表格内容

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();

我希望我已经解释过了。

4

2 回答 2

2

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.

于 2016-08-14T20:36:50.193 回答
0

我的例子:

<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>
于 2019-09-19T13:55:06.070 回答