0

I have an ng-repeat list displaying a collection of objects. I've implemented a filter that makes the list display only objects that have been inserted today. It looks like this:

<div class="row msf-row" 
     ng-repeat="record in recordlist | filter: record.date = dateJson">

This is working fantastic. My problem is that I want to show both objects recorded today, and also records that don't have the property arrival (as they might have been introduced yesterday, but they need to be marked as arrived).

I've tried this but till today at 00.00 won't know if it's correct.

<div class="row msf-row" 
     ng-repeat="record in recordlist | 
                filter: record.date = dateJson ||  
                        record.date != dateJson && !record.arrival">

Does this make sense?

2 Answers 2

1

When the expressions in your markup start to get this overwrought, it's time to refactor. You should define a function in your controller that carries out the check that you want:

$scope.shouldShow = function(record) {
    return record.date == dateJson ||
           (recode.date != dateJson && !record.arrival);
}

(note: this condition is redundant; you can shorten it to):

    return record.date == dateJson || !record.arrival;

Then use that in your filter:

<div class="row msf-row" ng-repeat="record in recordlist | filter:shouldShow">

This will allow you to update the logic without editing your view.

Sign up to request clarification or add additional context in comments.

5 Comments

I don't get to make it work. The conditions should be, show me only records that either have today's date (dateJson) or that don't have record.arrival (record.arrival = ''), independently of date. What am I missing?
What do you mean when you say you couldn't get it to work? What result are you getting? BTW, you seem to be using = where you should be using === (or maybe ==). You know the difference between those, right?
Is filtering everything out (no results). Maybe the problem is that record.arrival exists but is empty?
Could you edit your question to show your latest attempt, or put it in some place like PasteBin?
Fixed it, it was a typo : )
0

In case you're looking to create a custom filter, you can try this. Here I assume that
app = angular.module('yourModuleName', []) or app = angular.module('yourModuleName') (later, incase you've already instantiated your module before)

<div class="row msf-row" ng-repeat="record in recordlist | customFilter:dateJson">

app.filter('customFilter', function(){
    return function(records, dateJson){
        return records.filter(function(item, index, array){
            return item.date == dateJson || (item.date != dateJson && !item.arrival);
        });
   }
})

Disclaimer: I haven't tested this code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.