add/remove functionality on keyDown added

This commit is contained in:
Robert
2017-07-05 10:15:23 +02:00
committed by Robert
parent c696c62185
commit 91646212fd
2 changed files with 33 additions and 3 deletions

View File

@@ -11,7 +11,7 @@
if (!$scope.model.value) {
$scope.model.value = [];
}
//add any fields that there isn't values for
if ($scope.model.config.min > 0) {
for (var i = 0; i < $scope.model.config.min; i++) {
@@ -21,13 +21,43 @@
}
}
//TODO remove text box only when is empty
$scope.addRemoveOnKeyDown = function (event, index) {
console.log("Index: " + index);
console.log($scope.model.value);
console.log("KeyCode: " + event.keyCode);
var txtBoxValue = $scope.model.value[index];
console.log(txtBoxValue.value);
switch (event.keyCode) {
case 13:
if ($scope.model.config.max <= 0 || $scope.model.value.length < $scope.model.config.max) {
$scope.model.value.push({ value: "" });
}
break;
case 8:
var remainder = [];
if ($scope.model.value.length > 1) {
if (txtBoxValue.value === "") {
for (var x = 0; x < $scope.model.value.length; x++) {
if (x !== index) {
remainder.push($scope.model.value[x]);
}
}
$scope.model.value = remainder;
}
}
break;
default:
}
}
$scope.add = function () {
if ($scope.model.config.max <= 0 || $scope.model.value.length < $scope.model.config.max) {
$scope.model.value.push({ value: "" });
}
};
$scope.remove = function(index) {
$scope.remove = function (index) {
var remainder = [];
for (var x = 0; x < $scope.model.value.length; x++) {
if (x !== index) {

View File

@@ -3,7 +3,7 @@
<div ui-sortable="sortableOptions" ng-model="model.value">
<div class="control-group" ng-repeat="item in model.value">
<i class="icon icon-navigation handle"></i>
<input type="text" name="item_{{$index}}" ng-model="item.value" class="umb-editor" />
<input type="text" name="item_{{$index}}" ng-model="item.value" class="umb-editor" ng-keydown="addRemoveOnKeyDown($event, $index)"/>
<a prevent-default href="" localize="title" title="@content_removeTextBox"
ng-show="model.value.length > model.config.min"
ng-click="remove($index)">