3

I'm designing universal table that reads data and columns from ajax. In columns description is also filter name which angular should use for a specific column. But in HTML templates I can't use variables for filter names:/ Is there a solution for that? Or should I code javascript loop with data source?

Here is code example:

<tr ng-repeat="item in data">
    <td ng-repeat="col in cols">
        {{item[col.source]}}
        <span ng-if="col.ngFilter">
            {{col.ngFilter}} // ex. "state" filter
            {{item[col.source]|col.ngFilter}} //is not working - is looking for "col.ngFilter" not "state" filter.
        </span>
    </td>
</tr>

2 Answers 2

8

You cannot do it in your HTML. First, you need to apply the filter in your controller.

function MyCtrl($scope, $filter) {

    $scope.applyFilter = function(model, filter) {
        return $filter(filter)(model);
    };

}

Then, in your HTML:

Instead of

{{item[col.source]|col.ngFilter}}

use

{{applyFilter(item[col.source], col.ngFilter)}}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for response - I put all filter handeling in javascript.
0

For anyone looking to do something like

{{applyFliter(item[col.source], col.ngFilter)}} 

where ngFilter might contains some colon separated parameters such as

currency:"USD$":0

I ended up writing this little helper

function applyFilter (model, filter){
    if(filter){

        var pieces = filter.split(':');

        var filterName = pieces[0];

        var params = [model];

        if(pieces.length>1){
            params = params.concat(pieces.slice(1));
        }

        return $filter(filterName).apply(this,params);
    }else{
        return model;
    }
}

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.