如何在Angularjs中添加新行后以编程方式关注下一个单元格

时间:2017-12-13 12:29:31

标签: javascript angularjs html5

我是来自Knockout世界的角色新手。我已经连接到tab的按键事件,以便在表格的最后一个单元格中添加新内容。

我的问题是,焦点会转到uri栏中的信息图标,而不是新创建的行中的下一个单元格。我确信修复很简单我找不到解决方案,任何人都可以帮忙吗?

我正在使用Angularjs v1.59



var myApp = angular.module('myApp', []);
    myApp
    .factory('shippingItems', function () {
        return {
            data: [{
                quantity: 1,
                width: 1,
                height: 1,
                length: 1,
                weight: 1,
                details: 1
            },
            {
                quantity: 2,
                width: 2,
                height: 2,
                length: 2,
                weight: 2,
                details: 2
            },
            {
                quantity: 3,
                width: 3,
                height: 3,
                length: 3,
                weight: 3,
                details: 3
            }]
        };
    })
    .controller("userCtrl", ['$scope', 'shippingItems', function ($scope, shippingItems) {
            $scope.shippingItems = shippingItems.data;
    }])
    .directive("myTab", ['shippingItems', function (shippingItems) {
        return {
            link: function (scope, element, attrs) {
                scope.index = attrs.indexTracker;
                element.bind("keydown keypress", function (event) {
                    if (scope.index == (shippingItems.data.length-1)) {
                        if (event.which === 9) {
                            var newItem = {
                                quantity: null,
                                width: null,
                                height: null,
                                length: null,
                                weight: null,
                                details: null
                            };
                            shippingItems.data.push(newItem);
                            element.next().focus();
                        }
                    }
                });
            }
        }
    }]);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<div ng-app="myApp">
    <div ng-controller="userCtrl">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">lol</h3>
            </div>
            <div class="panel-body">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>Quantity</th>
                            <th>Width x Height x Length (cm)</th>
                            <th>Weight Per Item</th>
                            <th>Total Weight</th>
                            <th>Details</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="table-input" ng-repeat="item in shippingItems">
                            <td><input type="text" ng-model="item.quantity" /></td>
                            <td><input type="text" ng-model="item.width" /> x <input type="text" ng-model="item.height" /> x <input type="text" ng-model="item.length" /></td>
                            <td><input type="text" ng-model="item.weight" />kg</td>
                            <td>{{ (item.weight|number) * (item.quantity|number)}}kg</td>
                            <td my-tab="checking" index-tracker="{{$index}}"><input type="text" ng-model="item.details" /></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您实际上不需要directive service但是在此示例中,您可以看到如何使用angular.element从上一个$event传递input {1}}或li中的{1}}。

td
var app = angular.module("app", []);

app.controller("ctrl", function($scope, $timeout) {
  $scope.ul = [{}, {}, {}]

  $scope.callService = function(event, index) {
    if (event.keyCode === 13) {
      var nextInext = index + 1;

      if (angular.isUndefined($scope.ul[nextInext])) {
        $scope.ul.push({});
      }

      $timeout(function() {
        angular.element(event.target).closest(".parent").find(".child-" + nextInext + " input").select();
      })
    }
  }
})

答案 1 :(得分:0)

&#13;
&#13;
  var myApp = angular.module('myApp', []);
    myApp
    .factory('shippingItems', function () {
        return {
            data: [{
                quantity: 1,
                width: 1,
                height: 1,
                length: 1,
                weight: 1,
                details: 1
            },
            {
                quantity: 2,
                width: 2,
                height: 2,
                length: 2,
                weight: 2,
                details: 2
            },
            {
                quantity: 3,
                width: 3,
                height: 3,
                length: 3,
                weight: 3,
                details: 3
            }]
        };
    })
    .controller("userCtrl", ['$scope', 'shippingItems', function ($scope, shippingItems) {
            $scope.shippingItems = shippingItems.data;
    }])
    .directive("myTab", ['shippingItems', function (shippingItems,$timeout) {
        console.log('CALLED')
        return {
            link: function (scope, element, attrs) {
                element.bind("keydown", function (event) {
                event.preventDefault();
                scope.index = attrs.indexTracker;
                    if (scope.index == (shippingItems.data.length-1)) {
                        //if (event.keyCode === 81 && event.altKey) {
                        if (event.keyCode === 9) {
                            var newItem = {
                                quantity: null,
                                width: null,
                                height: null,
                                length: null,
                                weight: null,
                                details: null
                            };
                            shippingItems.data.push(newItem);
                            setTimeout(function(){
                                var tr_dom      = element[0].closest('tr').nextElementSibling
                                var input       = element[0].closest('tr').nextElementSibling.querySelector('input')
                                input.focus()
                             },10);
                            scope.$apply()
                        }
                    }
                });
            }
        }
    }]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
    <div ng-controller="userCtrl">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">lol</h3>
            </div>
            <div class="panel-body">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>Quantity</th>
                            <th>Width x Height x Length (cm)</th>
                            <th>Weight Per Item</th>
                            <th>Total Weight</th>
                            <th>Details</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="table-input" ng-repeat="item in shippingItems">
                            <td><input type="text" ng-model="item.quantity"/></td>
                            <td><input type="text" ng-model="item.width" /> x <input type="text" ng-model="item.height" /> x <input type="text" ng-model="item.length" /></td>
                            <td><input type="text" ng-model="item.weight" />kg</td>
                            <td>{{ (item.weight|number) * (item.quantity|number)}}kg</td>
                            <td ><input type="text" index-tracker="{{$index}}" ng-model="item.details" my-tab="checking"/></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;