简体   繁体   English

ajax功能成功刷新表

[英]Refresh table in success of ajax function

I have the following table where I have two buttons to delete and update.我有下表,其中有两个按钮可以删除和更新。 When I delete it automatically removes the table row.当我删除它时,它会自动删除表格行。 But when I edit the line and change the state, the user still sees the line, where it gets confusing because they don't know the lines that have already been edited and the ones left to edit.但是,当我编辑该行并更改状态时,用户仍然会看到该行,因为他们不知道已编辑的行和尚待编辑的行,因此会让人感到困惑。 So when changing state the row should also disappear from the table.因此,当更改状态时,该行也应该从表中消失。

Code:代码:

<div id="spoiler2" class="esconde">
<table class="table table-responsive" id="employee_table2"> 
<h1 style="font-size: 30px"><strong>Pedidos de Manutenção</strong></h1>
<thead>  
<tr> 
<th>Data</th>
<th>Valência</th>
<th>Descrição</th>
<th>Colaborador</th>        
<th>Estado</th> 
<th>Ação</th>   
<th>Eliminar</th>                           
</tr> 
</thead>
<tbody>
<?php  do{ ?>
<tr  id="<?php echo $produto2["Id"]; ?>">
<td><?php echo $produto2["DataRegisto"]; ?></td> 
<td><?php echo $produto2["Destino"]; ?></td> 
<td><?php echo $produto2["Descricao"]; ?></td> 
<td><?php echo $produto2["nome"]; ?></td>
<td><?php echo $produto2["Estado"]; ?></td>
<td><button type="button" name="edit" data-id="<?php echo $produto2["Id"]; ?>" id="open_<?php echo $produto2["Id"]; ?>" data-target="#add_data_Modal2" class="btn btn-warning btn-sm edit_data2" ><span class="glyphicon glyphicon-pencil"></span></button></td>
<td><button class="btn btn-danger btn-sm remove"><span class="glyphicon glyphicon-trash"></span></button></td>
</tr>  
<?php } while($produto2 = $result2->fetch_assoc()); ?>
</tbody>      
</table>   
</div>

js: js:

$(document).on('click', '.edit_data2', function(){  
           var employee_id2 = $(this).data('id');          
           $.ajax({  
                url:"./editarmanutencao",  
                method:"POST",
                cache: false,               
                data:{employee_id2:employee_id2},               
                dataType:"json",  
                success:function(data){

                    console.log(data);
                     $('#Id2').val(data.Id);
                     $('#Tratamento').val(data.Tratamento);
                     $('#Estado2').val(data.Estado);
                     $('#Prestador').val(data.Prestador);
                     $('#employee_id2').val(data.Id);                    
                     $('#insert2').val("Gravar");  
                     $("#add_data_Modal2").modal("show");                    
                }  
           });

      });  

function inserir_registo2()
{  

    var dadosajax = {
        'Id' : $("#Id2").val(),
        'DataTermino' : $("#DataTermino").val(),
        'Tratamento' : $("#Tratamento").val(),
        'Estado' : $("#Estado2").val(),
        'Prestador' : $("#Prestador").val()
    };
    $.ajax({
        url: './resolucaomanutencao',
        type: 'POST',
        cache: false,
        data: dadosajax,
        error: function(){
          $(".error_message").removeClass('hide');
        },
        success: function(result)
        { 
        $("#add_data_Modal2").modal("hide");
         }  
    });
}

How can I remove the table row by changing ajax's success state?如何通过更改 ajax 的成功状态来删除表格行?

If i understand correctly, you want to remove row from table after successful Edit operation.如果我理解正确,您想在成功编辑操作后从表中删除行。 If this is the case, you can use following code...如果是这种情况,您可以使用以下代码...

function inserir_registo2()
{  
    let rowToBeRemoved =  $("#"+$("#Id2").val());
    var dadosajax = {
        'Id' : $("#Id2").val(),
        'DataTermino' : $("#DataTermino").val(),
        'Tratamento' : $("#Tratamento").val(),
        'Estado' : $("#Estado2").val(),
        'Prestador' : $("#Prestador").val()
    };
    $.ajax({
        url: './resolucaomanutencao',
        type: 'POST',
        cache: false,
        data: dadosajax,
        error: function(){
          $(".error_message").removeClass('hide');
        },
        success: function(result)
        { 
        $("#add_data_Modal2").modal("hide");
        if(<<Condition>>) // in case you want to validate something before removing row
         {
           rowToBeRemoved.remove();
          }
      }  
    });
}

在 ajax 调用之前保存对父行的引用(例如var row = $(this).closest('tr') ),然后在成功时通过row.remove()删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM