简体   繁体   English

AngularJS删除动态生成的行

[英]Angularjs removing dynamically generated rows

I have following html and Angularjs controller code to add rows dynamically. 我有以下html和Angularjs控制器代码来动态添加行。

<form name="{{form.name}}"
      ng-repeat="form in forms">         
  <h2>{{form.name}}</h2>
  <div ng-repeat="(i,cont) in form.contacts">
          <input type="text" class="xdTextBox" ng-model="cont.ac"/>
          <input type="text" class="xdTextBox" ng-model="cont.a_number"/>
          <input type="text" class="xdTextBox" ng-model="cont.p_id"/>
  </div>
  <button ng-click="submit(form)">Submit</button>
  <button ng-click="addFields(form)">Add</button>
  <hr>
</form>

Controller code to add rowsis 控制器代码添加行

$scope.addFields = function (form) {     
   if (typeof  form.contacts == 'undefined') {
         form.contacts = [];
    }
    form.contacts.push({name:'', ac: '', a_number: '', p_id: '' });
}

What I want to do next is after adding rows if i mouse over any row a delete link or button shows up and if one clicks it, it removes that row. 我要做的下一步是添加行后,如果我将鼠标悬停在任何行上,则会显示一个删除链接或按钮,如果单击它,它将删除该行。 Here is the working plunker for the adding rows. 这是添加行的工作插件。 http://plnkr.co/edit/9bUnd7t0PyMwykgi0VZR?p=preview Please let me know how I can mouse over a row and click the remove button or link to remove that list. http://plnkr.co/edit/9bUnd7t0PyMwykgi0VZR?p=preview请让我知道如何将鼠标悬停在一行上,然后单击删除按钮或链接以删除该列表。 Thanks 谢谢

Take a look here: 在这里看看:

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

I added the "contact" class to the div container so I could identify it in the CSS: 我将“ contact”类添加到div容器中,以便可以在CSS中识别它:

<div ng-repeat="(i,cont) in form.contacts" class="contact">

I added the remove button inside the container and gave it the "remove" class: 我在容器内添加了删除按钮,并为其指定了“删除”类:

<button type="button" class="remove" ng-click="form.contacts.splice(i, 1);">Remove</button>

(Note: You may wish to have a function inside your scope for removing a contact if you need to do anything more complicated than just removing it from the array.) (注意:如果您需要做的事情不仅仅只是从数组中删除联系人,那么您可能希望在其作用域内具有删除联系人的功能。)

To get the button to be hidden initially, but show up when you hover over the row, I used the following CSS: 为了使按钮最初处于隐藏状态,但将鼠标悬停在该行上时显示,我使用了以下CSS:

.contact .remove { visibility: hidden; }
.contact:hover .remove { visibility: visible; }

You can do it by adding a function to your scope that recieves the form and index, then splicing the desired index out of it: 您可以通过在范围内添加一个接收表单和索引的函数,然后将所需的索引拼接出来来实现此目的:

 <div ng-repeat="(i,cont) in form.contacts">
              <input type="text" class="xdTextBox" ng-model="cont.ac"/>
              <input type="text" class="xdTextBox" ng-model="cont.a_number"/>
              <input type="text" class="xdTextBox" ng-model="cont.p_id"/>
              <button ng-click="delete(form, i)">Delete</button>
      </div>

Then, the Javascript (add this to your controller): 然后,将Javascript(将其添加到您的控制器中):

$scope.delete = function(form, index) {
      form.contacts.splice(index, 1);
    }

http://plnkr.co/edit/2SEGDnGoE7kaw0KvOpKr?p=preview http://plnkr.co/edit/2SEGDnGoE7kaw0KvOpKr?p=preview

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

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