表函数调用后失去排序 - 角度

时间:2015-03-17 12:14:03

标签: javascript angularjs sorting

我有一个使用angular orderby的表。我使用一个名为tcoSort的函数进行计算,然后对表进行排序。问题是当我从行中的单选按钮调用displayFullPricing()时 - 表失去了排序。

<table class="table table-striped table-hover">
    <tr  ng-repeat="prices in productVariant.prices | orderBy: tcoSort">
    <td>£{{prices.monthly.retailPrice}}</td>
    <td>£{{prices.nonRecurring.retailPrice | number:0}}</td>
    <td><input type="radio" name="{{productVariant.code}}"  ng-click="displayFullPricing($index)"></td>
</tr>

$scope.tcoSort = function (productVariant) {
if(!$scope.isDisplayPrice){
    return  productVariant.nonRecurring.retailPrice + (productVariant.monthly.retailPrice * 36);
}
};


$scope.displayFullPricing = function (index) {

   $scope.isDisplayPrice = true; //This is to stop the table sort running more than once. 
I put this here to act as a flag as everytime this function was called from the radio button, tcoSort would fire again and sort the table. 
This meant that the radio button would only work once.

   //rest of code

}

我想问题是我正在创建标志,以便在每次单击表格中的单选按钮时停止排序。但如果我不把它放在那里,$ index总是设置为最高结果 - 所以它总是为0.任何帮助或评论都赞赏。感谢。

1 个答案:

答案 0 :(得分:3)

不要将相关项的$索引传递给displayFullPricing函数,而应该只传递对象本身,例如。

ng-click="displayFullPricing(prices)"

大概在其他代码的某处,您只是根据其索引选择相关项目?