How to make individual items in a table selectable in angularjs

自闭症网瘾萝莉.ら 提交于 2019-12-24 07:03:07

问题


I have a table using ng-repeat... I want to be able to allow the users to select an item in the table using the key 'enter' which is equal to the keydown event 13. When a user clicks enter, I want to be able to focus in on the specific table row and make the variable 'selected' to true. When the user clicks on enter the second time, I want the 'selected' field to be set to false.

Here is what I have so far, any ideas?

html:

<tbody ng-model="educators">
    <tr ng-keydown="keydownevt()" tabindex="0" class="inventor-row"
        ng-repeat="ed in educators">
        <td class="inventor-col">
          <div><strong>{{ed.lastName}}, {{ed.firstName}}</strong></div>
          <div>{{ed.address}}</div>
          <div>{{ed.country}}</div>
        </td>
    </tr>
</tbody>

JS:

$scope.keydownevt = function () {
  $scope.keydownkeycode = event.keyCode;
  var selected = false;
  if (event.keyCode === 13) {
    // add focus
    return !selected;
  }
};

回答1:


Well, you didn't show where you want to store that selected value and an ng-model in tbody tag is really awkward, let me try to help

<tr ng-keydown="keydownevt(ed)" tabindex="0" ng-class="{selected: ed.selected}" class="inventor-row" ng-repeat="ed in educators">
        <td class="inventor-col">
          <div><strong>{{ed.lastName}}, {{ed.firstName}}</strong></div>
          <div>{{ed.address}}</div>
          <div>{{ed.country}}</div>
        </td>
      </tr>

$scope.keydownevt = function (ed) {
          $scope.keydownkeycode = event.keyCode;
          var selected = ed.selected || false;
          if (event.keyCode === 13) {
            // add focus
            return !selected;
          }
      };

Then you create a css class called ".selected" that paint that row or something



来源:https://stackoverflow.com/questions/44372932/how-to-make-individual-items-in-a-table-selectable-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!