简体   繁体   中英

Filtering in Angular using a slight complex boolean condition

I am struggling slightly with my filters in AngularJS. Essentially, I have a checkbox like this:

<div class="col-sm-2" style="text-align: right">
    <label for="include-deleted-search" class="control-label">Include Deleted</label>
</div>
<div class="col-sm-2">
    <input ng-model="search.includeDeleted" type="checkbox" id="include-deleted-search"/>
</div>

which I am trying to use to filter airlines if (the item in the filter is deleted, and includeDeleted is true) or the item in the filter is not deleted.

My table looks like this, but note the { isDeleted: search.includeDeleted } filter is not working as I'd like.

<table class="table table-hover table-striped table-bordered">
    <thead>
        <tr>
            <th>Ariline ID</th>
            <th>Airline Name</th>
            <th>Country</th>
            <th>Telephone</th>
            <th>24hr Telephone</th>
            <th>Email Address</th>
            <th>Deleted</th>
        </tr>
    </thead>
    <tr dir-paginate="airline in airlines | filter: { airlineName: search.airlineName||'' } | filter: { country: search.countryName||'' } | filter: { isDeleted: search.includeDeleted||!isDeleted }  | itemsPerPage: pageSize" current-page="currentPage">
        <td>{{ airline.airlineId }}</td>
        <td>{{ airline.airlineName }}</td>
        <td>{{ airline.country }}</td>
        <td>{{ airline.telephoneNumber }}</td>
        <td>{{ ariline.telephone24Hour }}</td>
        <td>{{ airline.emailAddress }}</td>
        <td>{{ airline.isDeleted }}</td>
    </tr>
</table>

How can I express this as a filter condition? Do I need to write a custom filter? Everything else is working correctly.

If I understand your intention correctly, you can set the expression based on the search.includeDelted . If it's true, return undefined (assuming isDeleted is a boolean value) which will not filter any of the items, otherwise return false, which will return only those who are falsy:

filter:{isDeleted: search.includeDeleted?undefined:false}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM