doufangmu9087 2018-12-01 16:20
浏览 59

删除表中的数据时Ajax无刷新 - Laravel

My problem is I can click the button but it is needed to refresh to delete it . it there something that is making my ajax code a problem why it is refreshing? I want a button when click delete it will automatically delete without refreshing.

my Button

<button type="button" data-client_id="{{ $client->id }}" class="btn-archive btn btn-info">Active</button>

<button type="button" data-client_id="{{ $client->id }}" class="btn-delete fa fa-trash btn btn-danger"></button>

my AJAX

 <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(document).on('click','.btn-archive',function(){
   var clientID=$(this).attr('data-client_id');
   var url='/admin/clients/archTrash/'+clientID;
   callAjax(url);
});

$(document).on('click','.btn-delete',function(){
   var clientID=$(this).attr('data-client_id');
   var url='/admin/clients/archTrashPermanent/'+clientID;
   callAjax(url);
});

function callAjax(url){
   $.ajax({
       url:url,
       dataType:'json',
       type:'GET',
       success:function(response){
          console.log(response);
       },
       error:function(err){
         console.log(err);
       }
   });
}
</script>

Table Structure

` class Client extends Model { use SoftDeletes;

 protected $dates = ['deleted_at'];
 // Table Name
 protected $table = 'clients';
 // Primary Key
 public $primaryKey = 'id';
 // Timestamps
 public $timestamps = true;`
  • 写回答

1条回答 默认 最新

  • duanluangua8850 2018-12-01 17:39
    关注

    You are only doing half the work: Data is sent to the server and it seems everything is working as expected on that site. But then you want to "delete a client" (I assume you expect a table row to disappear) without reloading the page. You need to code this inside the success handler:

    success: function(response) {
        $('#client-data').remove();
        console.log(response);
    }
    

    Note this example code will always remove the same element with the ID client-data. You'll probably need to use something like $(this).parent().remove(); as there seem to be multiple delete buttons on your page.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法