2014-02-12 42 views

回答

3

我分叉和更新您的Plunker。我实际上使用自定义指令做了同样的表。如果你正在做一个表,你应该使用语义标记。这就是我修改你的HTML的原因。

然后,我更新了select-me指令,向您展示我如何在点击时完成自动选择以及如何实现箭头键导航。

Updated Plunker

的layout.html

<table> 
    <thead> 
    <tr ng-repeat="element in header" class="header-cells" style="width:{{element.width}}px"> 
     <th>{{element.column}}</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr ng-repeat="element in body" data-indexrow="{{$index}}"> 
     <td ng-repeat="h in header" class="custom-row" data-indexcol="{{$index}}"> 
     <input type="text" ng-model="element[h.column]" class="body-cell" style="width:{{h.width}}px" select-me> 
     </td> 
    </tr> 
    </tbody> 
</table> 

selectMe指令(JS)

ct.directive("selectMe", function() { 
    return ({ 
    restrict: "A", 
    // templateUrl: 'sortdropdown.html', 
    link: link 
    }); 

    function link(scope, element, attributes) { 

    element.on('click', function(e) { 
     element.select(); 
    }); 

    element.on('keyup', function(e) { 
     var $input = angular.element(this), 
      $td = $input.parent(), 
      $tr = $td.parent(), 
      indexrow = parseInt($tr.attr('data-indexrow'),10), 
      indexcol = parseInt($td.attr('data-indexcol'),10); 

     console.log(indexrow); 
     console.log(indexcol); 

     // up arrow 
     if (e.keyCode === 38) { 
     //move up 
     } 
     // down arrow 
     if (e.keyCode === 40) { 
     //move down 
     } 
     // left arrow 
     if (e.keyCode === 37) { 
     //move left 
     } 
     // right arrow 
     if (e.keyCode === 39) { 
     //move right 
     } 
    }); 
    } 
}); 
+0

感谢glepretre ..这看起来很简单和工程 – runtimeZero

+0

@ glepretre..could你也建议我如何介绍细胞之间的运动? – runtimeZero

+0

@JamesHans我在行和列上添加了属性来为它们编制索引。我向你展示了如何让他们参与你的指令。我让你得到正确的输入,继续使用jQlite或jQuery,并处理特定的情况(第一行和最后一行,列同义)。 ;)别忘了把我投票给我! :p – glepretre