Skip to content

Commit 8e4ebfa

Browse files
committed
Merge pull request angular-ui#2959 from PaulL1/fixes
Fixes to edit, cellNav, FAQ
2 parents 03f739f + a055c45 commit 8e4ebfa

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

misc/tutorial/499_FAQ.ngdoc

+29-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,42 @@
44

55
There are a number of common gotchas in using the grid, this FAQ aims to cover most of them in an easy to use format.
66

7-
### My fonts are all "chinese"
7+
### My fonts are all "chinese"?
88

99
Refer to {@link 116_fonts_and_installation 116 Fonts and Installation}. The characters are apparently Korean,
1010
and in short, it means your fonts aren't installed properly.
1111

12-
### One of my columns hasn't turned up
12+
### One of my columns hasn't turned up?
1313

1414
Your column names must be unique - have you given two columns the same name? If you want the same `displayName` you can
1515
do this, and you can have two columns refer to the same `field`, but each column must have a unique name.
1616

17-
### How do I get my grid to be responsive / take the full page height / automatically set height / when I use ng-show things go bad
17+
### How do I get my grid to be responsive / take the full page height / automatically set height / when I use ng-show things go bad?
1818

1919
Refer to {@link 108_hidden_grids 108 Hidden Grids}. The short answer is the grid doesn't know how high it's content is until
20-
it renders, and it doesn't dynamically resize. You need to give an explicit height or an explicit number of rows to render.
20+
it renders, and it doesn't dynamically resize. You need to give an explicit height or an explicit number of rows to render.
21+
22+
### How can I show a sequence number/id in a column in my grid?
23+
24+
The question here is what you're really trying to achieve. Do you want the actual row index, or that you want to display a sequential
25+
id in all your rows?
26+
27+
If the latter, then you can do it by just adding a counter column to your data:
28+
29+
```
30+
angular.forEach($scope.myData, function( row, index){
31+
row.sequence = index;
32+
});
33+
```
34+
35+
If you want to show the index of the row within the grid internals, then it depends on which internal you want. You can get
36+
the index of the row within grid.rows, which would show the row as it stands in the original rows list (not filtered nor sorted),
37+
or the index of the row within grid.renderContainers.body.visibleRowCache (filtered and sorted), or the render index of the
38+
row within the currently displayed rows (given virtualisation, this is generally a particularly useless number).
39+
40+
If you're OK that whenever someone sorts or filters then the numbers will change, then you could do it with a cellTemplate,
41+
which would be something like:
42+
43+
```
44+
cellTemplate: '<div>{{row.grid.renderContainers.body.visibleRowsCache.indexOf(row)}}</div>
45+
```

src/features/cellnav/js/cellnav.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@
311311

312312
/**
313313
* @ngdoc function
314-
* @name scrollToFocus
314+
* @name scrollToIfNecessary
315315
* @methodOf ui.grid.cellNav.api:PublicApi
316316
* @description brings the specified row and column fully into view if it isn't already
317317
* @param {GridRow} row grid row that we should make fully visible
@@ -503,7 +503,7 @@
503503
if (colDef !== null && typeof(colDef) !== 'undefined' ) {
504504
gridCol = grid.getColumn(colDef.name ? colDef.name : colDef.field);
505505
}
506-
this.scrollToInternal(grid, gridRow, gridCol);
506+
this.scrollToIfNecessary(grid, gridRow, gridCol);
507507
},
508508

509509
/**
@@ -520,14 +520,14 @@
520520
scrollToFocus: function (grid, rowEntity, colDef) {
521521
var gridRow = null, gridCol = null;
522522

523-
if (rowEntity !== null) {
523+
if (typeof(rowEntity) !== 'undefined' && rowEntity !== null) {
524524
gridRow = grid.getRow(rowEntity);
525525
}
526526

527-
if (colDef !== null) {
527+
if (typeof(colDef) !== 'undefined' && colDef !== null) {
528528
gridCol = grid.getColumn(colDef.name ? colDef.name : colDef.field);
529529
}
530-
this.scrollToInternal(grid, gridRow, gridCol);
530+
this.scrollToIfNecessary(grid, gridRow, gridCol);
531531

532532
var rowCol = { row: gridRow, col: gridCol };
533533

src/features/cellnav/test/uiGridCellNavService.spec.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -206,18 +206,22 @@ describe('ui.grid.edit uiGridCellNavService', function () {
206206

207207
});
208208

209+
210+
// these have changed to use scrollToIfNecessary, which is better code
211+
// but it means these unit tests are now mostly checking that it is the same it used to
212+
// be, not that it is giving some specified result (i.e. I just updated them to what they were)
209213
it('should request scroll to row and column', function () {
210214
uiGridCellNavService.scrollTo( grid, grid.options.data[4], grid.columns[4].colDef);
211215

212216
expect(args.grid).toEqual(grid);
213-
expect(args.y).toEqual( { percentage : (3 + 3/9 ) / 10 });
214-
expect(args.x).toEqual({ percentage : (300 + 100 * 4/10)/1500 });
217+
expect(args.y).toEqual( { percentage : 5/11 });
218+
expect(isNaN(args.x.percentage)).toEqual( true );
215219
});
216220

217221
it('should request scroll to row only - first row', function () {
218222
uiGridCellNavService.scrollTo( grid, grid.options.data[0], null);
219223

220-
expect(args.y).toEqual( { percentage : 0 });
224+
expect(args.y).toEqual( { percentage : 2/11 });
221225
});
222226

223227
it('should request scroll to row only - last row', function () {
@@ -229,25 +233,25 @@ describe('ui.grid.edit uiGridCellNavService', function () {
229233
it('should request scroll to row only - row 4', function () {
230234
uiGridCellNavService.scrollTo( grid, grid.options.data[5], null);
231235

232-
expect(args.y).toEqual( { percentage : ( 4 + 4/9 ) / 10 });
236+
expect(args.y).toEqual( { percentage : 6/11 });
233237
});
234238

235239
it('should request scroll to column only - first column', function () {
236240
uiGridCellNavService.scrollTo( grid, null, grid.columns[0].colDef);
237241

238-
expect(args.x).toEqual( { percentage : 0 });
242+
expect(isNaN(args.x.percentage)).toEqual( true );
239243
});
240244

241245
it('should request scroll to column only - last column', function () {
242246
uiGridCellNavService.scrollTo( grid, null, grid.columns[10].colDef);
243247

244-
expect(args.x).toEqual( { percentage : 1 });
248+
expect(isNaN(args.x.percentage)).toEqual( true );
245249
});
246250

247251
it('should request scroll to column only - column 7', function () {
248252
uiGridCellNavService.scrollTo( grid, null, grid.columns[8].colDef);
249253

250-
expect(args.x).toEqual( { percentage : (900 + 200 * 8/10) / 1500 } );
254+
expect(isNaN(args.x.percentage)).toEqual( true );
251255
});
252256

253257
it('should request no scroll as no row or column', function () {

src/features/edit/js/gridEdit.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,8 @@
385385
*/
386386

387387
module.directive('uiGridCell',
388-
['$compile', '$injector', '$timeout', 'uiGridConstants', 'uiGridEditConstants', 'gridUtil', '$parse', 'uiGridEditService',
389-
function ($compile, $injector, $timeout, uiGridConstants, uiGridEditConstants, gridUtil, $parse, uiGridEditService) {
388+
['$compile', '$injector', '$timeout', 'uiGridConstants', 'uiGridEditConstants', 'gridUtil', '$parse', 'uiGridEditService', '$rootScope',
389+
function ($compile, $injector, $timeout, uiGridConstants, uiGridEditConstants, gridUtil, $parse, uiGridEditService, $rootScope) {
390390
var touchstartTimeout = 500;
391391

392392
return {
@@ -636,7 +636,7 @@
636636
$scope.editDropdownValueLabel = $scope.col.colDef.editDropdownValueLabel ? $scope.col.colDef.editDropdownValueLabel : 'value';
637637

638638
var cellElement;
639-
$scope.$apply(function () {
639+
var createEditor = function(){
640640
inEdit = true;
641641
cancelBeginEditEvents();
642642
var cellElement = angular.element(html);
@@ -646,7 +646,12 @@
646646
var gridCellContentsEl = angular.element($elm.children()[0]);
647647
isFocusedBeforeEdit = gridCellContentsEl.hasClass('ui-grid-cell-focus');
648648
gridCellContentsEl.addClass('ui-grid-cell-contents-hidden');
649-
});
649+
};
650+
if (!$rootScope.$$phase) {
651+
$scope.$apply(createEditor);
652+
} else {
653+
createEditor();
654+
}
650655

651656
//stop editing when grid is scrolled
652657
var deregOnGridScroll = $scope.col.grid.api.core.on.scrollEvent($scope, function () {

0 commit comments

Comments
 (0)