javascript - Angular 内联编辑表单例

标签 javascript css angularjs html-table inline-editing

我正在尝试为表格创建内联编辑(类似于 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)

最佳答案

通过将 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

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

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

关于javascript - Angular 内联编辑表单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39355209/

相关文章:

javascript - Promise.all 每次处理一组 6 个 promise

javascript - 是否可以通过这种方式为 Array 增值?

java - 将带有 html/Javascript 的字符串放入 selenium webdriver

javascript - 检查 span 元素是否包含超过 3 行

html - CSS 和 HTML : mark area of table with mouse tracking

css - swatch theme 的页脚无法固定在底部

javascript - 如何在angularjs中使用带有输入表单的md对话框?

javascript - angularjs:如何重新启动 Controller 以更新要观看的数据源模型?

javascript - 简单的 for 循环似乎无法正常工作

javascript - 在 Backbone.js 中绑定(bind)一个回调到这个