【问题标题】:Add/remove input boxes to the table row in Angular dynamically动态添加/删除输入框到Angular中的表格行
【发布时间】:2015-01-13 21:04:32
【问题描述】:

不知何故,我无法理解这个问题——我有一个表格,想在点击时添加该行,但该行应该有空输入框和一个用于删除自身的按钮。

 <tbody>
        <tr>
            <th>Test 1</th>
            <th>200</th>
            <th>Add new row</th>
        </tr>
         //I want to Add this part dynamically everytime when I click on Add new row
                <tr>
                     <td>
                        <input type="text" placeholder="Please Enter the Address">
                    </td>
                    <td>
                        <input type="text" placeholder="Number of Quantity">
                    </td>
         //On Delete it should delete just that particular row
                    <td>Delete</td>
                </tr>

        </tbody>

我创建了 plunker http://plnkr.co/edit/DDj5Z99tw1QuN8xlxZ7V?p=preview 只是为了展示我想要实现的目标。如果有人能够给出提示或将我链接到教程会很棒!

【问题讨论】:

  • 您似乎对角度一无所知。 plunker 没有应用程序或控制器。您需要做的就是将ng-click 放在要添加行的任何位置,并让被调用的函数向您的数据源添加一个项目。查看任何基本的角度教程。
  • 对不起,如果我之前没有说清楚。创建 plunker 只是为了更好地理解我的问题。有很多关于向表格添加行的教程。但我的问题更具体的是在表格中添加输入框。
  • 您应该已经在 plunker 中完成了设置,以便有人可以立即帮助您。

标签: javascript angularjs angular-directive


【解决方案1】:

请看看这个 plunker。

http://plnkr.co/edit/ogvezWz6WDwDhCnm2bDU

想法是在末尾使用可以控制可见性的行。并且使用 ngRepeat 您可以迭代地显示您添加的产品项。

    <tr ng-repeat="row in rows">
         <td>
            {{row.product}}
        </td>
        <td>
            {{row.quantity}}
        </td>
        <td>
          <button ng-click="deleteRow($index)">Delete</button>
          <button ng-click="addNewRow()">New</button>
        </td>
    </tr>
    <tr ng-show="addrow">
         <td>
            <input type="text" placeholder="Please Enter the Address" ng-model="product"/>
        </td>
        <td>
            <input type="text" placeholder="Number of Quantity" ng-model="quantity"/>
        </td>
        <td><button ng-click="save()">Save</button> <button ng-click="delete()">Delete</button></td>
    </tr>

还有控制器代码

angular.module('AddRow', [])
.controller('MainCtrl', [
'$scope', function($scope){
  $scope.rows = [ { "product": "Test 1", "quantity": "200"}];
  $scope.addrow = false;

  $scope.addNewRow = function(){
    $scope.addrow = true;
  };

  $scope.deleteRow = function(index){
    //delete item from array
    $scope.rows.splice(index,1);
  };

  $scope.save = function(){
    //add item to array
    $scope.rows.push({"product": $scope.product, "quantity": $scope.quantity});
    //reset text input values
    $scope.product = "";
    $scope.quantity = "";
    //hide the add new row
    $scope.addrow = false;
  };

  $scope.delete = function(){
    $scope.addrow = false;
  };
}]);

【讨论】:

  • 谢谢伙计:)。我快速浏览了您的解决方案,新按钮仅适用。但是很努力。
  • 那么您希望能够使用文本框添加多个空行吗?
  • 是的,这就是我现在在实际代码中面临的问题 :)。
  • 可能正在使用 ngHtmlBind 可能会有所帮助。让我快速尝试一下。
  • 现在它可以满足您添加多行的需求。请检查plunker。但是将文本输入值更新到模型时存在问题。
猜你喜欢
  • 2011-07-20
  • 1970-01-01
  • 2013-02-12
  • 2021-07-28
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多