2017-04-10 84 views
0

我有訂單的表,我想排序他們點擊和反向排序第二次click.On第一次單擊排序工作正常,但第二次點擊反向排序不工作。它引發解析exception.Here是我的代碼 HTML第二次點擊相反的順序

<tr> 
    <th><a href="#" ng-click="changeOrder('merchant.id');" ng-dblclick="forDoubleClick('merchant.id')"><abbr title="Merchant ID">M.I.</abbr></a></th> 
    </tr> 

    <tr dir-paginate="item in serverData | orderBy : orderBy | filter:filterData | itemsPerPage: itemPerPageValue"></tr> 

    ... 

我的JS代碼

$scope.orderBy = 'value'; 

$scope.changeOrder = function(value) { 
     $scope.orderBy = value; 
    } 

$scope.forDoubleClick = function(value) 
    { 
     $scope.orderBy = value + " : reverse "; 
    } 
+0

從[這個有用的SO答案]得到幫助(http://stackoverflow.com/questions/31074578/how-to-reverse-sort-a-column-on-click-using-angularjs) –

+0

謝謝你是工作正常。 –

回答

0

你不需要$scope.forDoubleClick功能,我只是增加了一個變量$scope.currentOrder = 1;這將決定Sort順序。即,如果%remainder等於0Ascending排序,否則按Descending順序排序。

,並增加$scope.currentOrder每個click

$scope.currentOrder = 1; 
$scope.changeOrder = function(value) { 
$scope.currentOrder++; 
if($scope.currentOrder % 2 != 0) 
    $scope.orderBy = '-' + value; 
}else{ 
    $scope.orderBy = value; 
} 
+0

我試過了,但反向排序錯了,我認爲它對字符串和int的作用不同,但我不知道在開始哪一個將是我的排序對象(int或字符串)它必須爲他們兩個工作。 –

+0

你能創建一個小提琴嗎? – manish

0

你可以嘗試這樣的事情?

$scope.sortOrder = 'ASC'; 
$scope.changeOrder = function(value) { 
    if($scope.sortOrder === 'ASC') { 
     $scope.orderBy = value; 
     $scope.sortOrder = 'DSC'; 
    } else { 
     $scope.orderBy = value + " : reverse "; 
     $scope.sortOrder = 'ASC'; 
    } 
} 
+0

我試過它發生分析錯誤。 https://docs.angularjs.org/error/$parse/syntax?p0=:&p1=is%20an%20unexpected%20token&p2=13&p3=merchant.id%20:%20reverse&p4=:%20reverse –

+0

我試過了,同樣的錯誤 –

+0

你可以刪除單引號ng-click =「changeOrder(merchant.id);」然後再試一次? –

0

而不是ng-click="changeOrder('merchant.id');"

ng-click="changeOrder(merchant.id);"同樣適用於ng-dblclick

+0

我試過了。不工作 –