Angular.js ui-grid custom date filter

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

    I am using angular-ui-grid 3.1.1 and angular-ui-bootstrap 1.3.2.

    My solution is based on ui-grid filter help and Munna S / Bennett Adams comments. I Obtained a solution with less code.

    It works with two filters, and a Custom template ('ui-grid/custom-ui-grid-filter').

      { field: 'DATE_TIME', name: 'Date Time', cellTooltip: true,
        cellFilter: 'date:\'yyyy-MM-dd\'',
        cellTemplate: 'ui-grid/date-cell',
        filterHeaderTemplate: 'ui-grid/custom-ui-grid-filter',
        width: '40%',
        filters: [
            {
              condition: function(term, value, row, column){
                    if (!term) return true;
                    var valueDate = new Date(value);
                    return valueDate >= term;
                },
              placeholder: 'Greater than or equal'
            },
            {
              condition: function(term, value, row, column){
                    if (!term) return true;
                    var valueDate = new Date(value);
                    return valueDate <= term;
                },
              placeholder: 'Less than or equal'
            }
        ],
        headerCellClass: $scope.highlightFilteredHeader }
    

    Remember that in the condition function, 'term' comes from the DatePicker filter and is a Date. Also, value is an string formatted as 'yyyy-MM-dd' in my example.

    Here is my Plunkr Hope it helps and if you have any improvement please let me know.

提交回复
热议问题