Tp5.1 batch deletion of goods

Time:2020-9-14

Select the goods to be deleted and click batch delete

 

 

  • First, use SQL statement in the controller to find out the goods information
  • Then use the good variable in the HTML source code.

 

{foreach $goods as $item}
    
        
        123
        2324
    
    {/foreach}

 

 

Button source code

Datadel() "class = BTN BTN danger radius" >  batch deletion
  • js 

 

function datadel(){
                $ids = $("input[name='ids']:checked");
                var checkID=[];
                $("input[name='ids']:checked").each(function(i){
                      checkID[i] = $(this).val();
                });

                //Determine whether the array is empty. If it is empty, it is forbidden to click
                if(checkID.length == 0){
                    return;
                }

                // console.log(checkID);

 

    layer.confirm ('are you sure you want to delete it? ',function(index){
    // $ids = $(".ids");

     

        $.ajax({
            type: 'POST',
            url:"{:url('productBatchDelAjax')}",
            data:{checkID:checkID},
            dataType: 'json',
            success: function(data){
                // alert(data);
                // $(obj).parents("tr").remove();
                $ids.each(function(i){
                      $(this).parents("tr").remove();
                      // console.log($(this).parents("tr"));
                });
                layer.msg ('deleted! "{ icon:1 , time:1000 };
            },
            error:function(data) {
                console.log(data.msg);
            },
        });        
    });
}
  • The controller asynchronously requests to delete data

 

public function productBatchDelAjax()
    {

       
          $data = $_POST['checkID'];
          
          $DB = new Db;
          $res=$DB::table("goods")->delete($data);
          if($res){
            echo"ok";
          
          }

    }
  • be careful:
  1. How does jQuery submit the selected ID to the background

 

 

$(“input[name=’ids’]:checked”).each(function(i){

checkID[i] = $(this).val();
                });

2. How to operate DOM elements without refreshing the page after deletion

$ids.each(function(i){
                      $(this).parents("tr").remove();
                      // console.log($(this).parents("tr"));
                });

3. TP5 bulk deletion syntax. Data is an array.

$res=$DB::table("goods")->delete($data);