将选定值从一个动态表推送到另一个动态表

时间:2017-03-30 11:20:03

标签: angularjs html5

我有两个名为产品和客户的表: -

<table cellpadding="0" cellspacing="0">
     <thead>
        <tr>
           <th>Product Code</th>
        </tr>
       </thead>
    <tbody>
  <tr ng-repeat="row in service track by $index" ng-class="{'ISselected': isSelected(row, $index)}" ng-click="selectedRow(row, $index)">
        <td>{{row}}</td>
  </tr>
    </tbody>
</table>



<table cellpadding="0" cellspacing="0">
      <thead>
         <tr>
           <th>Customer Code</th>
         </tr>
      </thead>
       <tbody>
         <tr ng-repeat="row in customers track by $index">
            <td>{{row}}</td>
         </tr>
       </tbody>
   </table>

还有一个按钮: -

<button type="button" class="btn btn-info">Add</button>

我的指令代码: -

scope.services = [];
 scope.customers = [];
  scope.selectedRowID = null;

            scope.selectedRow = function(rowID){
                    scope.selectedRowID = scope.selectedRowID == rowID ? null : rowID;
                }

            scope.isSelected = function(rowID){
                    return (scope.selectedRowID == rowID);
            }   

我想要的是当我从&#39;产品中选择一个特定的行/值时。表格并按'添加&#39;按钮然后该行/值被推送或添加到我的客户&#39;表。两个表中的数据都来自后端。

2 个答案:

答案 0 :(得分:1)

您不需要指令,而是通过将selected行推送到客户数据来完成工作。

这是基本fiddler,执行类似的东西。希望它有所帮助。

答案 1 :(得分:0)

我理解的是,您有ADD按钮的每一行,以便您可以将数据从一个表传输到另一个表。

首先在表格中为每一行添加“ADD”按钮。

然后将ng-click指令添加到ADD按钮

<button type="button" class="btn btn-info" ng-click="transferData($index)">Add</button>

然后将此功能添加到您的指令中:

$scope.transferData=function(index){
$scope.customers.push($scope.services [index]);
};

这将符合您的要求。