如何使用Angular.js从表中删除多行?

时间:2019-11-13 11:09:45

标签: php angularjs

我正在尝试使用Angular和php复选框从表中删除多行, 但是我无法使它正常工作,我对Angular的了解到此为止!

这是我的桌子:

<div class="table-responsive" style="overflow-x: unset;">           
    <button class="btn btn-danger" ng-click="deleteBulk()" >Delete</button>

    <table datatable="ng" dt-options="vm.dtOptions" class="table table-bordered table-striped" >
        <thead>
            <tr>                
                <th ><input type="checkbox" ng-model="master" > select</th>
                <th>date</th>
                <th>first name</th>
                <th>last name</th>
                <th>age</th>
                <th>action</th>
                <th>action</th>
                <th>action</th>
            </tr>
        </thead>
        <tbody>
            <tr id="<?php echo $row["id"]; ?>" ng-repeat="name in namesData "     >
                <td name="checkbox">
                  <input type="checkbox" name="arrExample"
                         ng-model="arrInput" ng-true-value="{{name.id}}"
                         ng-checked="master"
                         ng-click='pushInArray(name.id)'>
                </td>
                <td>{{name.date}}</td>
                <td>{{name.first_name}}</td>
                <td>{{name.last_name}}</td>
                <td>{{name.age}}</td>
                <td><button type="button" ng-click="fetchSingleData(name.id)" class="btn btn-warning btn-xs">edit</button></td>
                <td><button type="button" ng-click="deleteData(name.id)" class="btn btn-danger btn-xs">delete</button></td>
                <td><button type="button" ng-click="single_move(name.id)" class="btn btn-success btn-xs">send</button></td>            
            </tr>
        </tbody>            
    </table>

这是我的Angular代码:

$scope.exampleArray = [];
$scope.pushInArray = function(id) {
     // get the input value
     var inputVal = id;
     var array = $scope.exampleArray.push(inputVal);
     $scope.deleteBulk = function(){
        if(confirm("Are you sure you want to delete this record?")){
             $http({
                 method:"POST",
                 url:"insert.php",
                 data:{'id':$scope.exampleArray, 'action' : 'deleteBulk'}
             }).success(function(data){
                $scope.success = true;
                $scope.error = false;
                $scope.successMessage = data.message;
                $scope.fetchData();
             });
        }
    };   
};

这是我在insert.php中的php代码

if($form_data->action == "deleteBulk")
{
    $query = "
     DELETE FROM orders WHERE id='".$form_data->id."'
    ";
    $statement = $connect->prepare($query);
    if($statement->execute())
    {
        $output['message'] = 'done!';
    }
}

有人可以告诉我我在做什么错吗? 谢谢

1 个答案:

答案 0 :(得分:1)

您每次都使用唯一ID删除一行。所以代码库是正确的。

要删除多行,请使用每行中的复选框,用户将选择要删除的多行,然后通过多个ID删除行。

您还需要更新SQL查询以一次删除多行。

类似以下内容

DELETE FROM orders WHERE id IN (1, 2, 3, 4, ..., 10);