Skip to content

Commit 66f3404

Browse files
committed
feat(cellTemplates): add MODEL_COL_FIELD to cellTemplate parsing so it can be used in ng-model #1633
Breaking Change: All editable cell templates must be changed to use MODEL_COL_FIELD
1 parent b807997 commit 66f3404

File tree

10 files changed

+66
-11
lines changed

10 files changed

+66
-11
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
<a name="v3.0.0-rc.NEXT"></a>
2+
### v3.0.0-rc.NEXT Current Master
3+
4+
#### Breaking Changes
5+
* editableCellTemplates should use MODEL_COL_FIELD instead of COL_FIELD.
6+
https://github.com/angular-ui/ng-grid/issues/1633 MODEL_COL_FIELD variable was added to the cellTemplate and editCellTemplate for utilizing the bound field in ng-model. Edit feature
7+
was changed to use MODEL_COL_FIELD instead of COL_FIELD for consistency.
8+
9+
110
<a name="v3.0.0-rc.11"></a>
211
### v3.0.0-rc.11 (2014-09-26)
312

misc/tutorial/106_binding.ngdoc

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ This tutorial shows two-way binding to properties with special characters, array
88

99
Note that a function can not be edited.
1010

11+
In your custom cellTemplates, you can use:
12+
<br/>
13+
COL\_FIELD which will be replaced with grid.getCellValue(row). This should be used for any cellTemplates that need readonly access to the field value
14+
<br/>
15+
MODEL\_COL\_FIELD which will be replaced with row.entity.field. Use MODEL\_COL\_FIELD anytime you need ng-model='field'
16+
17+
18+
1119
@example
1220
<example module="app">
1321
<file name="app.js">

src/features/edit/js/gridEdit.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@
497497
origCellValue = cellModel($scope);
498498

499499
html = $scope.col.editableCellTemplate;
500-
html = html.replace(uiGridConstants.COL_FIELD, $scope.row.getQualifiedColField($scope.col));
500+
html = html.replace(uiGridConstants.MODEL_COL_FIELD, $scope.row.getQualifiedColField($scope.col));
501501

502502
var optionFilter = $scope.col.colDef.editDropdownFilter ? '|' + $scope.col.colDef.editDropdownFilter : '';
503503
html = html.replace(uiGridConstants.CUSTOM_FILTERS, optionFilter);
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div>
22
<form name="inputForm">
3-
<input type="{{inputType}}" ng-class="'colt' + col.index" ui-grid-editor ng-model="COL_FIELD" />
3+
<input type="{{inputType}}" ng-class="'colt' + col.index" ui-grid-editor ng-model="MODEL_COL_FIELD" />
44
</form>
55
</div>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div>
22
<form name="inputForm">
3-
<select ng-class="'colt' + col.index" ui-grid-edit-dropdown ng-model="COL_FIELD" ng-options="field[editDropdownIdLabel] as field[editDropdownValueLabel] CUSTOM_FILTERS for field in editDropdownOptionsArray"></select>
3+
<select ng-class="'colt' + col.index" ui-grid-edit-dropdown ng-model="MODEL_COL_FIELD" ng-options="field[editDropdownIdLabel] as field[editDropdownValueLabel] CUSTOM_FILTERS for field in editDropdownOptionsArray"></select>
44
</form>
55
</div>

src/features/edit/test/uiGridCell.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('ui.grid.edit GridCellDirective', function () {
1515
$timeout = _$timeout_;
1616

1717
$templateCache.put('ui-grid/uiGridCell', '<div class="ui-grid-cell-contents">{{COL_FIELD CUSTOM_FILTERS}}</div>');
18-
$templateCache.put('ui-grid/cellEditor', '<div><input ng-model="COL_FIELD" ui-grid-editor /></div>');
18+
$templateCache.put('ui-grid/cellEditor', '<div><input ng-model="MODEL_COL_FIELD" ui-grid-editor /></div>');
1919

2020
scope = $rootScope.$new();
2121
var grid = gridClassFactory.createGrid();

src/features/edit/test/uiGridCellWithDropdown.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('ui.grid.edit GridCellDirective - with dropdown', function () {
1515
$timeout = _$timeout_;
1616

1717
$templateCache.put('ui-grid/uiGridCell', '<div class="ui-grid-cell-contents">{{COL_FIELD CUSTOM_FILTERS}}</div>');
18-
$templateCache.put('ui-grid/cellEditor', '<div><select ng-model="COL_FIELD" ui-grid-edit-dropdown ng-model="COL_FIELD" ng-options="field[editDropdownIdLabel] as field[editDropdownValueLabel] for field in editDropdownOptionsArray"></select></div>');
18+
$templateCache.put('ui-grid/cellEditor', '<div><select ng-model="MODEL_COL_FIELD" ui-grid-edit-dropdown ng-options="field[editDropdownIdLabel] as field[editDropdownValueLabel] for field in editDropdownOptionsArray"></select></div>');
1919

2020
scope = $rootScope.$new();
2121
var grid = gridClassFactory.createGrid();

src/js/core/constants.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
angular.module('ui.grid').constant('uiGridConstants', {
44
CUSTOM_FILTERS: /CUSTOM_FILTERS/g,
55
COL_FIELD: /COL_FIELD/g,
6+
MODEL_COL_FIELD: /MODEL_COL_FIELD/g,
67
DISPLAY_CELL_TEMPLATE: /DISPLAY_CELL_TEMPLATE/g,
78
TEMPLATE_REGEXP: /<.+>/,
89
FUNC_REGEXP: /(\([^)]*\))?$/,

src/js/core/factories/Grid.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,26 @@ angular.module('ui.grid')
386386
* @description precompiles all cell templates
387387
*/
388388
Grid.prototype.preCompileCellTemplates = function() {
389-
this.columns.forEach(function (col) {
390-
var html = col.cellTemplate.replace(uiGridConstants.COL_FIELD, 'grid.getCellValue(row, col)');
389+
var self = this;
390+
this.columns.forEach(function (col) {
391+
var html = col.cellTemplate.replace(uiGridConstants.MODEL_COL_FIELD, self.getQualifiedColField(col));
392+
html = html.replace(uiGridConstants.COL_FIELD, 'grid.getCellValue(row, col)');
391393

392-
var compiledElementFn = $compile(html);
393-
col.compiledElementFn = compiledElementFn;
394-
});
394+
395+
var compiledElementFn = $compile(html);
396+
col.compiledElementFn = compiledElementFn;
397+
});
398+
};
399+
400+
/**
401+
* @ngdoc function
402+
* @name getGridQualifiedColField
403+
* @methodOf ui.grid.class:Grid
404+
* @description precompiles all cell templates
405+
* @param {GridColumn} col col object
406+
*/
407+
Grid.prototype.getQualifiedColField = function (col) {
408+
return 'row.entity.' + gridUtil.preEval(col.field);
395409
};
396410

397411
/**

test/unit/core/factories/Grid.spec.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
describe('Grid factory', function () {
22
var $q, $scope, grid, Grid, GridRow, GridColumn, rows, returnedRows, column, uiGridConstants;
3+
var gridClassFactory;
34

45
beforeEach(module('ui.grid'));
56

6-
beforeEach(inject(function (_$q_, _$rootScope_, _Grid_, _GridRow_, _GridColumn_, _uiGridConstants_) {
7+
beforeEach(inject(function (_$q_, _$rootScope_, _Grid_, _GridRow_, _GridColumn_, _uiGridConstants_, _gridClassFactory_) {
78
$q = _$q_;
89
$scope = _$rootScope_;
910
Grid = _Grid_;
1011
GridRow = _GridRow_;
1112
GridColumn = _GridColumn_;
1213
uiGridConstants = _uiGridConstants_;
14+
gridClassFactory = _gridClassFactory_;
1315

1416
grid = new Grid({ id: 1 });
1517
rows = [
@@ -262,6 +264,27 @@ describe('Grid factory', function () {
262264

263265
});
264266

267+
it('should replace constants in template', inject(function ($timeout) {
268+
269+
var colDefs = [
270+
{name:'simpleProp', cellTemplate:'<div ng-model="MODEL_COL_FIELD"/>'}
271+
];
272+
var grid = gridClassFactory.createGrid({columnDefs:colDefs });
273+
var rows = [
274+
new GridRow(entity,1,grid)
275+
];
276+
277+
$timeout(function () {
278+
grid.buildColumns();
279+
});
280+
$timeout.flush();
281+
grid.modifyRows([entity]);
282+
grid.preCompileCellTemplates();
283+
284+
var row = grid.rows[0];
285+
expect(grid.getColumn('simpleProp').compiledElementFn).toBeDefined();
286+
287+
}));
265288

266289
it('should bind correctly to simple prop', function() {
267290

0 commit comments

Comments
 (0)