0

我想通过单击删除按钮从 ng-repeat 表中删除一行。该按钮可以正常工作,但是如果用户选择“是”,我想在单击删除按钮后显示一个确认框,那么只有它应该删除该特定行,否则不会。有人有什么解决办法吗?

        <script>
           var app= angular.module("myapp",[]);
            app.controller("mycontroller",function ($scope, $http)
                //get data from database
                {
                $http.get("getData.php").then(function (response) {
                    $scope.names = response.data.records;});
                            //Remove record
                                    $scope.remove = function(index,xid){

                                  var url = "remove.php?xid="+xid;

                                  $http.get(url).then(function successCallback(response) {

                                   $scope.names.splice(index, 1);

                                   $http.get("getData.php").then(function (response) {
                                        $scope.names = response.data.records;
                                    });
                                  }); 

                });    

               </script>
               <body ng-app ="myapp" ng-controller="mycontroller">

        <table border=1>

                <tr   ng-repeat="x in names  | filter: searchText | orderBy:sortColumn">

                    <td>{{x.Bank}}</td>
                    <td>{{x.cname}}</td>
                    <td >{{x.cfees}}</td>
                    <td>{{x.fpaid }}</td>
                    <td ng-model="bfess">{{x.cfees-x.fpaid}}</td>
                    <td>{{x.sdate | date:'dd-MM-yyyy'}}</td>
                    <td>{{x.edate | date:'dd-MM-yyyy'}}</td>
                    <td>{{calDate(x.edate,x.sdate)}}</td>
                    <td>{{calDate(x.edate,getToday()| date:'dd-MM-yyyy')}}</td>     
                    <td><button type="button" class="btn btn-danger"  ng-click="remove($index,x.id);" value="Delete">Delete</button></td>
            </tr>
            <tr>
                <td>Total fess paid by students: {{ calcTotal(names) }}</td>
            </tr>

        </table>
4

1 回答 1

0

您可以使用 javascript 的确认功能。阅读:https ://www.w3schools.com/jsref/met_win_confirm.asp

要使用它,您需要在发出删除 http 请求之前添加一个确认步骤:

$scope.remove = function(index,xid){
     var url = "remove.php?xid="+xid;
     if(confirm("Do you really want to delete entry " + xid + "?\nConfirm with ok.")){
         $http.get(url).then(function successCallback(response) {
              $scope.names.splice(index, 1);
              $http.get("getData.php").then(function (response) {
                  $scope.names = response.data.records;
              });
          });
     }
}

如果用户在显示的对话框中按下“确定”,确认对话框将返回 true。如果他按下“取消”,函数将返回 false,在这种情况下,什么都不会发生。

编辑:在 if 语句之后添加了另一个“)”。

于 2017-12-10T18:48:39.690 回答