How to display datepicker in headerCellTemplate to filter the date
Hi I inserted a custom filter in the grid to filter the grid data. In that in one column i need to filter the date so i want to insert the date-picker to filter the date column i inserted headerCellTemplate but date-picker is not displaying please guide me in a right way to achieve this. Thanks in Advance.
yeah my problem is this too. i guess this will be available version 3.0 :/
duplicate of #2796 ? close this?
This one is on 2.x, so it's not quite a duplicate. But it's unlikely to get implemented given 3.0 is coming.
I have created datepicker as directive and i want to use in the columns. i want to filter like how age works. I can able to see the two texboxes but its not showig the datepicker as expected. but when i use "filterHeaderTemplate" outsite filters property its working but i can able place only one filterHeaderTemplate.
Is possible to have two headerCellTemplate in single column.. like below
columnDefs: [
{ field: 'mixedDate', displayName: "Registered On", cellFilter: 'date:"longDate"', filterCellFiltered:true ,
filters: [
{ condition: uiGridConstants.filter.LESS_THAN,
filterHeaderTemplate: '<div class="ui-grid-filter-container" ng-repeat="colFilter in col.filters"><input type="text" class="ui-grid-filter-input" ng-model="colFilter.term" datepicker /></div>'
},
{ condition: uiGridConstants.filter.GREATER_THAN,
filterHeaderTemplate: '<div class="ui-grid-filter-container" ng-repeat="colFilter in col.filters"><input type="text" class="ui-grid-filter-input" ng-model="colFilter.term" datepicker /></div>'
}]
}
]
+1
+1
does anyone know the solution for filter between two dates ?
@anchitanc
name: 'Date',
type: 'date',
cellFilter: 'date:"MM/dd/yyyy"',
filterHeaderTemplate: '<div class="ui-grid-filter-container" ng-repeat="colFilter in col.filters"><input type="date" ng-model="colFilter.term" style="font-size:12px"/></div>',
filters:
{
filterName: "greaterThan",
condition: uiGridConstants.filter.GREATER_THAN,
placeholder: 'greater than'
},
{
filterName: "lessThan",
condition: uiGridConstants.filter.LESS_THAN,
placeholder: 'less than'
}
Make sure that date column data is Date Object and not string. if the data is not Date object, just loop through gridoptions.data and set date = new Date();
angular.forEach($scope.gridOptions.data, function (val) {
val.date = new Date(val.date);
});
@anchitanc
I did that using jquery date picker. Here is the code of directives and how to use it in the grid.
.directive('myCustomModalDate', function() {
return {
template: 'From :
To : ',
controller: 'SmartSearchFiltersCtrl'
};
})
.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); }); } }); }) } } })
For Grid
filters:[{ condition: checkStart}, {condition:checkEnd}], filterHeaderTemplate: '
'Methods
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;
}
I have used Moment.js too.
Hope this helps someone!
Thanks for providing your answers. @bhavesh-vyas I tried your solution but i didnt work for me even column data are in Date for format. @meenakshijha i will try your solution and post the result.
@meenakshijha can you put this is a plunker or something. It would be really helpful, because i am unable to implement it right now
I worked up an example plunker with ui-bootstrap datepicker that filters from and to dates, if it helps anyone.
look like this plunker is not working correctly, Its not showing grid and data. Can you please check?
@anchitanc , hmmm, it's working fine for me in Chrome 49 (no errors in console, and the same code is working in my application). Are you getting errors in the console when you run it? Btw, I also put this answer on stackoverflow with the same code in snippet.
@bennettatoms can u please look at this https://github.com/angular-ui/ui-grid/issues/2196
I am moving all support question to our new google group for it so that we can keep the issue tracker for bugs only. You can find this discussion here: https://groups.google.com/forum/#!topic/ui-grid-support/unyHmZ8r1w4
I wrote two solutions. I am using Solution 1 for my project.
Solution 1:
In this Plunkr I used DatePickerPopup from angular-bootstrap and condition functions from ui-grid.
Here it is a portion of my code:
{ 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 }
If you have any comment or improvement, please let me know.
Solution 2:
I did other experiment that worked, but modifying rowSearcher.runColumnFilter function at ui-grid.js this way:
if (filter.flags.date === true) {
value = new Date(value);
// If the term has a dash in it, it comes through as '\-' -- we need to take out the '\'.
if (!(term instanceof Date))
{
term = new Date(term.replace(/\\/g, ''));
}
else
{
// Now you can compare the two dates as DATES only without worrying about time elements.
value.setHours(0,0,0,0);
}
}
For this solution, the field should be defined this way:
{ 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: uiGridConstants.filter.GREATER_THAN_OR_EQUAL,
placeholder: 'Greater than or equal',
flags: { date: true }
},
{
condition: uiGridConstants.filter.LESS_THAN_OR_EQUAL,
placeholder: 'Less than or equal',
flags: { date: true }
}
],
headerCellClass: $scope.highlightFilteredHeader }
Hope it helps.
@ComeOutToPlay: I'm trying to implement above solution 1 using angularjs, ui-grid and bootsrap, Somehow calander popup is not showing where user can pick the date range for filtering data set. No Javascript error prompt when i clicked on calendar button.
I checked and compared my script and all libraries with your plunker link all are in sync. Could you please suggest if you didn't mentioned and it's part of solution OR I'm missing something in my code.
Sorry for the delay. I was not working with ui-grid until now and I did not see your question. If you have problems with the popup, it is an styling problem probably. That's why in the plunkr the example works, but in the real life (your site) maybe not.
@ComeOutToPlay I'm using your plunkr http://plnkr.co/edit/sUr5it4qeQZ2JTgSzlCg?p=preview but I dont know why my date cant get the format "dd/MM/yyyy" when I selected the day. I already tried change all format in your plunkr but still doesn't work. You know how I can fix?