Last active
August 29, 2015 14:04
-
-
Save silasrm/1be824d00273ffdf8ee0 to your computer and use it in GitHub Desktop.
Filtro de período para ngRepeat do AngularJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ########################################################################### // | |
| // ########################################################################### // | |
| // - DateRange - // | |
| // ########################################################################### // | |
| // #### Adaptado de http://stackoverflow.com/a/21176675/2417962 ##### // | |
| // ########################################################################### // | |
| // #### Exemplos: ##### // | |
| // #### 1. Filtra os registros até data de hoje, usando a ##### // | |
| // #### propriedade 'data_ocorrencia' (sem aspas) do objeto, ##### // | |
| // #### sem incluir a data de hoje ##### // | |
| // #### ng-repeat="plantao in plantoes[turma.id] ##### // | |
| // #### |dateRange:'data_ocorrencia':false:dataHoje:false" ##### // | |
| // #### ##### // | |
| // #### 2. Filtra os registros da data de hoje em diante, usando a ##### // | |
| // #### propriedade 'data_ocorrencia' (sem aspas) do objeto, ##### // | |
| // #### incluindo a data de hoje ##### // | |
| // #### ng-repeat="plantao in plantoes[turma.id] ##### // | |
| // #### |dateRange:'data_ocorrencia':dataHoje:false" ##### // | |
| // ########################################################################### // | |
| define( | |
| [ | |
| 'app', | |
| ], | |
| function(app) { | |
| app | |
| .lazy | |
| .filter('dateRange', function () { | |
| return function(collection, field, startDate, endDate, inclusive) { | |
| var result = []; | |
| if(typeof inclusive === 'undefined') { | |
| var inclusive = true; | |
| } | |
| // date filters | |
| if(startDate === false && endDate !== false) { | |
| var endDate = (endDate && !isNaN(Date.parse(endDate))) ? Date.parse(endDate) : new Date().getTime(); | |
| } else if(startDate !== false && endDate === false) { | |
| var startDate = (startDate && !isNaN(Date.parse(startDate))) ? Date.parse(startDate) : 0; | |
| } else { | |
| var startDate = (startDate && !isNaN(Date.parse(startDate))) ? Date.parse(startDate) : 0; | |
| var endDate = (endDate && !isNaN(Date.parse(endDate))) ? Date.parse(endDate) : new Date().getTime(); | |
| } | |
| // if the collection are loaded | |
| if (collection && collection.length > 0) { | |
| for(var index in collection) { | |
| var item = collection[index]; | |
| if (typeof item === 'undefined') { | |
| continue; | |
| } | |
| var itemDate = new Date(item[field]); | |
| if (endDate == false) { | |
| if (inclusive) { | |
| if (itemDate >= startDate) { | |
| result.push(item); | |
| } | |
| } else { | |
| if (itemDate > startDate) { | |
| result.push(item); | |
| } | |
| } | |
| } else if (startDate == false) { | |
| if (inclusive) { | |
| if (itemDate < endDate) { | |
| result.push(item); | |
| } | |
| } else { | |
| if (itemDate < endDate) { | |
| result.push(item); | |
| } | |
| } | |
| } else { | |
| if (inclusive) { | |
| if (itemDate >= startDate && itemDate <= endDate) { | |
| result.push(item); | |
| } | |
| } else { | |
| if (itemDate > startDate && itemDate < endDate) { | |
| result.push(item); | |
| } | |
| } | |
| } | |
| } | |
| return result; | |
| } | |
| }; | |
| }); | |
| } | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment