The filtering feature for array and model selection in Angular's UI-Select appears to be malfunctioning

Below is a Json structure:

$scope.people = [
{ name: 'Niraj'},
{ name: 'Shivam'},
{ name: 'Arun'},
{ name: 'Mohit'}]

There's also a variable, var array = "Niraj,Shivam";. My goal is to filter out the names from the array and ensure that the default selection in Angular UIselect matches the array value.

https://i.stack.imgur.com/s8Gox.jpg

Refer to the figure provided. I also want to ensure that only newly added values are displayed upon clicking the "click me" button (Desired outcome). If all values are shown, it should not be an issue.

Check out my plnkr.

Answer №1

After much searching, I have finally discovered the solution for filtering and listing values based on conditions in my demo.js file.

'use strict';

var app = angular.module('demo', ['ngSanitize', 'ui.select']);

app.controller('DemoCtrl', function ($scope, $http, $timeout, $interval) {

  $scope.oldArray = [];
  $scope.people = [
    { name: 'Niraj'},
    { name: 'Shivam'},
    { name: 'Arun'},
    { name: 'Mohit'},
    { name: 'Koushik'},
    { name: 'KedaBro'},
    { name: 'Tripathi'},
    { name: 'Nlma'}, 
    { name: 'Nshma'},
    { name: 'BsCoder'}
  ];
  var people = $scope.people;
  var oldArray = [];

  var array = "Niraj,Shivam";
  array = array.split(",");
  for(var i=0 ; i < array.length ; i++){
    oldArray.push({
      name : array[i]
    });
  }
  
  var filteredArray = people.filter(function(itm){
    return array.indexOf(itm.name) < 0;
  });
  $scope.filteredArray = filteredArray;
  console.log(filteredArray); 

  $scope.multipleDemo = {};
  $scope.multipleDemo.selectedPeople = [];
  for(var  i = 0 ; i < array.length ; i++){
    $scope.multipleDemo.selectedPeople.push({name : array[i]});
  }

  $scope.submitValue = function(){
    console.log($scope.multipleDemo.selectedPeople);
  }
});

This is my html file

<!DOCTYPE html>
<html lang="en" ng-app="demo">
<head>
    <meta charset="utf-8">
    <title>AngularJS ui-select</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-sanitize.js"></script>

    <!-- ui-select files -->
    <script src="./select.js"></script>
    <link rel="stylesheet" href="./select.css">

    <script src="./demo.js"></script>

    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.css">    
</head>
<body class="ng-cloak" ng-controller="DemoCtrl as ctrl">
  <h1>Multiple Selection</h1>
  <ui-select multiple ng-model="multipleDemo.selectedPeople" theme="bootstrap" ng-disabled="ctrl.disabled" sortable="true" close-on-select="false" style="width: 500px;">
    <ui-select-match placeholder="Select person...">{{$item.name}}</ui-select-match>
      <ui-select-choices repeat="person in filteredArray">
        <div ng-bind-html="person.name"></div>
      </ui-select-choices>
  </ui-select>
  <button name="button" ng-click="submitValue()">Click me</button>
</body>
</html>

Link to Plnkr

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

Can an object in Node.js be 'required' to have access to the global scope of the importing files?

I've been researching extensively to find out if this is achievable. According to my findings so far, it seems that it may not be possible. Within my main.js file, I have the following code snippet: var commands = require('./commands.js'); ...

Sorting method in Ext JS 6.2.0 using mode

Seeking clarification on the sort([field],[direction],[mode]) method in Ext JS 6.2.0. Can someone explain the distinction between append, prepend, replace, and multi as mentioned in the documentation available at this link? I am unable to find a clear expl ...

The json encoding process results in a non-JSON entity

Utilizing $.ajax to validate data in my controller has been successful, except when there are processes preceding the variables that are placed into an array for json encoding. Despite the controller echoing the data, the view fails to render it as json en ...

How to effectively leverage useMediaQuery in material ui?

Upon completing the web application, I have made the decision to ensure it is mobile-friendly. Utilizing the material UI framework with react, I delved into the documentation but found myself uncertain about how to effectively implement it. Let me provide ...

Protractor: Unable to reach variables within closure set in the parent function

I have a code snippet in one of my JS files that looks like this: // test/lib/UserHelper.js 'use strict'; var Firebase = require('firebase'); exports.createUser = function (email, password) { browser.executeAsyncScript(function (do ...

When the checkbox is not selected, the content on the page will revert back to its original state

Is there a way to dynamically change content on a page when a checkbox is checked, and revert it back when the checkbox is unchecked? I don't want to manually set each element's value to default in JavaScript and CSS. <div class="switch&q ...

Tips on avoiding asynchronous issues in NodeJS: Ensure task a is finished before initiating task b

I have a situation where I need to ensure that task B code only executes after task A has completed. Task A involves converting an audio file, while Task B relies on the converted audio for further processing. The issue arises because Task A saves the n ...

Understanding the significance of the argument in the context of `express.json({ extended: false})`

Currently, I am in the process of setting up an API using express and encountered this particular line of code: app.use(express.json( { extended: false } )); Although I have referred to the documentation provided by express, I was unable to locate this sp ...

Add a new sibling item to Angular-UI-tree

I am trying to add a sibling item to a tree when clicked in angular-ui-tree (https://github.com/angular-ui-tree/angular-ui-tree) Here is an example of what I currently have: <item><input value"item A #1"><button>insert under</button& ...

Discovering ways to verify if an array is empty within JSON data using JMESPath?

I am presenting JSON data that looks like this: [ { "id": "i_1", "name": "abc", "address": [ { "city": [ "city1", "city2" ] }, { "city": [ "city1", "city2" ...

What is the best way to determine the length of an array using input from an HTML form?

Currently, I am in the process of developing a PHP file and one feature that I would like to implement is creating an array as illustrated in Example 1. As far as my understanding goes, an Array functions like a list where items can be added as shown in Ex ...

When accessing a class 2D array in C++, it is transformed into arr[] when called by a function

I am struggling with passing the array correctly for a university assignment. My goal is to prevent the array from being passed as a single array instead of a 2D array. The task involves creating a random maze generator that we can also interact with. Our ...

In Python, we have an array containing both strings and numbers. Our goal is to calculate the sum of all numerical values within each row and then add this

Hi there! I recently embarked on a journey to learn Python, starting just yesterday as my summer project. I've managed to store a CSV file as an array. Although the actual data is larger, I can work with this example and extrapolate the solution for ...

form submission issue with return false not working

Despite my efforts, the form still redirects to the page. I've been awake since 1AM trying to troubleshoot this issue! Please assist! function del_entry() { $('.delete_deal').submit(function() { var $g = $(this); ...

Create an interactive webpage that automatically generates new HTML elements after retrieving JSON data from a Web API during page load

I am currently in the process of developing a hybrid Android App using Phonegap/Apache Cordova. The main function of my app is to retrieve data from my web API, which is being served through JSON. I have implemented the following code snippet for this task ...

What is the process for retrieving paginated data from the store or fetching new data from an API service within an Angular 2 application using ngrx-effects?

After coming across this insightful question and answer about the structure of paginated data in a redux store, I found myself pondering how to implement similar principles using ngrx/store in an angular 2 application. { entities: { users: { 1 ...

JSON structure containing both lists and dictionaries within each other

I am struggling with the extraction of a nested JSON data into a dataframe that follows this specific structure: [ {'id': '1','sort': 'kg', 'name': 'meal','detail': [ {'s ...

Combining Two Collections in Mongoose using Node.js

As a beginner in MEAN Stack, I am currently working on a Node Js application. I have created two collections: var personSchema = Schema({ _id: Number, name: String, age: Number, stories: { type: Array, ref: 'Story' } }); va ...

Combining Laravel and VueJs for a Multi-Page Application (MPA)

I have recently developed a website using a combination of Laravel and VueJs. However, I am facing some confusion regarding the most suitable routing architecture to implement. Currently, my application has the following setup: The routing system is man ...

Modifying the Color of Individual Items within the Pagination Component in MUI

I am attempting to modify the background color of every item in my Pagination component by utilizing makeStyles. import { Pagination } from "@material-ui/lab"; import { makeStyles } from "@material-ui/core"; const pagination = makeStyl ...