How to filter date in ngTable?

自作多情 提交于 2019-12-10 23:58:47

问题


I am trying to filter date in ngTable based on the given format d MMM yyyy HH:mm:ss. I followed this answer and created a custom filter, but it does not take the given format instead d MMM, yyyy.

How can I have filter on ngTable to filter dates in a given format? Here is my plunker

ngTable

<td data-title="'Start Date'" class="text-center" header-class="text-left" filter="{ 'start_date': 'text' }" sortable="'type'">{{item.start_date| date: 'd MMM yyyy HH:mm:ss'}}</td>
        <td data-title="'End Date'" class="text-center" header-class="text-left" filter="{ 'end_date': 'text' }" sortable="'type'">{{item.end_date| date: 'd MMM yyyy HH:mm:ss'}}</td>

Custom filter

filter('customUserDateFilter', function($filter) {
    return function(values, dateString) {
        var filtered = [];
        if (typeof values != 'undefined' && typeof dateString != 'undefined') {
            angular.forEach(values, function(value) {
                var source = ($filter('date')(value.start_date)).toLowerCase();
                var temp = dateString.toLowerCase();
                //if ($filter('date')(value.start_date).indexOf(dateString) >= 0) {
                //if (temp.indexOf(" ") >=0)
                    //debugger;
                if (source.indexOf(temp) >= 0) {
                    filtered.push(value);
                }
            });
        }
        return filtered;
    }
})

回答1:


You have to be careful when you are changing the format of the date. This is because the filter formats the date which has to be the same format as shown in the table to ensure correct functionality:

var source = ($filter('date')(value.start_date)).toLowerCase();

must be changed to this:

var source = ($filter('date')(value.start_date, 'd MMM yyyy HH:mm:ss')).toLowerCase();

Here is the working plunkr.



来源:https://stackoverflow.com/questions/33398054/how-to-filter-date-in-ngtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!