Insert dynamic filter into an Angularjs Expression

Asked

Viewed 351 times

3

I am trying to insert a dynamic filter according to a field of an object in the ng-grid. I tried so many ways and I couldn’t:

$scope.gridOptions.columnDefs = [
    { displayName: 'entity.tipoPessoa', field: 'tipoPessoa'},
    { displayName: 'entity.id', field: 'id', cellTemplate: '<div class=\"ngCellText\" ng-class=\"col.colIndex()\"><span ng-cell-text>[Insira o trecho de código aqui]</span></div>'},
    //...
]:

Code snippet:

{{ row.entity.tipoPessoa == "F" ? row.entity.id | brCpf : row.entity.id | brCnpj }}

{{ row.entity.tipoPessoa == "F" ? "row.entity.id | brCpf" : "row.entity.id | brCnpj" }}

{{ row.entity.tipoPessoa == "F" ? "{{row.entity.id | brCpf}}" : "{{row.entity.id | brCnpj}}" }}

{{ row.entity.tipoPessoa == "F" ? {{row.entity.id | brCpf}} : {{row.entity.id | brCnpj}} }}

None of these ways worked. What’s the right way to do it? Should I try another approach?

  • Angular does not support operator ?. Use a span with ngIf. The filters brCpf, etc, are set and working correctly in a simple case? Which error got? What was expected? Any error in the console?

  • There was no error in the console, but it presented the string (Row.entity.id | brCpf) and not "ran it".

1 answer

2

I decided to create a custom filter, surely there must be another way of doing it, but I leave here the solution used:

app.filter('CpfCnpj', ['$filter', function($filter) {
    return function(CpfCnpj) {
        var cpfSize = 11;
        return CpfCnpj.length <= cpfSize ? $filter('brCpf')(CpfCnpj) : $filter('brCnpj')(CpfCnpj);
    };
}]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.