简体   繁体   English

如何在Angularjs x-editable中按Enter保存表单字段?

[英]How to save form fields by pressing enter in Angularjs x-editable?

I'm trying to save a form field using Angularjs x-editable. 我正在尝试使用Angularjs x-editable保存表单字段。 In the given example on their website, they use a save button to save the fields. 在其网站上的给定示例中,他们使用“保存”按钮来保存字段。 However, I am trying to acheive the save functionality without pressing the save button. 但是,我试图实现保存功能而不按保存按钮。 Instead I want to be able to save whenever I press enter key on any of the editable fields (Name, status, group). 相反,我希望每当我在任何可编辑字段(名称,状态,组)上按Enter键时都可以保存。 I know this is achievable on a traditional form by pressing enter but that does not seem to work in this case. 我知道这可以通过按Enter键以传统形式实现,但在这种情况下似乎不起作用。

Here is the link http://vitalets.github.io/angular-xeditable/#editable-form 这是链接http://vitalets.github.io/angular-xeditable/#editable-form

and the JSFiddle. 和JSFiddle。 http://jsfiddle.net/NfPcH/93/ . http://jsfiddle.net/NfPcH/93/

Any help is appreciated, thanks. 任何帮助表示赞赏,谢谢。

HTML HTML

    <h4>Angular-xeditable Editable row (Bootstrap 3)</h4>
<div ng-app="app" ng-controller="Ctrl">
   <table class="table table-bordered table-hover table-condensed">
    <tr style="font-weight: bold">
      <td style="width:35%">Name</td>
      <td style="width:20%">Status</td>
      <td style="width:20%">Group</td>
      <td style="width:25%">Edit</td>
    </tr>
    <tr ng-repeat="user in users">
      <td>
        <!-- editable username (text with validation) -->
        <span editable-text="user.name" e-name="name" e-form="rowform" onbeforesave="checkName($data, user.id)" e-required>
          {{ user.name || 'empty' }}
        </span>
      </td>
      <td>
        <!-- editable status (select-local) -->
        <span editable-select="user.status" e-name="status" e-form="rowform" e-ng-options="s.value as s.text for s in statuses">
          {{ showStatus(user) }}
        </span>
      </td>
      <td>
        <!-- editable group (select-remote) -->
        <span editable-select="user.group" e-name="group" onshow="loadGroups()" e-form="rowform" e-ng-options="g.id as g.text for g in groups">
          {{ showGroup(user) }}
        </span>
      </td>
      <td style="white-space: nowrap">
        <!-- form -->
        <form editable-form name="rowform" onbeforesave="saveUser($data, user.id)" ng-show="rowform.$visible" class="form-buttons form-inline" shown="inserted == user">
          <button type="submit" ng-disabled="rowform.$waiting" class="btn btn-primary">
            save
          </button>
          <button type="button" ng-disabled="rowform.$waiting" ng-click="rowform.$cancel()" class="btn btn-default">
            cancel
          </button>
        </form>
        <div class="buttons" ng-show="!rowform.$visible">
          <button class="btn btn-primary" ng-click="rowform.$show()">edit</button>
          <button class="btn btn-danger" ng-click="removeUser($index)">del</button>
        </div>  
      </td>
    </tr>
  </table>

  <button class="btn btn-default" ng-click="addUser()">Add row</button>
</div>

In my experience, the only way to do this is to capture the keydown event, passing the $event and the rowform. 以我的经验,唯一的方法就是捕获$ keydown事件,并传递$ event和rowform。 In the keydown handler you can evaluate the key pressed and save the form or simply save the current value to your persistent store. 在keydown处理程序中,您可以评估按下的键并保存表单,或者只是将当前值保存到持久性存储中。

...
<span editable-text="user.name" e-name="name" 
      e-form="rowform" onbeforesave="checkName($data, user.id)" 
      e-required
      e-ng-keydown="navigate($event,rowform)">
  {{ user.name || 'empty' }}
</span>
...

Then, in your controller 然后,在您的控制器中

$scope.navigate = function(event,form) {
  if (event.keyCode==13) {
    //...Enter Key Processing
  } else if (event.keyCode==27) {
    //...Escape key processing
  } else if (...other key codes) 
    //...Et cetera
}

You can use rowform.$submit() to submit the form. 您可以使用rowform.$submit()提交表单。

hope this helps. 希望这可以帮助。 Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM