简体   繁体   English

使用angularJs在ng-click上删除一行

[英]Remove a row on ng-click using angularJs

I have a table with the default empty row(Row further includes a columns with different functionalities).Dynamically a multiple rows can be added by click of a button as per the requirement.i want to remove a row when i click a remove-icon from the same-row(remove-icon included in one of the column). 我有一个默认空行的表(行进一步包括具有不同功能的列)。动态地,可以通过按需要点击按钮添加多行。我想在我点击删除图标时删除一行从同一行(其中一列中包含的删除图标)。

<td class="text-right"> <a href="" class="remove-service" ng-click="vm.removeService(service,true)">
                <img src="images/delete.png"></a> &ensp;</td>

i have a above column to be included in all rows.whenever i click on remove-icon from any of the rows.(i want to remove a particular row from which icon is clicked) 我有一个上面的列要包含在所有行中。每当我点击任何行中的remove-icon时(我想删除单击某个图标的特定行)

 vm.removeService = function (service, removeService) {

}

Here,is the ng-repeat code for the row. 这里是行的ng-repeat代码。

  <tr class="bx--table-row" ng-repeat="service in vm.servicesWithCountries track by $index">

 vm.servicesWithCountries = [{ serviceName:"", countries: []}];

Hi you can do like below : Assuming your ng-repeat object will be like somewhat mention below. 嗨,你可以这样做:假设你的ng-repeat对象将在下面提到。

 <table>
    <tr ng-repeat="item in items | orderBy: 'id'">
      <td>
        <span>
            Hello, {{item.name}}!
        </span>
      </td>
      <td>
        <a href="#" ng-click="delete(item)">Delete</a>
      </td>
    </tr>
  </table>

and in Controller : 在控制器中:

$scope.items = [{
    name: 'item 1',
    id: '4'
  }, {
    name: 'item 2',
    id: '3'
  }, {
    name: 'item 3',
    id: '2'
  }, {
    name: 'item 4',
    id: '1'
  }];

  $scope.delete = function(item) {
    $scope.items.splice($scope.items.indexOf(item), 1);
  }

refer this fiddle for complete code 请参阅此小提琴以获取完整代码

You can use splice and indexOf on your service 您可以在service上使用spliceindexOf

 <tr class="bx--table-row" ng-repeat="service in vm.servicesWithCountries track by $index">
    <td class="text-right"> <a href="" class="remove-service" 
                              ng-click="vm.removeService(service, true)">
                <img src="images/delete.png"></a> &ensp;</td>
  </tr>

Like: 喜欢:

ng-click="vm.removeService(service,true)"

JS JS

vm.removeService = function(service, removeService) {

    if(removeService === true){
       var index = vm.servicesWithCountries.indexOf(service); 
       vm.servicesWithCountries.splice(index, 1);
    }        
  }

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

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