ng-grid单击事件回调

时间:2022-06-25 19:41:23

I am using ng-grid and trying to set a callback for when a cell gets clicked. In that callback, I want to know the row and column of the cell. The best way I found is to use cellTemplate (and put an onclick event) in columnDefs for each column, which is not very DRY:

我正在使用ng-grid并尝试在单击单元格时设置回调。在该回调中,我想知道单元格的行和列。我找到的最好的方法是在columnDefs中为每个列使用cellTemplate(并放置一个onclick事件),这不是很干:

$scope.gridOptions = { 
    data: 'myData',
    columnDefs: [{field: 'name', displayName: 'Name',
                  cellTemplate: '<div onclick="...">{{row.entity[col.field]}}</div>'
                  },
                  {field: 'age', displayName: 'Age', cellTemplate: ...}
                ]
};

This is problematic too, since ng-click doesn't work, and I can't use the controller's scope with onclick..

这也是有问题的,因为ng-click不起作用,我不能使用onclick控制器的范围。

Non ng-grid solutions are welcome too..

非ng-grid解决方案也是受欢迎的..

1 个解决方案

#1


2  

Unfortunately, ng-grid doesn't have a very good concept of returning the column number.

不幸的是,ng-grid没有很好的返回列号的概念。

If you can get by with just row number, the below plunker should show you how to do it:

如果您只使用行号,下面的plunker应该告诉您如何操作:

http://plnkr.co/edit/jYZWV9FpCdeENIETLEXV?p=preview

You use the beforeSelectionChange function, which includes the rowItem. That has the row number and such.

您使用beforeSelectionChange函数,其中包含rowItem。那有行号等。

The other option is to use the cell template to add a data attribute to every cell with the row/column number, then setup an event listener for the click even on that. If's still using a lot of columnDefs and own't be very DRY, but it will be a bit more DRY than what you've got now.

另一个选项是使用单元格模板向具有行/列编号的每个单元格添加数据属性,然后为该单击设置事件侦听器。如果仍然使用很多columnDef并且不是非常干,但它会比你现在的干得多。

#1


2  

Unfortunately, ng-grid doesn't have a very good concept of returning the column number.

不幸的是,ng-grid没有很好的返回列号的概念。

If you can get by with just row number, the below plunker should show you how to do it:

如果您只使用行号,下面的plunker应该告诉您如何操作:

http://plnkr.co/edit/jYZWV9FpCdeENIETLEXV?p=preview

You use the beforeSelectionChange function, which includes the rowItem. That has the row number and such.

您使用beforeSelectionChange函数,其中包含rowItem。那有行号等。

The other option is to use the cell template to add a data attribute to every cell with the row/column number, then setup an event listener for the click even on that. If's still using a lot of columnDefs and own't be very DRY, but it will be a bit more DRY than what you've got now.

另一个选项是使用单元格模板向具有行/列编号的每个单元格添加数据属性,然后为该单击设置事件侦听器。如果仍然使用很多columnDef并且不是非常干,但它会比你现在的干得多。