0

I am trying to change a row in my database when the user clicks the delete button in my angular ui-grid. When the button is clicked, the row should be removed from the grid, and the table in the database should be updated with a value so that the row will no longer appear in the grid, but it will still be in the database (soft-delete). My table is called applicants and has rows id, firstname, lastname, email, position, resume, and deleted. Deleted is a boolean column defaulted to false, if true, the row should not appear in my ui-grid. I have this portion completed, however I cannot get my code to update the deleted column in the database when the button is pressed on the grid. This is the controller for the grid (gets the applicants from the database):

angular.module('myApp')
.controller('gridController',['$scope','$http','$log',function ($scope, $http,$log){

    $scope.deleteRow = function(row) {
        var index = $scope.gridOptions.data.indexOf(row.entity);
        $scope.gridOptions.data.splice(index, 1);


        $http({
            method:'POST',
            url:'/directives/updateApplicant'
        })
    }

    $scope.gridOptions = {
        enableFiltering:true,
        columnDefs: [
            {name: 'id', field:'id'},
            {name:'firstName',field:'firstName'},
            {name:'lastName',field:'lastName'},
            {name:'email',field:'email'},
            {name:'position',field:'position'},
            {name:'resume', field:'resumeFileName', cellTemplate:'<div class="ui-grid-cell-contents"><a href="/directives/resume/{{COL_FIELD}}" target="_self">{{ COL_FIELD }}</a></div>'},
            {name:'delete',cellTemplate: '<button class="btn primary" ng-click="grid.appScope.deleteRow(row)">Delete</button>', enableFiltering:false}
        ]
    }

    $scope.data = []
    $http({
        method:'GET',
        url:'/directives/applicants'
    }).then(function(success){
        $scope.gridOptions.data = success.data;
    },function(reason){
        $scope.error = reason.data;
        $log.info(reason);
    })
}])

when it hits the post route this code runs:

module.exports.updateApplicant = function(req,res){
    applicant.forge({id: '31'})
    .fetch({require:true})
    .then(function(applicant){
        applicant.save({
            deleted:'true'
        })

    .then(function(){
        res.json({error:false,data:{message:'applicant details updated'}})
    })
    .catch(function(err){
        res.status(500).json({error:true, data:{message: err.message}})
    });
    })
    .catch(function(err){
        res.status(500).json({error:true,data:{message:err.message}})
    })
}

this works just fine the only problem is that i have the ID hardcoded. Is there a way i can pass the ID from the selected row in the grid to this variable?

4
  • show your server side code that is run when you use that route Commented Jan 2, 2018 at 19:39
  • @SunriseM when i use the get route? Commented Jan 2, 2018 at 19:39
  • When you press the button, you need to make a http request (should be POST) to your server to update the database Commented Jan 2, 2018 at 19:41
  • @SunriseM updated question Commented Jan 2, 2018 at 20:25

1 Answer 1

1

Send http request when you click button

    $scope.deleteRow = function(row) {
      var index = $scope.gridOptions.data.indexOf(row.entity);
      $scope.gridOptions.data.splice(index, 1);


      $http({
        method:'POST',
        url:'/directives/updateApplicant',
        data: $scope.gridOptions.data[index],
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
      }).success(function (data, status, headers, config) {
            console.log("success");
      }).error(function (data, status, headers, config) {
           console.log(status);
      })
  }

you need install body-parser and use it as middleware (app.use(bodyParser.urlencoded({ extended: false })) to get the POST body.

module.exports.updateApplicant = function(req,res){

    var bid = req.body.id;         

    applicant.forge({id: bid})
    .fetch({require:true})
    .then(function(applicant){
        applicant.save({
            deleted:'true'
        })

    .then(function(){
        res.json({error:false,data:{message:'applicant details updated'}})
    })
    .catch(function(err){
        res.status(500).json({error:true, data:{message: err.message}})
    });
    })
    .catch(function(err){
        res.status(500).json({error:true,data:{message:err.message}})
    })
}
Sign up to request clarification or add additional context in comments.

2 Comments

i tried this, i'm getting "TypeError: Converting circular structure to JSON"
i actually just figured it out right before you updated this. I ended up doing something different from what you just posted but I commented what I had out and tested yours to double check and it works so I will accept the answer. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.