首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从angular-ui-grid更新数据库

从angular-ui-grid更新数据库
EN

Stack Overflow用户
提问于 2018-01-03 03:30:03
回答 1查看 546关注 0票数 0

当用户单击angular ui-grid中的delete按钮时,我正在尝试更改数据库中的一行。单击该按钮时,应从网格中删除该行,并使用值更新数据库中的表,以便该行不再出现在网格中,但它仍将在数据库中(软删除)。我的表名为申请者,包含id、名字、姓氏、电子邮件、职位、简历和已删除的行。Deleted是一个布尔列,默认为false,如果为true,则该行不应出现在我的ui网格中。我已经完成了这一部分,但是当按下网格上的按钮时,我无法获得更新数据库中已删除列的代码。这是网格的控制器(从数据库中获取申请者):

代码语言:javascript
复制
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);
    })
}])

当它到达post路由时,此代码将运行:

代码语言:javascript
复制
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}})
    })
}

这工作得很好,唯一的问题是我有硬编码的ID。有没有一种方法可以将网格中选定行的ID传递给这个变量?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-03 04:36:24

单击按钮时发送http请求

代码语言:javascript
复制
    $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);
      })
  }

您需要安装正文解析器并将其用作中间件(app.use(bodyParser.urlencoded({ extended: false }))来获取POST正文。

代码语言:javascript
复制
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}})
    })
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48066718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档