issues with search functionality in angular

Just diving into the world of angular and struggling with implementing a 'live search' functionality. I've stored my JSON data as a variable in a JavaScript file and successfully displayed it in the HTML. I also have a 'list' radio button that changes the display size, working like a charm. However, things get tricky when I try to access the reverse filter option using another radio button. The goal is to filter the book titles in reverse order by their names, but nothing seems to happen. If you want to take a look at the JSON structure, you can find it here. Below is the code snippet where I attempt to perform the reverse filter action:


<div class="container result">
  <div ng-class="list ? 'col-md-6' : 'col-md-4'" class="books" ng-repeat="books in books | filter:search | orderBy:'books.doc.name':reverse"> <a href="https://address/{{books.doc.name}}" target="_blank">
    <img ng-class="list ? 'col-md-2' : 'col-md-12'" ng-src="{{books.doc.thumbnail_590_url}}" alt="Click to read {{books.doc.name}}" title="Click to read {{books.doc.name}}" class="img-thumbnail" /></a>
    <h4 ng-class="list ? 'col-md-10' : 'col-md-12'">{{books.doc.name}}</h4> 
  </div>

And here's the JavaScript:


angular.module("myApp",["ngSanitize"])
.filter('replace', function () {
var pat = / /gi;
return function (text) {
    var clean = text.replace(pat, "-");
    var temp = clean.split("---");
    if (temp.length>1) {
      clean = temp[0];
    }
    return clean;
};
})
.controller("Example", function($scope, $interval) {
$scope.search = "orig";
$scope.books = books;
$scope.reverse = false;
$scope.list = false;
});  

Answer №1

After some troubleshooting, I was able to solve the issue. I discovered that Angular filter does not work with a json object, it requires the data to be in the form of a json array. To convert my data to a json array, I removed the outer curly brackets and replaced them with square brackets. Additionally, I removed any numbers and colons like "1272:" that were present in the original json file. These adjustments enabled me to successfully utilize the live search functionality.

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

Discover the color value within an array that begins with the "#" symbol

In my PHP code, I have written a function that extracts values from a CSS file and creates an array. Now, I need to loop through this array and create another array that only contains color values (strings starting with #). The challenge is that the length ...

Oops! It seems like you've stumbled upon a 404 error on Django

I need to retrieve the price value immediately after a product is selected in the sale form. The sale form includes fields for price, quantity, and product. Once a user selects a product, the price of that product should populate the input box for price. T ...

I attempted to log the elements of my submit button form, but unfortunately nothing is appearing in the browser console

I am attempting to capture the data from my submit button form, but for some reason, nothing is showing up in the console of my browser. $("#signupform").submit(function(event){ // Stopped default PHP processing event.preve ...

The function $scope.removeNinja cannot be found

As a beginner in Angular, I encountered an issue while working on a project that involved creating a Directory of ninjas with various functionalities implemented using Angular. Here is a snippet of my HTML file: <!DOCTYPE html> <html lang="en" n ...

Utilizing Backbone's setInterval function to periodically update all models within a collection at 10-second intervals

Is there a way to automatically update my view every 10 seconds? Or can I rerender or refetch the view only when something in my JSON file changes? Main JavaScript: var AppRouter = Backbone.Router.extend({ routes: { "" : "categories", }, initializ ...

Is the default behavior of Ctrl + C affected by catching SIGINT in NodeJS?

When I run my nodejs application on Windows, it displays ^C and goes back to the cmd prompt when I press Ctrl + C. However, I have included a SIGINT handler in my code as shown below: process.on('SIGINT', (code) => { console.log("Process term ...

ionic rating that cannot be selected for the average score

<rating ng-model="newRate.rating" max="max" class="assertive"></rating> Is there a way to disable clicking on Ionic rating component? ...

Unable to parse JSON string as an array in PHP

I am in need of assistance. I am having trouble converting a string to a JSON array using PHP. Below is the code I am working with. $education=$_POST['education']; The above line produces the following output [{'name':'aaa', ...

Is there a way to implement prototype inheritance without contaminating an object's prototype with unnecessary methods and properties?

I prefer not to clutter the object prototype with all my library's methods. My goal is to keep them hidden inside a namespace property. When attempting to access an object property, if it is undefined, the script will search through the prototype cha ...

Tips for configuring CakePHP to trigger the second submit button when the enter key is pressed

My form includes two submit buttons: "cancel" and "find." While both buttons work correctly when clicked, pressing the enter key always triggers the submission of "cancel." I don't want to change the button order in the form. To address this issue, I ...

When attempting to upload a jsonArray to php, I encountered an issue where I received an error stating that the org.json.JsonArray

I am encountering an issue while attempting to send a jsonArray to a PHP server. The error message states: "Error while posting org.json.JSONArray cannot be converted to JSON Object." Please inform me about what might be causing this problem. Here is the ...

Exploring the integration of data from two Firestore collections using JavaScript

I manage two different types of collections, one being called CURRENCY-PAIR and the other Alerts. The collection CURRENCY-PAIR includes the following information: Currency-Pair Name Currency-AskPrice Currency-BidPrice On the other hand, the Alerts colle ...

One-way communication between two clients using Socket.io

While working on a basic socket.io application using React and Express, I've encountered an issue where two clients are facing difficulties in sending data to each other. For instance: Player 1 connects to the server followed by Player 2. Player 1 ...

Is your event listener failing to activate?

I have been working on a project where I gather array elements from user input and try to display them in an HTML table. Unfortunately, the event listener seems to not be triggering, and I can't figure out why. Here is the relevant HTML code: ...

What are strategies for handling intricate state logic within my Vue.js checkbox component?

I'm facing a few challenges as I dive into learning Vue.js for just one week. Keep in mind that this example is actually nested within a parent component called <question>, but for the sake of simplicity, I've streamlined the code for this ...

Tips on handling json files and constructing an lstm model?

I'm currently working on implementing LSTM on the HP news dataset, which is stored in JSON format (). I attempted to run this code and encountered errors. I'm unsure what's causing the issue with this code? from keras.layers import LSTM, Act ...

Utilize dynamic function calls in JavaScript

I am trying to dynamically call a function from a string like "User.find". The goal is to call the find() function in the User object if it exists. This is what my attempt looks like: var User = {}; User.find = function(){ return 1; } var input ...

What is the best approach to incorporating Ant-design-vue via cdn in my project?

I've been working on a Vue macro application for specific functionality in NetSuite. Since I can't utilize npm or other package installers, I've resorted to using CDN. The Vue app and Ant Design are both functioning properly, but the issue l ...

JavaScript and jQuery: The Power of Dynamic Arrays

Even though my var email contains a string data, why does my array length always turn out to be 0? (I've confirmed that the data is there by using alert on var email). var emails = new Array(); //retrieve all the emails $('.emailBox ...