Refine your search by name following the implementation of a character-altering filter

Encountered a scenario in which there is a need to filter elements generated by the 'ng-repeat' directive. I have implemented a custom filter that replaces one character with another and vice versa for each element created.

However, when attempting to search for the newly swapped character, the filter does not recognize it - only searches for the original character.

How can I apply an input filter after applying my custom filter that swaps characters?

In the custom filter brNumber, dots are replaced by commas and vice-versa. So, searching for a dot will only yield results with commas.

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 }

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;
    }
});

Answer №1

To implement a filtered value within the same function as a filter in the view, you can follow this example: 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);
    }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Despite being logged, the current value of firebase.auth().currentUser in Firebase is null

I have coded a query in my service.TS file that displays the "state" of items based on the UID of the logged-in user: getLists(): FirebaseListObservable<any> { firebase.auth().onAuthStateChanged(function(user) { if (user) {console.log("blah", fir ...

Link to download an Excel spreadsheet

My server is capable of generating excel files dynamically, and I am utilizing AJAX to download these dynamic excel files. Upon successful completion in the callback function, I receive the data for the excel file. $.ajax({ url: exporting. ...

Discover the power of the "Load More" feature with Ajax Button on

After browsing through the previous questions and experimenting with various techniques, I've come close to a solution but still can't get it to work. The closest example I found is on Stack Overflow: How to implement pagination on a custom WP_Qu ...

What causes the result of UNDEFINED when accessing an ENVIRONMENT VARIABLE in a REACT application?

Unfortunately, I've hit a roadblock with this seemingly simple question and can't seem to find the answer. I'm having trouble accessing environment variables in a specific file. I am using create-react-app (^16.11.0) The .env file is loca ...

Having trouble retrieving an object through a GET request in Node.js with Express

Currently, I am going through a tutorial on Node.js by Traversy Media and I have encountered an issue that has me stumped. The problem arises when I attempt to retrieve the response of a single object from an array of objects using the following code: app ...

What is the best way to retrieve the value of the "Observer" object?

I am a user of Vue.js and I have the following data in this.suspendedReserveMemo: this.suspendedReserveMemo [__ob__: Observer]650: "memo3"651: "memo4"652: ""653: ""654: ""655: ""656: ""657: ""658: ""659: ""660:""661: ""662: ""length: 663__ob__: Observer {v ...

The assigned type does not match the type 'IntrinsicAttributes & { children?: ReactNode; }'. This property is not assignable

I have been struggling to resolve this issue, but unfortunately, I have not found a successful solution yet. The error message I am encountering is: Type '{ mailData: mailSendProps; }' is causing an issue as it is not compatible with type &apos ...

Sending data from configuration to factory in AngularJS and UI Router

Trying to perform a search in an http call with a dynamic value based on the current view. The factory setup is as follows: .factory('SearchService', ['$http','$filter', function($http, $filter) { var service = { get ...

Enabling Context Isolation in Electron.js and Next.js with Nextron: A Step-by-Step Guide

I've been working on a desktop application using Nextron (Electron.js + Next.js). Attempting to activate context isolation from BrowserWindow parameters, I have utilized the following code: contextIsolation: true However, it seems that this doesn&ap ...

Jquery Parallax causing scrolling problems across different browsers

Could you please take a look at the following link in both Chrome and Firefox? I am encountering some unusual issues. Primary Concern The scrolling function works smoothly in Firefox, but it seems to be malfunctioning in Chrome. Secondary Problem Whe ...

What is the best way to refine the results from an AJAX request in Datatables?

I have successfully configured a Datatables plugin, set up a new table, and populated it with content using an AJAX call: var table= $("#mytable").DataTable({ ajax: "list.json", columns: [ {"data": "name"}, {"data": "location"}, ...

What is the best way to make my if statement pause until a GET request finishes (GUARD) with the help of Angular?

I am currently working on implementing admin routes for my Angular app, and I have used a role guard to handle this. The code snippet below showcases my implementation: However, I would like the get request to finish executing before the if statement begi ...

Do we need to specify ngRoute in the angular module configuration?

Being new to angular.js, I have a question about ngRoute - is it necessary to define it on the angular module? From what I understand, it is required if we want to update the view based on URL changes. But can we also manually define routes and return vi ...

What is the process of creating a MaterialUI checkbox named "Badge"?

Badge API at https://material-ui.com/api/badge/ includes a prop called component that accepts either a string for a DOM element or a component. In my code: <Badge color="primary" classes={{ badge: classes.badge }} component="checkbox"> <Avatar ...

Learn how to update image and text styles using Ajax in Ruby on Rails with the like button feature

I'm working on implementing a Like button in Rails using Ajax, similar to this example: Like button Ajax in Ruby on Rails The example above works perfectly, but I'm interested in incorporating images and iconic text (such as fontawesome) instead ...

Python (Selenium) - Guide on extracting and storing data from multiple pages into a single CSV file

I am facing a challenge with scraping data from a web page that contains a table spanning multiple pages. I am using Python with selenium for this task. The website in question is: The issue I am encountering is related to navigating through the pages of ...

Using the Match() function with an array of objects

I am working with an object array containing multiple variables. let Novels = []; class Novel { constructor(isbn, title, author, edition, publication, year) { this.isbn = isbn; this.title = title; this.author = author; this.publicat ...

How CSS can affect the layout of elements on a webpage

I am looking to achieve something similar to this: The left box should maintain a fixed width, while the right one resizes to fit the full width of the browser window. I also need the height of both boxes to be resizable. Apologies for any inconvenience ...

Retrieving a json file from a local server by utilizing angularjs $http.get functionality

After fetching a JSON file from localhost, I can see the data when I console log. However, when I inject the Factory into my controller, it returns a null object. This indicates that the variable errorMessage does not receive the JSON object because the ...

Why does the for loop assign the last iteration of jQuery onclick to all elements?

I've encountered an issue with my code that I'd like to discuss var btns = $('.gotobtn'); $('#'+btns.get(0).id).click(function() { document.querySelector('#navigator').pushPage('directions.html', myInf ...