当用户单击angular ui-grid中的delete按钮时,我正在尝试更改数据库中的一行。单击该按钮时,应从网格中删除该行,并使用值更新数据库中的表,以便该行不再出现在网格中,但它仍将在数据库中(软删除)。我的表名为申请者,包含id、名字、姓氏、电子邮件、职位、简历和已删除的行。Deleted是一个布尔列,默认为false,如果为true,则该行不应出现在我的ui网格中。我已经完成了这一部分,但是当按下网格上的按钮时,我无法获得更新数据库中已删除列的代码。这是网格的控制器(从数据库中获取申请者):
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路由时,此代码将运行:
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传递给这个变量?
发布于 2018-01-03 04:36:24
单击按钮时发送http请求
$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正文。
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}})
})
}https://stackoverflow.com/questions/48066718
复制相似问题