user6060368
user6060368

Reputation:

How to refresh the table records automatically in angularjs after an event occurs

I am working on a Web App , using Laravel as backend API and AngularJS for frontend. I have successfully fetched the data from Laravel API and displayed it via AngularJS ng-repeat . Now i want a delete button for each record which is displayed in the table.When a user click that delete button it should delete the clicked record.I did the following try which is working perfectly.But the problem occurs when i click delete button it deletes record from database but it is not refreshing the records list , instead of refreshing it just shows the headers titles of table and nothing else. When i manually refresh it from browser then it displays back the records list.I want to load the list automatically after the record is deleted.

Summary: On delete i want to load / refresh the list automatically which is not happening right now.

Student Controller:

public function destroy($id)
    {
        $student = Student::find(Input::get('id'));
        if($student){
            $student->delete();
            return Response::json(['destroy'=>true]);
        }
    }

app.js

var myApp = angular.module('myApp', ['mainCtrl', 'myAppService']);

myappservice.js

angular.module('myAppService', [])

    .factory('Result', function($http) {
        return {
            get : function() {
                return $http.get('api/result');
            },
            show : function(id) {
                return $http.get('api/result/' + id);
            },
            save : function(resultData) {
                return $http({
                    method: 'POST',
                    url: 'api/result',
                    headers: { 'Content-Type' : 'application/x-www-form-urlencoded' },
                    data: $.param(resultData)
                });
            },
            destroy : function(id) {
                return $http.delete('api/result/' + id,{params: {id}});
            }
        }

    });

MainCtrl.js

angular.module('mainCtrl', [])

    .controller('mainController', function($scope, $http, Result) {
        // object to hold all the data for the new comment form
        $scope.resultData = {};

        // loading variable to show the spinning loading icon
        $scope.loading = true;

        // get all the comments first and bind it to the $scope.comments object
        Result.get()
            .success(function(data) {
                $scope.students = data;
                $scope.loading = false;
            });


        // function to handle submitting the form
        $scope.submitResult = function() {
            $scope.loading = true;

            // save the comment. pass in comment data from the form
            Result.save($scope.resultData)
                .success(function(data) {
                    $scope.resultData = {};
                    // if successful, we'll need to refresh the comment list
                    Result.get()
                        .success(function(getData) {
                            $scope.students = getData;
                            $scope.loading = false;
                        });

                })
                .error(function(data) {
                    console.log(data);
                });
        };

        // function to handle deleting a comment
        $scope.deleteResult = function(id) {
            $scope.loading = true; 

            Result.destroy(id)
                .success(function(data) {

                    // if successful, we'll need to refresh the comment list
                    Result.get()
                        .success(function(getData) {
                            $scope.students = getData;
                            $scope.loading = false;
                        });

                });
        };

    });

View

 <table class="table table-striped">
                            <thead>
                              <tr>
                                <th>Roll No</th>
                                <th>Student Name</th>
                                <th>Father Name</th>
                                <th>Obtained Marks</th>
                                <th>Total Marks</th>
                                <th>Percentage</th>
                                <th>Delete</th>
                              </tr>
                            </thead>
                            <tbody ng-hide="loading" ng-repeat="student in students | filter:searchText">
                                  <tr> 
                                    <td>@{{ student.rollno }}</td>
                                    <td>@{{ student.name }}</td>
                                    <td>@{{ student.fname }}</td>
                                    <td>@{{ student.obtainedmarks }}</td>
                                    <td>@{{ student.totalmarks }}</td>
                                    <td>@{{ student.percentage }}</td>
                                    <td>
                                        <a href="#" ng-click="deleteResult(student.id)" class="text-muted btn-danger btn-lg">Delete</a></p>
                                    </td>

                                  </tr>
                            </tbody>
</table>

Console Error: DELETE http://localhost/ngresulty/public/api/result/30?id=30 500 (Internal Server Error)

Upvotes: 0

Views: 3404

Answers (3)

Krittikorn Deeraksa
Krittikorn Deeraksa

Reputation: 21

Try to put $scope.$apply(); statement at the end of event

Upvotes: 0

aseferov
aseferov

Reputation: 6393

if you get 500 server error Result.destroy() calls error function. Its not success

    $scope.loading = true; 

            Result.destroy(id)
                .success(function(data) {
                    // if successful, we'll need to refresh the comment list
                    Result.get()
                        .success(function(getData) {
                            $scope.students = getData;
                            $scope.loading = false;
                        });

                })
                .error(function(data) {
                    // its not successful but i want call Result.get() anyway
                    Result.get()
                        .success(function(getData) {
                            $scope.students = getData;
                            $scope.loading = false;
                        });

                }));
        };

Upvotes: 0

nikk wong
nikk wong

Reputation: 8670

My guess would be that the Result.get() call is not returning the right data. You may want to check that endpoint.

Instead of making another call though, since you know that on deletion there was a success event, you could just change the scope then and not call your backend again, ie:

    // function to handle deleting a comment
    $scope.deleteResult = function(id) {
        $scope.loading = true; 

        Result.destroy(id)
            .success(function(data) {

                // do something with data if you want to
                $scope.students.splice(id, 1);

            });
    };

Upvotes: 1

Related Questions