2016-11-21 75 views
0

使用ng-repeat创建的表中有多行。每一行都有一个下拉列表。我希望在下拉列表中获取选定的值,并使用JavaScript或JQuery单击“下一步”按钮将该值与该行中的其他相应值附加到字符串格式中。例如:“one,B:two,B:three,A:four:C”(在这个例子中,假设B选择下拉第一行,其他行类似逻辑)查找表中每行的下拉列表的选定值

以下是HTML代码片段:

<div class="mainDiv" ng-controller="myController">           
    <div class="div1"> 
     <table class="table myTable"> 
      <thead> 
       <tr> 
        <th> 
         <center>serial</center> 
        </th> 
        <th> 
         <center>data</center> 
        </th> 
        <th> 
         <center>aplhabet</center> 
        </th> 
       </tr> 
      </thead> 
      <tbody class="myTableBody"> 
       <tr ng-repeat="data in myData" id="{{$parent.$id+'-'+$index}}"> 
        <td>{{$index + 1}}</td> 
        <td>{{data}}</td> 
        <td> 
         <select> 
          <option value="Select">-- Select --</option> 
          <option value="A">A</option> 
          <option value="B">B</option> 
          <option value="C">C</option> 
         </select> 
        </td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
    <div class="div2"> 
     <button type="button" class="btn btn-md btn-primary btnNext">Next</button> 
    </div> 
</div> 

和下面是angularJS部分:

myApp.controller('myController', function ($scope) { 
    $scope.myData = ["one","two","three","four"]; 
}); 
+0

需要{{数据}},每行下拉的选定值。例如在表 串行数据\t字母一个\t乙乙Ç甲 预期结果:“一个,B:2,B:3 ,A:4:C” –

回答

1

添加ng-model={ selectData[data] }

然后更新与下列

控制器
$scope.selectData = {} 
getResults= function() { 
    var returnData = $scope.myData.map(function(data) { 
    return data + ',' + $scope.selectData[data]; 
    }); 
    return returnData.join(':'); 
}; 
0

切换到对象。

$scope.myData = { 
    one: { 
     id: 'one', 
     value: null 
    }, 
    two: { 
     id: 'two', 
     value: null 
    }, 
    three: { 
     id: 'three', 
     value: null 
    }, 
    four: { 
     id: 'four', 
     value: null 
    } 
} 

在HTML中,添加一个ng-model您选择

<select ng-model="data.value"> 
    <!-- your options, because I'm lazy --> 
</select> 

你会得到一个包含所有你想要的对象。在我看来,我发现使用对象而不是数组更容易。