2016-11-21 74 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> 

你會得到一個包含所有你想要的對象。在我看來,我發現使用對象而不是數組更容易。