Angular.js ui-grid custom date filter

后端 未结 4 648
抹茶落季
抹茶落季 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:31

    Do you mean something like this ?

    First you should include jQuery UI Datepicker

    Then you will also create a directive for it:

    app.directive('datePicker', function(){
        return {
            restrict : "A",
            require: 'ngModel',
            link : function(scope, element, attrs, ngModel){
                $(function(){
                    $(element).datepicker({
                         changeMonth: true,
                         changeYear: true,
                         closeText: 'Clear',
                         showButtonPanel: true,
                         onClose: function () {
                            var event = arguments.callee.caller.caller.arguments[0];
                            // If "Clear" gets clicked, then really clear it
                            if ($(event.delegateTarget).hasClass('ui-datepicker-close')) {
                                $(this).val('');
                                scope.$apply(function() {
                                   ngModel.$setViewValue(null);
                                });
                            }
                        },
                        onSelect: function(date){
                            scope.$apply(function() {
                               ngModel.$setViewValue(date);
                            });
                        }
                   });
                })
            }
        }
    })
    

    In your columnDefs you will also need to use customer filters and filter header template:

    filters:[{ condition: checkStart}, {condition:checkEnd}],filterHeaderTemplate: '

    from : to :
    '

    Assume you are using momentjs The filter functions will be like this:

    function checkStart(term, value, row, column) {
            term = term.replace(/\\/g,"")
            var now = moment(value);
            if(term) {
                if(moment(term).isAfter(now, 'day')) return false;;
            } 
            return true;
        }
    
        function checkEnd(term, value, row, column) {
            term = term.replace(/\\/g,"")
            var now = moment(value);
            if(term) {
                if(moment(term).isBefore(now, 'day')) return false;;
            } 
            return true;
        }
    

提交回复
热议问题