2017-01-03 21 views
0

によるソートではない、と私はそうのようなJS日付オブジェクトの束持っている:私はとき日付で列をフィルタリングすることができるようにしたい角度UI-グリッドは、私はUI-グリッドを使用しています日付

"dob": new Date('1942-11-19') 

を「昇順/降順ソート」ボタンをクリックします。そのため、私はそうのようなアップcolDefを設定している:

 { 
     field: 'dob' 
     , displayName: 'D.O.B.' 
     , width: '130' 
     , editableCellTemplate: '<div><form name="inputForm"><input type="INPUT_TYPE" ng-class="\'colt\' + col.uid" ui-grid-editor ng-model="MODEL_COL_FIELD" style="border-bottom-color: #74B3CE; border-bottom-width: 2px;"></form></div>' 
     , headerCellClass: $scope.highlightFilteredHeader 
     , cellTemplate: '<div class="ui-grid-cell-contents" >{{grid.getCellValue(row, col)| date:\'MM-dd-yyyy\'}}</div>' 
     , cellFilter: 'date' 
     , type: 'date' 
     }, 

しかし、列は単に正しくソートされません。私もそうのような外部のボタンから、それをソートする機能を設定してみました:

 function mostRecent(){ 
     console.log('clicked mostRecent'); 
     $scope.gridApi.grid.sortColumn(
      $scope.gridApi.grid.getColumn('dob'), uiGridConstants.DESC 
     ); 
     $scope.gridApi.grid.notifyDataChange(uiGridConstants.dataChange.ALL); //this line updates the rest of the columns accordingly 
     }; 

しかし、それはまた、正しくないミッシュ・マッシュソートが発生します。誰が問題が何であるか知っていますか?私はそれが私のcellTemplateと関係があるかもしれないと思ったが、テンプレートを削除した後に違いはなかった...

答えて

2

はい、あなたは正しいですが、ui-gridは日付タイプの列の並べ替えをサポートしていません。

ただし、columnDefにはsortingAlgorithmを定義できます。ここで

は、あなたの列の定義は次のようになります方法です。

... 

columnDefinition.sortingAlgorithm = function (firstDateString, secondDateString) { 
    var dateFormat = 'YYYY-MM-DD'; 
    return function (firstDateString, secondDateString, dateFormat) { 
    if (!firstDateString && !secondDateString) { 
     return 0; 
    } 

    if (!firstDateString) { 
     return 1; 
    } 

    if (!secondDateString) { 
     return -1; 
    } 

    var firstDate = $window.moment(firstDateString, dateFormat); 
    if (!firstDate.isValid()) { 
     throw new Error('Invalid date: ', firstDateString); 
    } 

    var secondDate = $window.moment(secondDateString, dateFormat); 
    if (!firstDate.isValid()) { 
     throw new Error('Invalid date: ', secondDateString); 
     } 

    if (firstDate.isSame(secondDate)) { 
     return 0; 
    } else { 
     return firstDate.isBefore(secondDate) ? -1 : 1; 
    } 
    }; 
}; 

... 

この例ではMoment.jsが使用されていることに注意してください。これは非常に便利なライブラリですので、プロジェクト内で別の場所を使用している可能性があります。

+0

感謝を。私はそれを試した、それは残念ながら、並べ替えに影響を与えていないようです。私はまだ同じ結果を得ています。未完成の結果 – IWI

+0

@IWI PlunkerまたはJSFiddleを作成できますか? – tomepejo

+1

あなたは本当に正しいです。私はばかだと私の日付は文字列で日付オブジェクトではないことに気づいた.... – IWI

関連する問題