Angular.js ui-grid custom date filter

后端 未结 4 628
抹茶落季
抹茶落季 2021-02-09 09:51

I am using the angular grid, ui-grid, located at ui-grid.info.

I am trying to make a custom filter that will filter the grid by date using date inputs controls, one for

4条回答
  •  不思量自难忘°
    2021-02-09 10:35

    If anyone else is looking for a solution to this, I implemented custom FROM and TO filters in the grid header using ui-bootstrap modals with datepickers in them. See this plunker for details.

    It's obvious in retrospect, but one of the tripwires in finding the solution was making sure that the date values populating the date column in the grid were actually of type Date. As it turns out, comparing the dates selected from the datepickers to strings won't get you very far.

    Note: this solution depends on lodash for some data transformation.

    var app = angular.module('app', ['ngAnimate', 'ui.grid', 'ui.grid.selection', 'ui.bootstrap']);
    
    app.controller('MainCtrl', ['$scope', '$http', 'uiGridConstants', function ($scope, $http, uiGridConstants) {
    
      $scope.gridOptions = {
        enableFiltering: true,
        onRegisterApi: function(gridApi){
          $scope.gridApi = gridApi;
        },
        columnDefs: [
          {
            field: 'DATE_TIME',
            displayName: 'Date Time',
            enableFiltering: true,
            enableCellEdit: false,
            filterHeaderTemplate: '
    ', filters: [ { name: 'From', condition: uiGridConstants.filter.GREATER_THAN_OR_EQUAL }, { name: 'To', condition: uiGridConstants.filter.LESS_THAN_OR_EQUAL } ], cellFilter: 'date:"M/d/yyyy h:mm:ss a"', width: '40%' }, { field: 'QTY', displayName: 'Quantity', enableCellEdit: false, enableFiltering: false }, { field: 'UNIT_COST', displayName: 'Unit Cost', enableCellEdit: false, enableFiltering: false }, { field: 'TOTAL_COST', displayName: 'Total Cost', enableCellEdit: false, enableFiltering: false } ] }; // in plnkr, grab the following data from external file // $http.get('grid-data.json') // .success(function(data) { // $scope.gridOptions.data = data; $scope.gridOptions.data = [ { "DATE_TIME": "2015-10-12T10:46:27.000Z", "QTY": 3, "UNIT_COST": 0.25, "TOTAL_COST": 0.75 }, { "DATE_TIME": "2015-10-18T06:09:27.000Z", "QTY": 4, "UNIT_COST": 0.25, "TOTAL_COST": 1.00 }, { "DATE_TIME": "2015-10-05T11:57:27.000Z", "QTY": 6, "UNIT_COST": 0.55, "TOTAL_COST": 0.90 }, { "DATE_TIME": "2015-10-21T03:42:27.000Z", "QTY": 8, "UNIT_COST": 0.25, "TOTAL_COST": 2.00 }, { "DATE_TIME": "2015-09-29T18:25:27.000Z", "QTY": 3, "UNIT_COST": 0.45, "TOTAL_COST": 1.35 }, { "DATE_TIME": "2015-09-19T21:13:27.000Z", "QTY": 5, "UNIT_COST": 0.25, "TOTAL_COST": 1.25 }, { "DATE_TIME": "2015-08-31T15:46:27.000Z", "QTY": 7, "UNIT_COST": 0.10, "TOTAL_COST": 0.70 }, { "DATE_TIME": "2015-10-12T10:14:27.000Z", "QTY": 2, "UNIT_COST": 0.65, "TOTAL_COST": 1.30 } ]; // make sure date values are Date objects _.forEach($scope.gridOptions.data, function (val) { val.DATE_TIME = new Date(val.DATE_TIME); }); }]) .controller('gridDatePickerFilterCtrl', ['$scope', '$timeout', '$uibModal', 'uiGridConstants', function( $scope, $timeout, $uibModal, uiGridConstants) { $timeout(function() { console.log($scope.col); var field = $scope.col.colDef.name; var allDates = _.map($scope.col.grid.appScope.gridOptions.data, function(datum) { return datum[field]; }); var minDate = _.min(allDates); var maxDate = _.max(allDates); $scope.openDatePicker = function(filter) { var modalInstance = $uibModal.open({ templateUrl: 'custom-date-filter.html', controller: 'customGridDateFilterModalCtrl as custom', size: 'md', windowClass: 'custom-date-filter-modal', resolve: { filterName: [function() { return filter.name; }], minDate: [function() { return new Date(minDate); }], maxDate: [function() { return new Date(maxDate); }], } }); modalInstance.result.then(function(selectedDate) { console.log('date', selectedDate); $scope.colFilter.listTerm = []; console.log(typeof selectedDate); console.log(selectedDate instanceof Date); $scope.colFilter.term = selectedDate; }); }; }); }]) .controller('customGridDateFilterModalCtrl', ['$scope', '$rootScope', '$log', '$uibModalInstance', 'filterName', 'minDate', 'maxDate', function($scope, $rootScope, $log, $uibModalInstance, filterName, minDate, maxDate) { var ctrl = this; console.log('filter name', filterName); console.log('min date', minDate, 'max date', maxDate); ctrl.title = 'Select Dates ' + filterName + '...'; ctrl.minDate = minDate; ctrl.maxDate = maxDate; ctrl.customDateFilterForm; ctrl.filterDate = (filterName.indexOf('From') !== -1) ? angular.copy(ctrl.minDate) : angular.copy(ctrl.maxDate); function setDateToStartOfDay(date) { return new Date(date.getFullYear(), date.getMonth(), date.getDate()); } function setDateToEndOfDay(date) { return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59); } ctrl.filterDateChanged = function () { ctrl.filterDate = (filterName.indexOf('From') !== -1) ? setDateToStartOfDay(ctrl.filterDate) : setDateToEndOfDay(ctrl.filterDate); $log.log('new filter date', ctrl.filterDate); }; ctrl.setFilterDate = function(date) { $uibModalInstance.close(date); }; ctrl.cancelDateFilter = function() { $uibModalInstance.dismiss(); }; }]) .directive('customGridDateFilterHeader', function() { return { template: '
     
    ', controller: 'gridDatePickerFilterCtrl' }; }) ;
    
    
    
      
        
        
        
        
        
        
        
        
        
        
        
      
    
       
        
        

提交回复
热议问题