I'm a newbie in angularJS
我是angularJS的新手
I use angularJS with ui-router and a slickgrid I try to call a method from a click on a image to call a web service and delete the row
我使用带有ui-router和slickgrid的angularJS我尝试通过单击图像调用方法来调用Web服务并删除该行
I have this controller :
我有这个控制器:
app.controller('customerController', ['$scope', '$compile', 'CustomerService', function ($scope, $compile, CustomerService) {
// function to get customers
$scope.getCustomers = function () {
CustomerService.getCustomers($("#userId").val(), $scope.getArchived).then(function (customers) {
dataView.beginUpdate();
dataView.setItems(customers);
dataView.endUpdate();
grid.invalidate();
}, function (reason) {
noty({ text: reason, type: 'error' });
});
}
// function to archive customer
$scope.archiveCustomer = function(customerId) {
if (confirm(translate("LBL_DELETE_CUSTOMER_CONFIRMATION"))) {
CustomerService.archiveCustomer(customerId).then(function () {
$scope.getCustomers();
}, function (reason) {
noty({ text: reason, type: 'error' });
});
}
return false;
}
// create grid
var dataView,
grid;
var options = {
enableCellNavigation: true,
enableColumnReorder: false,
autoHeight: true
};
var columns = [
{ id: "id", name: "", field: "id", width: 20, formatter: deleteFormatter, cssClass: "centeredColumn" },
{ id: "customer", name: translate("Customer"), field: "customer", width: 250 }
];
function deleteFormatter(row, cell, value, columnDef, dataContext) {
html = "<img src='images/delete.gif' alt='" + translate("LBL_DELETE_CUSTOMER") + "' style='cursor:pointer;' ng-click='archiveCustomer(" + dataContext.id + ");' />";
linker = $compile(angular.element(html));
htmlElements = linker($scope);
html = htmlElements[0].outerHTML;
return html;
}
dataView = new Slick.Data.DataView({ inlineFilters: true });
grid = new Slick.Grid("#customersGrid", dataView, columns, options);
$scope.getCustomers();
}]);
My problem is with the ng-click on the img element added by the deleteFormatter. What is wrong? I saw sample with directive but it's not clear in my mind... could you light me?
我的问题是ng-click由deleteFormatter添加的img元素。哪里不对?我看到了带有指令的样品,但在我看来并不清楚......你能点亮我吗?
Edit
Try with asyncPostRender without success: columns definition :
尝试使用asyncPostRender但没有成功:列定义:
var columns = [
{ id: "id", name: "", field: "id", width: 20, formatter: asyncFormatter, asyncPostRender: deleteRender, cssClass: "centeredColumn" },
{ id: "customer", name: translate("Customer"), field: "customer", width: 250 }
];
formatter and render:
格式化和渲染:
function asyncFormatter(row, cell, value, columnDef, dataContext) {
return "Data loading...";
}
function deleteRender(cellNode, row, dataContext, colDef) {
html = "<img src='images/delete.gif' alt='" + translate("LBL_DELETE_CUSTOMER") + "' style='cursor:pointer;' ng-click='archiveCustomer(" + dataContext.id + ");' />";
linker = $compile(angular.element(html));
htmlElements = linker($scope);
$(cellNode).empty()
cellNode.innerHTML = htmlElements[0].outerHTML;
}
2 个解决方案
#1
1
UPDATED
"formatter" seem to be just a configuration function of given column. You need to use a render function for that column like "asyncPostRender". See this example.
“formatter”似乎只是给定列的配置函数。您需要为该列使用渲染函数,如“asyncPostRender”。看这个例子。
Your new element (delete img) is out of any Angular scope. You need to $compile it into Angular in the render event of grid row.
您的新元素(删除img)超出任何Angular范围。你需要在网格行的渲染事件中将它编译成Angular。
#2
1
The answer with the help of Kursad Gulseven (Thank you) and this post
答案在Kursad Gulseven(谢谢)和这篇文章的帮助下
The controller declaration :
控制器声明:
app.controller('customerController', ['$scope', '$rootScope', '$compile', '$interpolate', 'CustomerService', function ($scope, $rootScope, $compile, $interpolate, CustomerService) {
Columns definition :
列定义:
var columns = [
{ id: "id", name: "", field: "id", width: 20, formatter: deleteFormatter, asyncPostRender: deleteRender, cssClass: "centeredColumn" },
{ id: "customer", name: translate("Customer"), field: "customer", width: 250 }
];
formatter and render:
格式化和渲染:
function deleteFormatter(row, cell, value, columnDef, dataContext) {
return "<img src='images/delete.gif' alt='delete' style='cursor:pointer;' ng-click='archiveCustomer(" + dataContext.id + ");' />";
}
function deleteRender(cellNode, row, dataContext, colDef) {
// compile the html with angular to get ng-click with the correct scope
var interpolated = $interpolate($(cellNode).html())($scope);
var linker = $compile(interpolated);
var htmlElements = linker($scope);
$(cellNode).empty()
$(cellNode).append(htmlElements);
}
the archive customer function :
存档客户功能:
$scope.archiveCustomer = function (customerId) {
if (confirm(translate("LBL_DELETE_CUSTOMER_CONFIRMATION"))) {
CustomerService.archiveCustomer(customerId).then(function () {
$scope.getCustomers();
}, function (reason) {
noty({ text: reason, type: 'error' });
});
}
}
#1
1
UPDATED
"formatter" seem to be just a configuration function of given column. You need to use a render function for that column like "asyncPostRender". See this example.
“formatter”似乎只是给定列的配置函数。您需要为该列使用渲染函数,如“asyncPostRender”。看这个例子。
Your new element (delete img) is out of any Angular scope. You need to $compile it into Angular in the render event of grid row.
您的新元素(删除img)超出任何Angular范围。你需要在网格行的渲染事件中将它编译成Angular。
#2
1
The answer with the help of Kursad Gulseven (Thank you) and this post
答案在Kursad Gulseven(谢谢)和这篇文章的帮助下
The controller declaration :
控制器声明:
app.controller('customerController', ['$scope', '$rootScope', '$compile', '$interpolate', 'CustomerService', function ($scope, $rootScope, $compile, $interpolate, CustomerService) {
Columns definition :
列定义:
var columns = [
{ id: "id", name: "", field: "id", width: 20, formatter: deleteFormatter, asyncPostRender: deleteRender, cssClass: "centeredColumn" },
{ id: "customer", name: translate("Customer"), field: "customer", width: 250 }
];
formatter and render:
格式化和渲染:
function deleteFormatter(row, cell, value, columnDef, dataContext) {
return "<img src='images/delete.gif' alt='delete' style='cursor:pointer;' ng-click='archiveCustomer(" + dataContext.id + ");' />";
}
function deleteRender(cellNode, row, dataContext, colDef) {
// compile the html with angular to get ng-click with the correct scope
var interpolated = $interpolate($(cellNode).html())($scope);
var linker = $compile(interpolated);
var htmlElements = linker($scope);
$(cellNode).empty()
$(cellNode).append(htmlElements);
}
the archive customer function :
存档客户功能:
$scope.archiveCustomer = function (customerId) {
if (confirm(translate("LBL_DELETE_CUSTOMER_CONFIRMATION"))) {
CustomerService.archiveCustomer(customerId).then(function () {
$scope.getCustomers();
}, function (reason) {
noty({ text: reason, type: 'error' });
});
}
}