Using AngularJS to inject a variable into the standard filter

Currently, I am attempting to develop a custom filter that mimics the functionality of the standard Angular filter in HTML, with the distinction that it accepts a variable as input rather than a fixed value.

For instance, within my html document, you may come across something similar to this:

<tr ng-repeat = "item in Items | filter:{function:'1300'}"> 
  <td>[[item.name]]</td>
</tr>

However, I aspire to have the flexibility to provide a variable instead of '1300', whose actual value can be determined when the user interacts with a button (which corresponds to a specific value).

I've attempted to replicate the built-in filter function by creating a filter.js file and invoking it within my controller without success.

If anyone could offer assistance, it would be greatly appreciated!

Answer №1

If you're looking to implement this functionality, check out this jsBin example:

<tr ng-repeat = "item in Items | filter:{function:theFilter}"> 
  <td>{{item.name}}</td>
</tr>

Your button setup could be like the following:

<button ng-click="setFilter(1300)">1300</button>
<button ng-click="setFilter(1400)">1400</button>
<button ng-click="setFilter(1200)">1200</button>

Here's how your controller method would look:

$scope.setFilter = function(filter) {
    $scope.theFilter = filter;
}

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

Reversing the order of input-group-addon and input in bootstrap on mobile devices

I attempted to adjust the layout of a bootstrap input-group-addon on mobile devices by using two input elements and manipulating their display and visibility properties. From a design standpoint, I achieved the desired result as the input is now positione ...

Organizing and recycling React styled-components and native elements

As a backend engineer transitioning to frontend development, I am currently learning React and exploring styled-components for styling components. However, I am facing challenges in using styled-components with native React components and updating them in ...

Keep an ear out for events coming from the identical Socket.IO socket

Currently, I am in the process of testing an Angular service that wraps Socket.IO. In order to successfully conduct this test, it is crucial for me to monitor events being emitted by the same socket that I am setting up. Below is the code snippet of the s ...

template for displaying data in Vue.js using props

In my application, I have a component that I frequently use to display tables. Now, I want to enhance this component by allowing the customization of table fields (templates) through props in the parent component. This way, I can avoid constant edits to th ...

Difficulty encountered when trying to update intricate model using Angular UI modal

My current project involves managing a model containing nested arrays that represent different sections of a floor plan. Each section contains an array of booth objects. To optimize user interaction, I've created a view that displays all the booths on ...

What could be causing the malfunction of removeEventListener in my Nuxt application?

Whenever a search result is displayed on my app, the component below renders and checks if the user scrolling is at the bottom of the page. Initially, the code works fine, but I encounter an error when returning to the page after navigating away and scro ...

Animation is not applied to Bootstrap Collapse on a row within a table

My Bootstrap 4 table has an issue - when I apply the "collapse" class to a table row, it behaves differently compared to applying it to a div element. Clicking "animate div" smoothly animates the target div, but clicking "animate tr" does not animate the ...

Passing default props to a component in React that includes a function as one of the

I am working on a React component that has default props set. The issue arises when I try to pass an additional prop, specifically a function. class MyComponent extends Component { constructor(props) { console.log('props', props); supe ...

Use regular expressions to locate all closing HTML tags and all opening HTML tags individually

My JavaScript function is currently filtering strings by removing all HTML tags. However, I have now realized that I need to perform two separate operations: first, replacing all closing tags with <br>, and then removing all opening tags. return Str ...

Achieve a customized glow effect by blending FragColor with UnrealBloom in ThreeJS using GLSL

I am interested in implementing selective bloom for a GLTF model imported into ThreeJS using an Emission map. To accomplish this, the first step is to make objects that should not have bloom completely black. The plan includes utilizing UnrealBloomPass an ...

Passing a table value to a PHP script using JQuery on click event

I am struggling with implementing functionality to filter a dataset based on the link that the user clicks on within a bootstrap modal. The modal contains a morris.js graph and I need to pass the clicked value (e.g. Cluster1 or Cluster2) to a data pull scr ...

JavaScript: Identify the variable that has been updated

When checking for changes in the values of a couple of variables, I use the following condition: <% if (ctx.recipient.@firstName != ctx.recipient.@firstName_init || ctx.recipient.@lastName != ctx.recipient.@lastName_init || ctx.recipient.@emailPreferred ...

Error in ThreeJS: The constructor THREE.FaceNormalsHelper is not defined

I am currently studying a course on three.js and encountered an issue in Chrome version 79 while progressing through the material: "THREE.FaceNormalsHelper is not a constructor". The line causing trouble is as follows: normals = new THREE.FaceNormalsH ...

Obtain selected values from another view

When I have a list of checkboxes and a submit button, clicking on the submit button should display all checked values in another view table. Most solutions suggest getting the values in the same view, but I specifically need to retrieve the values in a dif ...

Conditional Matching with Javascript Regular Expressions

My strings are formatted like this: #WTK-56491650H #=> want to capture '56491650H' #M123456 #=> want to capture 'M123456' I am trying to extract everything after the # character, unless there is a dash. In that case, I onl ...

Transferring information and storing it in a textbox

I have a homepage that features a popup window. <textarea class="form-control item"></textarea> <button type="button" class="btn btn-primary" name="name">Send</button> Additionally, there is a secondary page at (/conclusion/main) ...

Converting and storing Canvas data as a blob, and then saving the blob as

There is a clickable icon on the page that triggers an action when clicked. Currently, the icon is set as an anchor tag. However, I want to change it to a div like all the other icons in my UI. But for some reason, the following code isn't working. W ...

Omit specific TagNames from the selection

How can I exclude <BR> elements when using the statement below? var children = document.getElementById('id').getElementsByTagName('*'); Is there a special syntax for getElementsByTagName that allows me to achieve this, or is the ...

Transferring data between nested IFrames and triggering a page refresh

Looking for a way to pass multiple values between two embedded IFRAMES and refresh the receiving iframe src. Is there any solution you recommend, specifically in c# code behind file (asp.net)? Welcome any ideas and suggestions. Thank you. ...

Creating a function that writes to a file by utilizing the data input from

After running the provided code snippet, it successfully works in a standalone project. However, I am interested in making modifications to replace the variable "sample_text" with an output that is displayed in the terminal instead of being hardcoded int ...