I am developping a table of inputs.
What I want is : when use presses '+' key (wherever the cursor is in the table row), the app adds a new line in the table.
It works fine doing this :
<tr ng-repeat="sell in sells" ng-keypress="newLine($event)">
My problem is that when user presses tab key in an input of the row to go to next input, the next input value is highlighted (which is the normal behaviour of tab key).
Then if user presses '+' to add a new line, it replaces the value of the input by the '+' sign.
I have set up a directive to allow user only to type number in the inputs, but it doesnt work when the input value is highlighted.
angular.module('myApp').directive('numbersOnly', function($timeout) {
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
// this next if is necessary for when using ng-required on your input.
// In such cases, when a letter is typed first, this parser will be called
// again, and the 2nd time, the value will be undefined
if (inputValue == undefined) return ''
var transformedInput = inputValue.replace(/[^0-9.]+/g, '');
if (transformedInput!=inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
};
});
<td style="width:59px"><input type="text" ng-model="sell.quantity" numbers-only enter-as-tab ></td>
If someone knows a way to prevent user from replacing highlighted value by the '+'.... or to disable the default behaviour of tab key.
Thanks in advance.
You can override the default action of the '+' key using a custom directive.
module.directive('overridePlusKey', ['$window', function ($window) {
// Linker function
return function (scope, element, attrs) {
element.bind('keydown', function (e) {
var keyCode = e.keyCode || e.which;
console.log(keyCode);
if (keyCode == 107 || keyCode == 187)
{
e.preventDefault();
// Your code here to add a row
}
});
};
}]);
Then apply it to the inputs like so:
<input type="text" ng-model="content" override-plus-key />
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments