【问题标题】:delete a row from table in angularjs从angularjs中的表中删除一行
【发布时间】:2016-05-20 16:41:18
【问题描述】:

我创建了一个数据库,其中包含多个表格,例如问题、答案等。我创建了一个视图页面并显示数据库中的数据。我还为此视图创建了一个控制器。我在视图中的每条记录的末尾都给出了一个删除按钮。如何在此删除按钮单击上删除记录

我的看法

<div class="container" ng-controller="questionEditCtrl">

    <form class="form-horizontal" role="form" name='quizEdit' ng-submit="submit(data)">
    <div class="table-responsive">          
        <table class="table">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Questions</th>
                    <th>Answer 1</th>
                    <th>Answer 2</th>
                    <th>Answer 3</th>
                    <th>Answer 4</th>
                    <th>Answer 5</th>
                    <th>Correct Answer</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php
                foreach ($quizes as $q) {
                    ?>
                    <tr>
                        <td ng-model="data.quesID"><?php echo $q['question_id']; ?></td>
                        <td ng-model="data.ques"><?php echo $q['question']; ?></td>
                        <td ng-model="data.ans0"><?php
                            if (isset($q['answers'][0])) {
                                echo $q['answers'][0];
                            }
                            ?></td>
                        <td ng-model="data.ans1"><?php
                            if (isset($q['answers'][1])) {
                                echo $q['answers'][1];
                            }
                            ?></td>
                        <td ng-model="data.ans2"><?php
                            if (isset($q['answers'][2])) {
                                echo $q['answers'][2];
                            }
                            ?></td>
                        <td ng-model="data.ans3"><?php
                            if (isset($q['answers'][3])) {
                                echo $q['answers'][3];
                            }
                            ?></td>
                        <td ng-model="data.ans4"><?php
                    if (isset($q['answers'][4])) {
                        echo $q['answers'][4];
                    }
                    ?></td>
                        <td ng-model="data.corr_ans"><?php
                    if (isset($q['correctAnswer'])) {
                        echo $q['correctAnswer'];
                    }
                    ?></td>
                        <td><a href="">Edit</a> / <a href="" ng-click="delete()">Delete</a></td>

                    </tr>
    <?php
}
?>
            </tbody>
        </table>
    </div>
    </form>
</div>

控制器

;
(function () {
    'use strict';

    angular.module('app', []).controller('questionEditCtrl', ['$scope', '$timeout', '$http', function ($scope, $timeout, $http) {


            $scope.delete = function () {
                if (confirm("Are you sure you want to delete this record?")) {


                    // todo code for deletion

                }
            };

  }]);

})();

【问题讨论】:

标签: mysql angularjs delete-row


【解决方案1】:

也许这会对你有所帮助。 在 delete 方法中传递行的 id 并将 make angular ajax post 调用传递给服务器以删除行。

  (function() {
      'use strict';

      angular.module('app', []).controller('questionEditCtrl', ['$scope', '$timeout', '$http',
        function($scope, $timeout, $http) {


          $scope.delete = function(data) {
            if (confirm("Are you sure you want to delete this record?")) {


              // todo code for deletion
              // in data variable you will get id of the row and using that id
              // you can make a server call to delete the data using post method you need to modify your delete method i.e make it which accept data as a aurgument or you can passs id of the row is to deleted from talbe 

            }
          };

        }
      ]);

    })();

【讨论】:

    【解决方案2】:

    你为什么在你的应用程序中使用 php?这对我来说似乎是一件可怕的事情。 angular 可以轻松地自行处理您使用 php 完成的部分...

    (见这里:) Should I mix AngularJS with a PHP framework?

    关于您的问题: 您必须在您的服务器上实现一个 DELETE 请求,并使用 $http 指令在您的控制器中使用 angular 调用它。 之后,您可以使用您可能实现的相应 GET 请求再次从服务器获取数据,如果您正确执行所有操作,angular 将更新视图本身。

    但如前所述,这里的主要问题是首先混合 angular 和 php。

    【讨论】:

    • 你能告诉我如何完全使用 angularjs 编写这个视图吗?并且不使用 php .. 我无法让 ng-repeat 工作
    • 对不起,但我不认为这是做这种事情的地方。您可能应该首先深入了解一些基本的 angularjs 教程。这个想法是:1)通过 $http 从您的服务器获取数据 2)将其放入控制器上的某个变量 3)通过 ng-repeat 在您的视图中可视化它。那就是说你应该准备好通过一些我认为的教程来工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多