【问题标题】:Angular inline edit table singleton角内联编辑表单例
【发布时间】:2017-01-14 06:39:34
【问题描述】:

我正在尝试为表格创建内联编辑(类似于angular-xeditable),但我希望一次只允许编辑一行。目前您可以将任意数量的行置于编辑模式。

选项

  1. 将行列表保持在编辑模式,一旦选择要编辑的新行,循环遍历列表并强制退出编辑模式,将请求行置于编辑模式并将其添加到名单。这基本上允许列表中最多有 1 行,导致一次可编辑一行。但我没有说明如何让行退出编辑模式。

  2. 有一个退出编辑模式的取消按钮。所以也许使用jquery(或angular.element)来获取所有取消按钮的列表并务实地点击它们 - 再次,在编辑模式下强制退出行,但这可能会减慢大表的速度,因为它将点击取消甚至不在编辑模式下的行。

<table class="table table-striped">
<thead>
  <tr>
    <th id="th-name">Name</th>
    <th id="th-age">Age</th>
    <th id="th-actions">Actions</th>
  </tr>
</thead>
<tr ng-repeat="contact in model.contacts">
  <td>
    <span ng-show="edit != true">{{contact.name}}</span>
    <input ng-show="edit" type="text" ng-model="model.selected.name" class="form-control" placeholder="Name">
  </td>
  <td>
    <span ng-show="edit != true">{{contact.age}}</span>
    <input ng-show="edit" type="text" ng-model="model.selected.age" class="form-control" placeholder="Name"></td>
  <td>
    <button ng-show="edit != true" id="table-edit" ng-click="edit = true; editContact(contact)" uib-tooltip="Delete" tooltip-trigger="mouseenter" tooltip-placement="bottom"><i class="fa fa-fw fa-pencil"></i></button>
    <button ng-show="edit" id="table-save" ng-click="edit = false; saveContact($index)" uib-tooltip="Delete" tooltip-trigger="mouseenter" tooltip-placement="bottom"><i class="fa fa-fw fa-floppy-o"></i></button>
    <button ng-show="edit" id="table-cancel" ng-click="edit = false; reset()" uib-tooltip="Delete" tooltip-trigger="mouseenter" tooltip-placement="bottom"><i class="fa fa-fw fa-trash"></i></button>
  </td>
</tr>
</table>

这是一个 plunker 演示(允许所有行处于编辑模式):Plnkr Demo`

这是我想要实现的工作演示,但它使用模板。它根据我的喜好多次调用 getTemplate(每次运行摘要时调用它 - 单击按钮、键入、显示工具提示)。 Working Demo (Using templates)

【问题讨论】:

    标签: javascript css angularjs html-table inline-editing


    【解决方案1】:

    您可以使用 $index 变量(已内置到 ng-repeat 操作中)将 ng-repeat 操作的索引传递给您的 editContact 函数:

     <button class="edit-btn" ng-show="edit != true" id="table-edit" ng-click="edit = true; editContact(contact, $index);" uib-tooltip="Delete" tooltip-trigger="mouseenter" tooltip-placement="bottom"><i class="fa fa-fw fa-pencil"></i></button>
    

    请注意,我还给按钮起了一个类名!!

    然后在你的 app.js 文件中,你可以将其他按钮的显示设置为none,当一个按钮被触发编辑时。然后在保存编辑时,将显示设置为阻止:

    var eles = document.getElementsByClassName('edit-btn');
    
    $scope.editContact = function (contact, ind) {
    
        $scope.model.selected = angular.copy(contact);
    
        //remove display of other buttons
        for(var i = 0; i < eles.length; i++){
          if(i != ind){
            eles[i].style.display = "none";
          }
        }
    
    };
    
    $scope.saveContact = function (idx) {
        console.log("Saving contact");
        $scope.model.contacts[idx] = angular.copy($scope.model.selected);
    
        //redo display of all buttons
        for(var i = 0; i < eles.length; i++){
            eles[i].style.display = "block";
        }
    
        $scope.reset();
    };
    

    您可以看到在editContact 按钮内,ind 变量是当前被点击的编辑按钮的索引。

    这是一个 Plunker:http://plnkr.co/edit/N9EKErLJkFR5TwEzImNP?p=preview

    【讨论】:

    • 很好的解决方法。我将您的“重做显示”循环移动到重置功能,因为取消编辑也应该显示所有按钮 (plnkr.co/edit/mTa8EPmuwEOw1DBHLAHN?p=preview)。但正如您从使用模板的演示中看到的那样,理想的期望是显示所有按钮,并且能够在其他行上单击编辑而无需保存/取消
    • 只需将要显示和隐藏的元素(eles)更改为输入文本框。此外,您还必须为包含内容的跨度添加一个类名。不过逻辑应该是一样的。
    • 仍然没有达到预期的效果(显示所有数据和按钮,并且可以单击任何编辑按钮进入相应行的编辑模式)。请查看我的回答,如果您有任何反馈,我们将不胜感激!感谢您的帮助!
    【解决方案2】:

    我能够通过将 editContact 和 reset 函数更新为以下内容来获得所需的结果:

    $scope.editContact = function(contact, event) {
      // Nothing first time, but will have an element second time
      $timeout(function() {
        // Click the element and remove the class since it is not in edit mode anymore
        angular.element('.row-edit-mode').triggerHandler('click');
        angular.element('.row-edit-mode').removeClass('row-edit-mode');
    
        // If it is not a button, then it is the fa-icon, so get its parent, which is a button
        var eventElement = angular.element(event.target).is('button') ? event.target : event.target.parentNode;
        // Find it's sibling with given id, and add a class to it
        angular.element(eventElement).siblings("#table-cancel").addClass('row-edit-mode')
    
        $scope.model.selected = angular.copy(contact);
      });
    };
    
    $scope.reset = function() {
      // Remove class on cancel
      angular.element('.row-edit-mode').removeClass('row-edit-mode');
      $scope.model.selected = {};
    };
    

    http://plnkr.co/edit/vAACyxv2bE0li5muefsQ?p=preview

    我仍然看到开关之间有轻微的闪烁,所以如果有人有建议让它更流畅,我将非常感激。

    如果我在几天内没有看到其他达到预期结果的答案,我会将这个答案标记为已接受。感谢所有提供帮助的人!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-07
      • 2014-07-15
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      相关资源
      最近更新 更多