应用更改字符的过滤器后按名称过滤

时间:2023-02-09 16:32:27

I've come across a situation where I need to filter elements created by the 'ng-repeat' directive, but I've applied a custom filter that swaps one character by another and vice versa, for each created element.

我遇到过一种情况,我需要过滤'ng-repeat'指令创建的元素,但我已经为每个创建的元素应用了一个自定义过滤器,将一个字符交换为另一个字符,反之亦然。

Then, if I search for the new character that was swapped, the filter won't find it - unless I search for the old one.

然后,如果我搜索交换的新字符,过滤器将找不到它 - 除非我搜索旧字符。

How do I apply this input filter after using my custom filter which swaps characters?

在使用交换字符的自定义过滤器后,如何应用此输入过滤器?

In my custom filter brNumber, dots are swapped by commas and vice-versa, so if I search for a dot the filter will find only the ones with comma.

在我的自定义过滤器brNumber中,点用逗号交换,反之亦然,因此如果我搜索一个点,过滤器将只找到带逗号的点。

FIDDLE

小提琴

< HTML >

<div 
ng-app="myApp" 
ng-init="
    person=
    [
        {firstName:'Johnny',lastName:'Dowy'},
        {firstName:'Homem,25',lastName:'Cueca,Suja'},                
        {firstName:'Alleria.Donna',lastName:'Windrunner'}
    ];"
>

First Name: <input type="text" ng-model="firstName">
<br />
The persons's objects have: | <span ng-repeat="i in person | orderBy: 'firstName' | filter:firstName">{{ ( i.firstName + ' ' + i.lastName ) | brNumber }} | </span>

{ Javascript.js }

{Javascript.js}

app.filter( 'brNumber', function()
{
    return function( text )
    {
        string = text.toString();        
        returnString = '';

        for ( i = 0; i < string.length; i++ )
        {
            returnString += string[i] ===  ',' ? '.' :
            (
                string[i] === '.' ? ',' : string[i]
            );
        }

        return returnString;
    }
});

1 个解决方案

#1


4  

You can wrap a filtered value in same function as a filter in view. Check this https://jsfiddle.net/5Lcafzuc/3/

您可以将筛选值包装在与视图中的筛选器相同的函数中。检查这个https://jsfiddle.net/5Lcafzuc/3/

/// wrap filter argument in function
ng-repeat="i in person | orderBy: 'firstName' | filter: replace(firstName)"

/// add function in scope and use it in display filter
var replace = function(text) {        
         if(!text) {
           return false;
         }         

         if(text.indexOf(".") >= 0) {
           text = text.replace(".", ",");
         } else if(text.indexOf(",") >=0) {
           text = text.replace(",", ".");
         }

         return text;
      }

app.controller('myCtrl', function($scope) {    
    $scope.replace = replace;
});

app.filter( 'brNumber', function() {
    return function(text) {        
         return replace(text);
    }
});

#1


4  

You can wrap a filtered value in same function as a filter in view. Check this https://jsfiddle.net/5Lcafzuc/3/

您可以将筛选值包装在与视图中的筛选器相同的函数中。检查这个https://jsfiddle.net/5Lcafzuc/3/

/// wrap filter argument in function
ng-repeat="i in person | orderBy: 'firstName' | filter: replace(firstName)"

/// add function in scope and use it in display filter
var replace = function(text) {        
         if(!text) {
           return false;
         }         

         if(text.indexOf(".") >= 0) {
           text = text.replace(".", ",");
         } else if(text.indexOf(",") >=0) {
           text = text.replace(",", ".");
         }

         return text;
      }

app.controller('myCtrl', function($scope) {    
    $scope.replace = replace;
});

app.filter( 'brNumber', function() {
    return function(text) {        
         return replace(text);
    }
});