Wrap kendo-grid in an custom directive #306
Comments
Answered on Stack Overflow. |
mishoo, the problem with this solution is that the default options are configured on the controller, and I can not reuse the grid settings, such as sortable, resizable, pageable, etc... |
@rgiosa I'm not sure I understand what you mean, can you detail? If you'd like for instance to setup defaults for selectable, sortable and pageable in your custom directive you can easily do it (similar to how we modify columns), for example: // in directive's link function
if (!options.hasOwnProperty("pageable"))
options.pageable = true;
if (!options.hasOwnProperty("sortable"))
options.sortable = true; but again, I probably don't understand what you'd like to do… |
mishoo, this is exactly what I want! The only point now is that you can not access the grid with $scope.grid. |
You can access it, but not in the directive because the widget is not instantiated at this point. You can do something like this in the controller: $scope.$on("kendoWidgetCreated", function(ev, widget){
if (widget === $scope.grid) {
// $scope.grid is available now
}
}); The |
mishoo, actually I meant that the $scope.grid is not accessible in the controller. |
Of course it's not — the controller defines the data that your grid is created from. Since that data is only available in scope after the controller runs, we obviously must build the widget after the controller runs. Using the event I that described above you can execute some code right on the moment when the widget becomes available. Hope this clarifies… |
mishoo, imagine that I need to use the code below:
We still have to use the kendoWidgetCreated event? |
Nope, most probably not. But it depends on when |
mishoo, i forked your example here: http://embed.plnkr.co/jyjtU3SEEDYsa2CFRDTo/preview the $scope.grid is not working... |
Ah, of course, that's because our directive creates a nested scope, and the widget is not visible in the outer scope (which is where that controller runs in). Welcome to AngularJS complexities. ;-) Here's the fixed sample: http://plnkr.co/edit/uq62XqadunzBKJOrR9uT?p=preview It's using |
Let's say I want to change 'resizable' value in the directive'.
Is there a way to make that work? |
mishoo, in fact the changes made in the directive options is not working... =/ I believe this is an important point, since large applications grids always follow the same pattern. |
I guess option attributes need to be defined in the compile function of the directive. |
@rgiosa that was because in fact we made a copy of the options (with angular.extend) in the directive. I fixed it, note that now sortable, pageable and selectable are set from the directive and it works: http://plnkr.co/edit/uq62XqadunzBKJOrR9uT?p=preview |
@mishoo |
With reference to your latest plunkr, the checkbox selection is independent of the grid's row selections. How can you keep these two in sync, especially when multiple row selection is enabled on the grid? |
@mishoo : your comment #306 (comment) helped me resolve my problem! All kendo angular documentation mentions that the variable passed to the kendo-grid directive as |
@i2shar it's not related to Kendo, it's just how Angular scope works. This document explains in much detail what's going on, and you should read it if you're using Angular (especially if you're defining custom directives). |
@mishoo: Your directive from http://plnkr.co/edit/uq62XqadunzBKJOrR9uT?p=preview is what I was looking for. However, I noticed that the directive have "scope:true", which make it inherits from the parent scope per my understanding. As a result, when I tried to use this directive more than once (in my case i used two times since I have two kendo grid on my view from) in the same controller it doesn't work. Somehow it created two columns/sets of the checkboxes. I tried to change the "scope: true" to "scope: {}", but it didn't work. I forked your directive into my plunker and attempted to create an insolated scope by changing it to "scope: { mygridoptions: "="}" and pass in "my gridoptions" attribute. It works to the point that it has the checkboxes, but it couldn't invoke the selectAll function. Do you have any suggestions on how to update your directive to use isolated scope so I can use that directive more than once in the same controller? |
I want to create some basic options to reuse and i think that create a custom directive to wrap a kendo-grid is the way to go.
My custom directive has to offer a new checkbox column to select one row or all rows.
I already have some methods to manipulating selected rows, but my intention is to make this methods a part of directive.
Is this possible?
The text was updated successfully, but these errors were encountered: