Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have 2 dropdowns one for fields like 'id' , 'name' etc and one for filters like 'uppercase' , 'contains' , 'endWith' etc. Filters value are custom filters name written in angularjs.I have one search textbox where the user types the search item for selected field and property.

<div>
    <label>Filter</label>
    <select ng-model="selectedFilter">
        <option value="uppercase">Uppercase</option>
        <option value="startsWithLetter">Starts With</option>
    </select>
</div>
<div>
    <label>Fields</label>
    <select ng-model="selectedField">
        <option value="Id">Id</option>
        <option value="name">startsWithLetter</option>
    </select>
</div>

<input type="text" ng-model="search"/>

Now I have writtern a general implementation to fire filter like this

app.filter('picker', function ($filter) {
return function (value, filterName) {
    return $filter(filterName)(value);
};

});

and few custom filters like this

app.filter('startsWithLetter', function () {
return function (items, letter) {
    var filtered = [];
    var letterMatch = new RegExp(letter, 'i');
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        if (letterMatch.test(item.name.substring(0,1))) {
            filtered.push(item);
        }
    }
    return filtered;
};
});

here is the $scope model

$scope.friends = [
    {
        id: 1,
        name: 'Andrew'
    },
    {

        id: 2,
        name: 'Will'
    },
    {

        id: 3,
        name: 'Mark'
    },
    {

        id: 4,
        name: 'Alice'
    },
    {

        id: 5,
        name: 'Todd'
    }];

I am struggling with a syntax to write in ng-repeat so that filter apply only on that property selected and the selected filter with that search term . if nothing selected then table will show regular data.

 <ul>
    <li ng-repeat="friend in friends | filter:{syntax }">
       {{ friend.id}} , {{ friend.name }}
    </li>
</ul>

enter image description here I am implementing this feature. Need help?

share|improve this question
    
try "friend in friends | startsWithLetter : 'A' "... but if you need a variable list of filters, you can create something like "friend in friends | myVariableFilter : myArgs " and populate "myArgs" with conditional parameters – Joaozito Polo Jan 5 at 13:55
    
filter will be based on selected property . – Ghazanfar Jan 5 at 13:57
    
Suppose user select field : name and filter: startWith and type A in search term how you will map that . { id: 1, name: 'Andrew' }, { id: 4, name: 'Alice' } – Ghazanfar Jan 5 at 13:59
    
I will make a jsbin with answer – Joaozito Polo Jan 5 at 14:03
    
thanks waiting for it – Ghazanfar Jan 5 at 14:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.