0

Using AngularJS 1.3.4.

I have a html table that is being populated from an web api where I make an http request to get that data and populate my html table. My example json is as below:

{
    id: 1,
    firstName: "John",
    lastName: "Rein",
    status: 'available'
},
{
    id: 2,
    firstName: "David",
    lastName: "Gumry",
     status: 'not available'
}

Now I have a dropdown below the table, and I am using ui-select for it. I want to populate this dropdown based on the status in my json. For example in my json above I have 2 status available and not available. I want my dropdown to have these values. After I populate my dropdown, I want to filter my table based on the dropdown value that is selected. I have my dropdown as:

  <ui-select tagging ng-model="selected" theme="bootstrap">
    <ui-select-match placeholder="Pick one...">{{$select.selected.value}}</ui-select-match>
    <ui-select-choices repeat="val in values | filter: $select.search track by val.value">
        <div ng-bind="val.value | highlight: $select.search"></div>
    </ui-select-choices>        
</ui-select>

I have created my jsfiddle at: https://jsfiddle.net/aman1981/wfL1374x/

I am not sure how can I bind the results from json to my DDL and filter my table.

2
  • The link to your jsfiddle is broken Commented Jun 18, 2018 at 18:18
  • @jbrown I have fixed that. Commented Jun 18, 2018 at 18:45

1 Answer 1

1

You had a few issues that needed handling including redundant use of ng-app and ng-controller. Also, it seems as though ui-select works best using the ControllerAs syntax which is a preferred approach in general.

After these changes and others (too many to list), here's the working code:

angular.module('myApp', ['ui.select'])

  .controller("PeopleCtrl", function($http) {

    var vm = this;

    vm.people = [];
    vm.isLoaded = false;
    vm.values = [];

    vm.loadPeople = function() {
      // *** I had to comment this out as it is not allowed in the SO Code Snippet but is fine for your code
      //$http({
      //  method: 'POST',
      //  url: '/echo/json/',
      //  data: mockDataForThisTest

      //}).then(function(response, status) {
      //  console.log(response.data);
      //  vm.people = response.data;
      //});
      vm.people = [{
          id: 1,
          firstName: "John",
          lastName: "Rein",
          status: 'available'
        },
        {
          id: 2,
          firstName: "David",
          lastName: "Gumry",
          status: 'not available'
        }
      ];

      vm.values = [...new Set(vm.people.map(obj => ({
        value: obj.status
      })))];
    };

    vm.selected = {
      key: null,
      value: null
    };

    var mockDataForThisTest = "json=" + encodeURI(JSON.stringify([{
        id: 1,
        firstName: "John",
        lastName: "Rein",
        status: 'available'
      },
      {
        id: 2,
        firstName: "David",
        lastName: "Gumry",
        status: 'not available'
      }
    ]));
  })
<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

  <link href="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-select/0.20.0/select.min.css" rel="stylesheet" />

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-sanitize/1.2.23/angular-sanitize.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-select/0.20.0/select.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body ng-app="myApp">
  <div ng-controller="PeopleCtrl as ctrl">
    <br>
    <p> Click <a ng-click="ctrl.loadPeople()">here</a> to load data.</p>

    <h2>Data</h2>
    <div class="row-fluid">
      <table class="table table-hover table-striped table-condensed">
        <thead>
          <tr>
            <th>Id</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Status</th>
          </tr>
        </thead>
        <tbody>
          <tr ng-repeat="person in ctrl.people | filter: {status: ctrl.selected.value} : true">
            <td>{{person.id}}</td>
            <td>{{person.firstName}}</td>
            <td>{{person.lastName}}</td>
            <td>{{person.status}}</td>
          </tr>
        </tbody>
      </table>
    </div>
    <br><br>

    <div width="50px">
      <ui-select tagging ng-model="ctrl.selected" theme="bootstrap">
        <ui-select-match placeholder="Pick one...">{{$select.selected.value}}</ui-select-match>
        <ui-select-choices repeat="val in ctrl.values | filter: $select.search track by val.value">
          <div ng-bind="val.value | highlight: $select.search"></div>
        </ui-select-choices>
      </ui-select>
    </div>
  </div>
</body>

Sign up to request clarification or add additional context in comments.

8 Comments

thank for this. I had mentioned that the dropdown needs to be populated from the json itself, not the hardcoded values. I had created with hardcoded values as I dont know how to populate that from json. Sorry if I was not clear.
I’m not sure what more you are looking for. You have an $http call to get the data. What else are you needing?
The ui-select is bind to repeat="val in ctrl.values" where values are hardcoded array in the controller. Since My json has "Status" as on of the key. I want my dropdown to bind to status from the Json not the hard coded Values array. Hope its clear now
is this doable?
Are you asking if the values array can be initialized with data from the peoples array?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.