【问题标题】:Ajax No Refresh when Deleting Data in a Table - LaravelAjax 删除表中的数据时不刷新 - Laravel
【发布时间】:2019-05-03 12:11:54
【问题描述】:

我的问题是我可以点击按钮,但需要刷新才能删除它。有什么东西让我的 ajax 代码出现问题,为什么它会让人耳目一新?我想要一个按钮,点击删除它会自动删除而不刷新。

我的按钮

<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>

我的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>

表结构

` 类客户端扩展模型 { 使用软删除;

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

【问题讨论】:

  • 您是删除一行还是只删除数据?你的问题不是很清楚。
  • @Daniel 嗨先生,我正在删除每一行是我很抱歉的误解。
  • 您能帮帮我吗?我被困在这里:(@Daniel
  • 提供你的表结构。
  • 我已经添加了我的模型请看

标签: php html ajax laravel eloquent


【解决方案1】:

如果您想删除 larval 中的记录而不使用 ajax 刷新页面,请使用以下代码。我正在使用此代码并且它工作正常

  1. 在此按钮类中将 id 发送到 ajax,您可以将其关联到您的 id &lt;button class="deleteRecord btn btn-danger" data-id="{{ $party-&gt;id }}" &gt;Delete Record&lt;/button&gt;

  2. ajax代码is like this with sweet alert

$(".deleteRecord").click(function(){

var id = $(this).data("id");
var token = $("meta[name='csrf-token']").attr("content");
var parent = $(this).parent();

  swal({
                  title: "Wait..!",
                  text: "Are You sure, You want to delete Party?",
                  icon: "warning",
                  buttons: true,
                  dangerMode: true,
                }).then((willDelete) => {

                    if (willDelete) {
$.ajax({
    url: "delete_party/"+id,
    type: 'DELETE',
    data: {
        "id": id,
        "_token": token,
    },
    success: function (){

           parent.slideUp(300, function () {
                parent.closest("tr").remove();
            });


    },
    error: function() {

        alert('error');
    },
});
  

   } else {

         swal("Your Party is safe");
   }

});

});

``

【讨论】:

    【解决方案2】:

    这就是我正在做的:

    在创建表时,我将唯一的 ID(在本例中为用户 ID)附加到可以删除的每一行。这就是我在刀片中的操作方式:

    @foreach ($visitors as $visitor)
        <tr class="data-user-id-{{$visitor->id}}">
            <td class="text-left">
                {{$visitor->name}}
            </td>
            <td class="text-left" data-original-value="11">
                {{$visitor->email}}
            </td>
            <td class="text-left">
                <a href="#" data-user-id="{{$visitor->id}}" class="btn btn-small btn-info btn-visitor-enable-disable">
                    Enable
                </a>
            </td>
            <td class="text-left">
               <a href="#" data-user-id="{{$visitor->id}}" class="btn btn-small btn-danger btn-visitor-delete">
                  X
               </a>
             </td>
       </tr>
    @endforeach
    

    然后,在我的 .ajax 调用的成功方法中,我只需使用已删除行的唯一 ID 选择该行并删除该行。我就是这样做的。

    $(".btn-visitor-delete").on('click',function(e) {
        e.preventDefault();
        const userId = $(this).attr('data-user-id');
        var confirmation = confirm("Are you sure you want to delete this user?");
        if (confirmation) {
            $.ajax({
                type:'GET',
                url:'/dashboard/allwhitelistedusers/delete/' + userId,
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                // data:{user_id: userId},
                success:function(data){
                    //Refresh the grid
                    $(".data-user-id-"+userId).remove();
                    alert(data.success);
                },
                error: function(e){
                    alert(e.error);
                }
            });
        }
        else{
            //alert ('no');
            return false;
        }
    });
    

    【讨论】:

      【解决方案3】:

      您只完成了一半的工作:数据已发送到服务器,并且该站点上的一切似乎都按预期工作。但是您想“删除客户端”(我假设您希望表格行消失)而不重新加载页面。您需要在成功处理程序中对此进行编码:

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

      请注意,此示例代码将始终删除 ID 为 client-data 的相同元素。您可能需要使用 $(this).parent().remove(); 之类的东西,因为您的页面上似乎有多个删除按钮。

      【讨论】:

      • 我需要提供表结构吗?
      • 你是对的@Pida,你认为这是我的代码中缺少的代码吗?为什么?你的代码背后的逻辑是什么?
      • 最后,我应该在哪里将该代码放在我的 ajax 代码中?抱歉,我对 ajax 不太熟悉。另外,“客户数据”这个名字是从哪里来的?你能更明确地解释一下吗?谢谢
      • 这个想法只是更新页面。假设您有 10 个客户。目前,您只是指示服务器从数据库中删除其中一个。但是您的页面仍将显示 10 行,直到您重新加载它或使用 JavaScript 删除一行。是的,请向我们展示您的表格结构。 client-data 只是一个占位符。
      • 哦,对对对!但是client-data 我很困惑。我的输入表单上没有客户数据名称。我应该把客户数据放在哪里?还是我不应该担心?
      猜你喜欢
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      相关资源
      最近更新 更多