javascript - 编辑记录需要刷新页面

标签 javascript angularjs

一切正常,但是当我插入一条记录并尝试编辑同一条记录时,它不会被编辑,并且要编辑同一条记录,我必须刷新页面,更新中也会发生同样的情况,一旦更新记录,我必须刷新编辑同一条记录的页面

<html ng-app="crudApp">
    <head>
        <meta charset="UTF-8">
        <title>Angular js</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    </head>

    <body ng-controller="DbController">
        <form   id="form" name="employee">
            First Name: <input type="text" ng-model="firstname"><br/><br/>
            Last Name: <input type="text" ng-model="lastname"><br/><br/>
            <input type="submit" name="btnInsert" id="Insert" value="Insert" ng-click='insertData()'/>
            <input type="submit" name="btnUpdate" id="Update"value="Update" ng-click='UpdateData(currentUser)'/>
        </form>

        <div>
            <table border="1">
                <tr>
                    <th>Firstname</th>
                    <th>Lastname</th>
                    <th>Delete</th>
                    <th>Edit</th>
                </tr> 
                <tr ng-repeat="detail in details">
                    <td>{{detail.firstname}}</td>
                    <td>{{detail.lastname}}</td>
                    <td><input type="button" value="Delete"  ng-click="deleteInfo(detail.id)"/></td>
                    <td><input type="button" value="Edit"  ng-click="editInfo(detail.id)"/></td>
                </tr>
            </table>
        </div>
        <script>
                    var crudApp = angular.module('crudApp', []);
                    crudApp.controller("DbController", ['$scope', '$http', function ($scope, $http) {

                            getInfo();
                            function getInfo() {
                                $http.post('select.php').success(function (data) {
                                    // Stored the returned data into scope
                                    $scope.details = data;
                                });
                            }

                            $scope.deleteInfo = function (info) {
                                $http.post('delete.php', {del_id: info
                                }).success(function (result) {
                                    getInfo()
                                    console.log(result);
                                });
                            }

                            $scope.insertData = function () {
                                $http.post("insert.php", {
                                    'firstname': $scope.firstname,
                                    'lastname': $scope.lastname,
                                }).success(function () {
                                    getInfo();
                                    document.getElementById("form").reset();
                                });
                            }


                            $scope.editInfo = function (info) {
                                $scope.currentUser = info;
                                $http.post('edit.php', {edit_id: $scope.currentUser,
                                }).success(function (result) {
                                    $scope.firstname = result[0]['firstname'],
                                            $scope.lastname = result[0]['lastname'],
                                            $("#Insert").hide();
                                    $("#Update").show();
                                });
                            }

                            $scope.UpdateData = function (currentUser) {
                                $http.post("update.php", {
                                    'firstname': $scope.firstname,
                                    'lastname': $scope.lastname,
                                    'update_id': currentUser,
                                }).success(function (result) {
                                    getInfo();
                                    $("#Insert").show();
                                    $("#Update").hide();
                                    document.getElementById("form").reset();
                                });
                            }

                        }]);

        </script>

    </body>

</html>

最佳答案

编辑后,您将把响应数据插入到新变量中。这不应该发生。您应该仅更新旧数据(即 $scope.detail )。

方法如下:

首先,在您的<table>中代码,您必须再传递 1 个参数,即 $index 。这个$index是我们必须更新的记录的键。我们需要它来更新 $scope.detail 中的记录.

<table border="1">
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Delete</th>
        <th>Edit</th>
    </tr> 
    <tr ng-repeat="detail in details">
        <td>{{detail.firstname}}</td>
        <td>{{detail.lastname}}</td>
        <td><input type="button" value="Delete"  ng-click="deleteInfo(detail.id)"/></td>
        <td><input type="button" value="Edit"  ng-click="editInfo(detail.id, $index)"/></td>
    </tr>
</table>

其次,请参阅 editInfo() 的代码。而不是将响应数据分配给新变量。我们正在更新 $scope.detail 的旧数据仅有的。了解我们如何使用 index 找到我们的数据我们从上一步中获得的变量。

//info: this info is the detail.id that you've passed from the table
//index: this is the index of the record from $scope.detail that we want want to edit.
$scope.editInfo = function (info, index) {
    $scope.currentUser = info;
    $http.post('edit.php', {edit_id: $scope.currentUser,
    }).success(function (result) {

        $scope.detail[index] = result[0]['firstname'];
        $scope.detail[index] = result[0]['lastname'];

        $("#Insert").hide();
        $("#Update").show();
    });
}

关于javascript - 编辑记录需要刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38909802/

相关文章:

javascript - 更改与值相关的颜色

javascript - 从循环内更新数据

angularjs - Angular UI 路由器 : nested states for home to differentiate logged in and logged out

angularjs - 如何说 Protractor 等待页面加载?

javascript - 通过它在 Firebase 中的唯一 ID 查找对象

javascript - 函数使用的内存被删除后会发生什么

javascript - 如果我使用相同的对象,为什么我的 console.log 显示不同的属性值? react js

javascript - 将 Google map 集成到 WinJS 中

javascript - AngularJS 组件和 ADAL(用于 JS 的 Active Directory Azure 库)

javascript - 我怎样才能做出瀑布 Q promise ?