如何从外部排序功能更新ng-grid

时间:2013-06-29 03:03:59

标签: angularjs ng-grid

任何人都知道如何从外部排序功能更新ng-grid?

我已将userExternalSort设置为true。然后,我有这个代码(Coffeescript):

$scope.$on 'ngGridEventSorted', (event, data) ->
    console.log "Before sort " + $scope.recs[0].location

    $scope.recs.sort (a, b) ->
        if data.directions[0] = "asc"
            return a.location > b.location ? 1 : -1
        else
            return a.location > b.location ? -1 : 1

    console.log "After sort " + $scope.recs[0].location

我的功能确实排序。但是,ng-grid永远不会更新。我已经尝试了$ scope。$ apply()无效 - 它已经在申请中了。

感谢。

1 个答案:

答案 0 :(得分:6)

您的代码存在一些问题,为了帮助您完全使用代码,我需要查看您的gridOptions,以便我可以看到您是否正确更新数据。如果我们能够获得一些代码也可能会有所帮助

首先,该选项是“useExternalSort”,实际上会关闭ngGridEventSorted事件,所以你需要使用类似的东西:

$scope.$watch 'gridOptions.ngGrid.config.sortInfo', (newVal,oldVal) ->
    console.log "Before sort " + $scope.recs[0].location

    $scope.recs.sort (a, b) ->
        if data.directions[0] = "asc"
            return a.location > b.location ? 1 : -1
        else
            return a.location > b.location ? -1 : 1

    console.log "After sort " + $scope.recs[0].location