The issue with the AngularJS filter seems to be arising specifically when applied to

My AngularJS filter isn't functioning properly when used with an Object.

Here's the View:

<input type="text" ng-model="searchKeyUser.$" placeholder="Search Keys" class="form-control"><br>
<ul class="list-group">
    <li class="list-group-item clickable keys" ng-repeat="key in keyUsers | filter:searchKeyUser" ng-class="{ active:Group.isActiveKey(key.Key.id)}" ng-click="Group.show()">
          {{key.Key.key}}{{key.show}}
    </li>
</ul>

I've tried using searchKeyUser.$, searchKeyUser.Key.$, searchKeyUser.Key.key, but it didn't work as expected. However, the text {{key.Key.key}}{{key.show}} is displaying correctly.

EDIT: JavaScript code snippet:

(function() {
  var app = angular.module('groupUsers', []);
  app.controller('GroupsController', function($scope, $http){

    //Fetching Relationships Users Keys
    $http.get(myBaseUrl + '/QlikRelKeyUser/returnGroups').success(function(data, status, headers, config) {
      $scope.keyUsers = data;
      console.log($scope.keyUsers);
      for(x in $scope.keyUsers){
        aux = 0;
        qtd = $scope.keyUsers[x].QlikUser.length;
        for(y in $scope.keyUsers[x].QlikUser){
          aux++;
          if(qtd == 1){
            $scope.keyUsers[x].show = ' (' + $scope.keyUsers[x].QlikUser[y].name + ')';
          }else if(aux == qtd){
            $scope.keyUsers[x].show += ', ' + $scope.keyUsers[x].QlikUser[y].name + ')';
          }else{
            if(aux==1){
              $scope.keyUsers[x].show = ' (' + $scope.keyUsers[x].QlikUser[y].name;
            }else{
              $scope.keyUsers[x].show += ', ' +       $scope.keyUsers[x].QlikUser[y].name;
            }
          }
        }
      }
      console.log('');
    }).error(function(data, status, headers, config) {
      console.log('ERROR');
      console.log(config);
    });
    $scope.searchKeyUser = {$:''};
   });

EDIT 2: Same View:

<input type="text" ng-model="searchGroup.$" placeholder="Search Groups" class="form-control"><br>
        <ul class="list-group" >
          <li class="list-group-item clickable" ng-repeat="group in groups | filter:searchGroup" ng-class="{ active:Group.isSet(group.QlikUserGroup.id)}" ng-click="Group.setActive(group.QlikUserGroup.id)">
            {{group.QlikUserGroup.name}}
            <span class='badge bg-important'>1</span>
            <span class='badge' stylee='padding-right:30px;'>{{Group.totalKeys(group.QlikUserGroup.id)}}</span>
          </li>
        </ul>

Using the Same JavaScript:

$http.get(myBaseUrl + '/QlikUserGroup/returnGroups').success(function(data, status, headers, config) {
      $scope.groups = data;
    }).error(function(data, status, headers, config) {
      console.log('ERROR');
      console.log(config);
    });

The output of console.log($scope.groups); shows:

Array[67]
0: Object
$$hashKey: "object:530"
QlikUserGroup: Object
id: "1"
name: "Name name name"
__proto__: Object
__proto__: Object
1: Object
2: Object
3: Object
4: Object
...

Answer №1

Angular filter works only with arrays, but you are trying to pass an object. So there are at least two solutions available:

  • Modify the server-side code to return an array.
  • Create an array from the object in JavaScript, similar to the following:

    $scope.keyUsers = [];
    console.log(data);
    for(x in data){
        var el = data[x];
        $scope.keyUsers.push(el);
        el.show = '('+ el.QlikUser.map(function(el){ return el.name; }).join(', ') +')';
    }
    console.log($scope.keyUsers);
    

var data = {
  '1': {
    'QlikUser': [{
      name: 'a'
    }, {
      name: 'b'
    }, {
      name: 'c'
    }, {
      name: 'd'
    }, ]
  }
}

var keyUsers = [];
console.log(data);
for (x in data) {
  var el = data[x];
  keyUsers.push(el);
  el.show = '(' + el.QlikUser.map(function(el) {
    return el.name;
  }).join(', ') + ')';
}
console.log(keyUsers);

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

If an AngularJS Form Item is hidden with jQuery's hide() method, will it still be validated?

Even though we're in 2020, I'm still working with AngularJS 1.x instead of the newer version due to work requirements. Despite this limitation, I am facing a challenge with setting up a form that requires exclusive-or validation between two field ...

Error message: Discord bot written in JS is unable to read the property 'execute' as it is undefined

I'm having trouble getting this code to work. Here is the main file snippet: const fs = require('fs'); bot.commands = new Discord.Collection(); const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith ...

Adjustable height and maximum height with overflow functionality

Currently, I am in the process of developing a task manager for my application and facing an obstacle when trying to calculate the height of a widget. My goal is to determine the maximum height (assuming a minimum height is already set) by subtracting a ce ...

Finding it difficult to grasp the concept of enabling CORS through an ajax request

I'm struggling to figure out how to enable CORS while using Ajax to send data to a remote server on a different domain. Despite researching extensively and reading numerous Stackoverflow threads, I can't seem to make sense of it all. I understand ...

Retrieve the date for the chosen time slot by utilizing the fullCalendar feature

I've been experiencing issues with a piece of code that is supposed to retrieve the date corresponding to a user-selected slot. Here's what I've tried so far: $('.fc-agenda-axis.fc-widget-header').on('mousedown', functio ...

How to use jQuery to highlight the parent element when clicking on a child element?

I'm struggling with some HTML code that looks like the following: <ul> <li class="curent"><a href="home.html">Home</a></li> <li> <a href="javascript:void(0)">Products</a> <ul ...

When invoking Javascript, its behavior may vary depending on whether it is being called from a custom

Currently, I am in the process of implementing versioning capabilities to a custom entity called MFAs. However, I have encountered a peculiar issue. The problem arises from having a JavaScript web resource that is being invoked from two different locations ...

Tips for avoiding Netlify's error treatment of warnings due to process.env.CI being set to true

Recently, I encountered an issue with deploying new projects on Netlify. After reviewing the logs, I noticed a message that had never appeared during previous successful deployments: The build failed while treating warnings as errors due to process.env.CI ...

Is there a way to specifically remove only the last row from a table?

Recently, I encountered a problem with my JS code that adds and removes table rows based on user interaction. Adding rows functioned perfectly, but the issue arose when attempting to delete rows. Instead of deleting only the last row as intended, all rows ...

Disregarding any negative values while utilizing the np.log(array) function

While attempting to calculate the logarithm of a specific column in a numpy array, like this: logSFROIIdC = np.log(data_dC[:, 9]), an error is returned by the compiler: -c:13: RuntimeWarning: divide by zero encountered in log. I understand the reason for ...

Mobile display exhibiting glitches in animation performance

I have implemented an animation in the provided code snippet. const logo = document.querySelector('.logo'); const buttons = document.querySelectorAll('.loadclass'); const html = document.querySelector('html') const cornerme ...

Transforming dates in the pandas data structure layout

After opening my data in .csv format using pandas, I am looking to change the date format from dd/mm/YYYY hh:mm:ss to simply YYYY-mm-dd. For example, from 19/11/2014 15:26:13 to 2014-11-19. How can I accomplish this transformation within a pandas data arra ...

Identify the page search function to reveal hidden content in a collapsible section

Our team has implemented an expandable box feature on our wiki (in Confluence) to condense information, using the standard display:none/block method. However, I am looking for a way to make this work seamlessly with the browser's find functionality. S ...

Unique title: "Implementing Unique Event Handlers with VueJS Components"

My VueJS and Buefy project begins with two distinct click actions: Click on the Cyan area -> redirects to another page (Action 1) Click on the Magenta area -> shows a dropdown menu (Action 2) https://i.stack.imgur.com/AVLOS.png However, when clic ...

I have an Observable but I need to convert it into a String

Seeking assistance with Angular translation service and Kendo.UI components. In the Kendo.UI documentation, it mentions the use of MessageService for component translation implementation. To achieve this, an abstract class must be extended containing a m ...

The EJS is throwing an error because it cannot define the property "round" on an undefined object

I'm currently delving into the realm of Node.js, using the "Secrets of Ninja" book as my guide. I've come across an EJS program in the book that I copied verbatim to run, but I encountered an error despite not making any modifications to the code ...

multiple server-side tables with toggle buttons

I'm struggling with a page that contains 3 tables using datatables. The issue is that datatables can't handle more than one table, and after searching for a solution, I found a customized SSP Datatables at this link: here However, I'm wonde ...

Retrieve JSON data from a PHP script to be used in an Angular scope

I am attempting to retrieve JSON data from a PHP file to use in an Angular controller. I have used json_encode(pg_fetch_assoc($result)); within the PHP file and when I check with console.log($scope.contents); in the Angular controller, the JSON data is ret ...

Insert a new element at the current scroll position without disrupting the existing scroll or view

My goal is to replicate the functionality of the Twitter Mac client. I have a scrollable box with a fixed height and multiple blocks inside. I want to add a new block before all the others, but keep it invisible to the user so they have to scroll to the to ...

Adding Roles Using Discord.js - A Simple Guide

I'm currently working on a Discord bot using Discord.js, and I am trying to implement a feature where users can use a command to assign themselves a role. However, despite my best efforts, I am unable to get this functionality to work. In my bot' ...