2015-06-04 62 views
0

排序時,日期根據字符串排序,即只通過前2個字符。可以根據日期發送用於在UI網格中排序的代碼。 我試過排序,但它所做的是按字符串格式排序。UI GRID日期排序

+0

如果您希望我們向您發送代碼,則表明您處於錯誤的位置。向我們展示您嘗試過的方式..您如何使用網格和您的示例數據。 –

回答

0
http://ui-grid.info/docs/#/tutorial has provided many examples demonstrating the sorting. 


Please have a look at the below pluck. Hope this solves 

http://plnkr.co/edit/Buzf1ZLNv2X6rhPSZkuG?p=preview 

var app = angular.module('app', ['ngAnimate', 'ngTouch', 'ui.grid','ui.grid.moveColumns']); 

app.controller('MainCtrl', ['$scope', '$http', 'uiGridConstants', function ($scope, $http, uiGridConstants) { 
    var today = new Date(); 
    var nextWeek = new Date(); 
    nextWeek.setDate(nextWeek.getDate() + 7); 

    $scope.highlightFilteredHeader = function(row, rowRenderIndex, col, colRenderIndex) { 
    if(col.filters[0].term){ 
     return 'header-filtered'; 
    } else { 
     return ''; 
    } 
    }; 

    $scope.gridOptions = { 
    enableFiltering: false, 
    onRegisterApi: function(gridApi){ 
     $scope.gridApi = gridApi; 
    }, 
    columnDefs: [ 
     // default 
     { field: 'name', headerCellClass: $scope.highlightFilteredHeader }, 
     { field: 'mixedDate', displayName: "Long Date", cellFilter: 'date:"longDate"', filterCellFiltered:true, width: '25%', 
     }, 
     { 
     field: 'phone', 
     filter: { 
      condition: function(searchTerm, cellValue) { 
      var strippedValue = (cellValue + '').replace(/[^\d]/g, ''); 
      return strippedValue.indexOf(searchTerm) >= 0; 
      } 
     }, headerCellClass: $scope.highlightFilteredHeader 
     }, 
     // date filter 
     { field: 'mixedDate', cellFilter: 'date', width: '15%', filter: { 
      condition: uiGridConstants.filter.LESS_THAN, 
      placeholder: 'less than', 
      term: nextWeek 
     }, headerCellClass: $scope.highlightFilteredHeader 
     } 
    ] 
    }; 

    $http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/500_complex.json') 
    .success(function(data) { 
     $scope.gridOptions.data = data; 
     $scope.gridOptions.data[0].age = -5; 

     data.forEach(function addDates(row, index){ 
     row.mixedDate = new Date(); 
     row.mixedDate.setDate(today.getDate() + (index % 14)); 
     row.gender = row.gender==='male' ? '1' : '2'; 
     }); 
    }); 

    $scope.toggleFiltering = function(){ 
    $scope.gridOptions.enableFiltering = !$scope.gridOptions.enableFiltering; 
    $scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN); 
    }; 
}]) 
.filter('mapGender', function() { 
    var genderHash = { 
    1: 'male', 
    2: 'female' 
    }; 

    return function(input) { 
    if (!input){ 
     return ''; 
    } else { 
     return genderHash[input]; 
    } 
    }; 
}); 
+0

請不要縮進你的評論,所以我們看看代碼是什麼,什麼不是 –