-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #399, making cell editing work again
This is an issue with how src/ng-input.js is handling the value of the ng-input directive. The current default template contains an $eval, which means that when ng-input.js runs a $parse on it, it gets the evaluated value of the cell itself. For a string, (which is an object) this ends up not actually modifying the cell contents but at least it doesn't fire an exception. For an integer primitive you would end up with an undefined setter and a noop getter, which would throw exceptions when you try to use undefined as a function to set the cell value. The fix, as I see it, is to use a regex to strip out the $eval statement from the ng-input in the editable cell template. I've also cleaned up ng-input.js, added some comments, and added handling for when the enter key is used to modify a cell's contents
- Loading branch information
Showing
2 changed files
with
64 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,53 @@ | ||
ngGridDirectives.directive('ngInput',['$parse', function($parse) { | ||
return function ($scope, elm, attrs) { | ||
var getter = $parse($scope.$eval(attrs.ngInput)); | ||
return function ($scope, elm, attrs) { | ||
|
||
// Strip the $eval off of the ng-input tag, we want WHERE to put the edited values, not the actual edited/displayed value itself | ||
var inputter = attrs.ngInput.replace(/.+?\((.+?)\)/, '$1'); | ||
|
||
var evaled = $scope.$eval(inputter); | ||
var getter = $parse(evaled); | ||
var setter = getter.assign; | ||
var oldCellValue = getter($scope.row.entity); | ||
var oldCellValue = getter($scope); | ||
|
||
elm.val(oldCellValue); | ||
elm.bind('keyup', function() { | ||
var newVal = elm.val(); | ||
if (!$scope.$root.$$phase) { | ||
$scope.$apply(function(){setter($scope.row.entity,newVal); }); | ||
} | ||
}); | ||
|
||
elm.bind('keyup', function() { | ||
var newVal = elm.val(); | ||
// dump(newVal); | ||
if (!$scope.$root.$$phase) { | ||
$scope.$apply(function(){ | ||
setter($scope, newVal); | ||
}); | ||
} | ||
}); | ||
|
||
elm.bind('keydown', function(evt){ | ||
switch(evt.keyCode){ | ||
case 37: | ||
case 38: | ||
case 39: | ||
case 40: | ||
case 37: // Left arrow | ||
case 38: // Up arrow | ||
case 39: // Right arrow | ||
case 40: // Down arrow | ||
evt.stopPropagation(); | ||
break; | ||
case 27: | ||
case 27: // Esc (reset to old value) | ||
if (!$scope.$root.$$phase) { | ||
$scope.$apply(function(){ | ||
setter($scope.row.entity,oldCellValue); | ||
setter($scope, oldCellValue); | ||
elm.val(oldCellValue); | ||
elm.blur(); | ||
}); | ||
} | ||
case 13: // Enter (apply new value) | ||
if (!$scope.$root.$$phase) { | ||
$scope.$apply(function(){ | ||
setter($scope, elm.val()); | ||
elm.blur(); | ||
}); | ||
} | ||
default: | ||
break; | ||
} | ||
return true; | ||
}); | ||
}; | ||
}; | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9f78401
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this fix work with complex properties, like person.name? I think the eval logic had stopped that from working.
9f78401
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@swalters good question; I will add a test for that.
9f78401
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@swalters yep it works: 4a5c93d