Setting dropdown options dynamically within an ng-repeat loop

I am currently encountering an issue with my Angular code, particularly when populating the dropdown within an HTML table (Code provided below). Could someone kindly assist me in determining what actions need to be taken inside modify() in order to populate the dropdown?

HTML Code:

<table class="table" ng-app="empApp" ng-controller="employeeController">
<thead>
    <tr class="info">
        <th>Emp Name</th>
        <th>Status</th>
        <th colspan="2">&nbsp;</th>
    </tr>
</thead>
<tbody>
<tr ng-repeat="emp in employees">
  <td>
    <div ng-hide="editingData[emp.id]">{{ emp.name }}</div>
    <div ng-show="editingData[emp.id]"><input type="text" ng-model="emp.name" /></div>
    </div>
  </td>
  <td>
    <div ng-hide="editingData[emp.id]">{{ emp.status.name }}</div>
    <select ng-show="editingData[emp.id]" class="form-control" ng-model="emp.status" 
    ng-options="status.id as status.name for status in statuses"></select>
  </td>
  <td colspan="2">
    <div class="btn-group">
        <button type="submit" class="btn btn-primary" ng-hide="editingData[employee.id]" ng-click="modify(emp)">Modify</button>&nbsp;
        <button type="submit" class="btn btn-primary" ng-show="editingData[employee.id]" ng-click="update(emp)">Update</button>&nbsp;
    </div>
 </td>
</tr>
</tbody>
</table>

Javascript Code:

    var empApp = angular.module("empApp", []);
    empApp.controller('employeeController', function($scope, $http) {

    $scope.statuses = [{"id":1,"name":"Active"}, {"id":1,"name":"Inactive"}];

    $scope.employees = [{"id":1,"name":"Mark","status":{"id":1,"name":"Active"}},{"id":2,"name":"Sara","status":{"id":2,"name":"Inactive"}}]; 
    $scope.editingData = {};

    for (var i = 0, length = $scope.employees.length; i < length; i++) {
        $scope.editingData[$scope.employees[i].id] = false;
    }

    $scope.modify = function(employee){
        $scope.editingData[employee.id] = true;
        //In this section, ensure you set the Employee Status correctly to enable dropdown population

    };
  });

I am having difficulty in successfully populating the dropdown with the current value, as illustrated in the diagram below: https://i.sstatic.net/VseyM.png

Answer №1

Feel free to take a look at this plunker and see if it meets your requirements.

1: Both statuses have the same id of 1. Please update them accordingly:

From:

$scope.statuses = [{"id":1,"name":"Active"}, {"id":1,"name":"Inactive"}];

To:

$scope.statuses = [{"id":1,"name":"Active"}, {"id":2,"name":"Inactive"}];

2: Modify your select ng-model to emp.status.id.

From:

<select ng-show="editingData[emp.id]" class="form-control" ng-model="emp.status" 
    ng-options="status.id  as status.name for status in statuses"></select>

To:

<select ng-show="editingData[emp.id]" class="form-control" ng-model="emp.status.id" 
    ng-options="status.id  as status.name for status in statuses"></select>

3: Revise your buttons' ng-hide/ng-show attributes.

From:

<div class="btn-group">
    <button type="submit" class="btn btn-primary" ng-hide="editingData[employee.id]" ng-click="modify(emp)">Modify</button>&nbsp;
    <button type="submit" class="btn btn-primary" ng-show="editingData[employee.id]" ng-click="update(emp)">Update</button>&nbsp;
</div>

To:

<div class="btn-group">
    <button type="submit" class="btn btn-primary" ng-hide="editingData[emp.id]" ng-click="modify(emp)">Modify</button>&nbsp;
    <button type="submit" class="btn btn-primary" ng-show="editingData[emp.id]" ng-click="update(emp)">Update</button>&nbsp;
</div>

Update: According to @Rajesh's suggestion, consider storing the status_id instead of the entire status object.
Take a look at his fiddle.

Answer №2

Simply swap out your select tag section with this code snippet

<select ng-show="editingData[emp.id]" class="form-control" ng-model="emp.status" 
    ng-options="status as status.name for status in statuses track by status.id"></select>

The reason behind this is that you are passing the entire object in ng-model, and it's necessary to track the full object by ID in order to populate the dropdown list and have the correct value selected

Additionally, in your buttons:

<div class="btn-group">
        <button type="submit" class="btn btn-primary" ng-hide="editingData[emp.id]" ng-click="modify(emp)">Modify</button>&nbsp;
        <button type="submit" class="btn btn-primary" ng-show="editingData[emp.id]" ng-click="update(emp)">Update</button>&nbsp;
    </div>

This is because you are using 'emp' instead of 'employee' within ng-repeat since it will be dynamic

Answer №3

As you iterate through

<tr ng-repeat="emp in employees">

You are currently toggling based on:

editingData[employee.id]

Consider updating this to

editingData[emp.id]

Answer №4

Be sure to incorporate emp.id into both your button and modify function, as well as looping through the data. Utilize $scope.employees[i].isVisible = false along with ng-hide = $scope.employees.isVisible and ng-show = $scope.employees.isVisible

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

Issue with scrolling feature on chrome browser

I've noticed that this piece of code functions correctly in Firefox, but unfortunately doesn't seem to be working in Chrome. In Chrome, the scrolling feature is not functioning as expected - the click event fires, but it fails to move to the posi ...

Using JS to switch between text and state in ToneJS

I am facing an issue with the text not displaying in the "play-stop-toggle" element, despite my limited experience with JS. My desired outcome includes: [SOLVED] The text in <div id="play-stop-toggle" onclick="togglePlay()">Play ...

Solving the CSS Trick: Responsive Data Table (featuring inline editing) Display Glitch

In my quest to create a responsive table with inline editing, I turned to Chris's guide at CSS-Tricks (check it out here). To see how it all comes together, take a look at this Plunker demo. On mobile screens, the responsiveness is on point. https: ...

How can I determine in JQUERY when all ajax requests on a page have finished processing?

Is there a way in JQUERY to determine when all ajax calls on a page have finished? I need to run a specific method once all the ajax calls are done. ...

Expecting a volumetric result can be deceiving when dealing with objects that have three flat

The problem at hand: When subtracting a cube from a sphere, an interesting result occurs where the z axis maintains its volume while the y and x axes create flat disks. This peculiar outcome is puzzling to me as I utilize the typical subtraction method wi ...

Storing intricate information in MongoDB

I've been exploring the JSON-BSON structures of MongoDB, but I'm struggling to understand how to insert documents into other documents and query them. Here's what I'm curious about - let's say someone wants to store an array of do ...

The button is functioning properly on desktops using bootstrap, but is unresponsive on mobile devices

I have successfully created a bootstrap button with an embedded link. Here is how it appears: Upon hovering over the button: This is the code snippet for the button: <div class="s-8"><button type="button" onClick="javascript:location.href = &ap ...

Schedule - the information list is not visible on the calendar

I am trying to create a timeline that loads all data and events from a datasource. I have been using a dev extreme component for this purpose, but unfortunately, the events are not displaying on the calendar. Can anyone offer any insights into what I might ...

Creating dynamic data fields in a Vue instance for form validation in Vue2

As a newcomer to Vue.js, I'm tackling the challenge of implementing form validation on a dynamically generated form. The input fields are populated based on the JSON object retrieved through an AJAX request. While exploring various form validation lib ...

Transmitting POST form information from an AngularJS client to an Express/Node.js server

I am currently facing an issue where the data from my AngularJS form is not reaching the Express server, despite the client function executing successfully. I suspect there might be a problem with the URLs being used. Snippet from my AngularJS controller: ...

The print screen button in Internet Explorer is not functioning the same way as in other browsers when using JavaScript

Recently, I implemented a JavaScript code that restricts Normal users (non-professionals) from using print screen, Ctrl+A, and Ctrl+C functionalities on the browser. While this code works seamlessly on Firefox and Chrome, it seems to have intermittent su ...

Is there a way to determine whether a mouse-down event took place on the scroll-bar or elsewhere within the element?

Looking for a solution with my HTML div acting as a canvas containing various objects. The issue lies in the fact that when attempting to draw a selection rectangle by dragging with the mouse, if scroll bars appear due to the large size of the canvas, scr ...

Swapping between running programs with JavaScript

Challenge: I am exploring the possibility of establishing a communication loop between a server-side program and client-side JavaScript. The idea is that all outputs from the program should be transmitted to JavaScript for user display, while all user inp ...

Angularjs: Adding Negative and Positive Numbers

How can I extract negative numbers and positive numbers separately from JSON data obtained from the server using the $http.get method? One of the fields in the data is called Credits, which contains both negative and positive values. Can anyone help me wit ...

How can I reset the default sorting in ui-grid after clearing other sorting options?

Currently utilizing UI-Grid 3.0 (). I want to make it clear to the user that there is a default sort applied to the data, but hide it while they are sorting by another column. When they remove their custom sort, I want the default sort to resume. However, ...

Is it possible to target elements based on a specific CSS3 property they use?

Is there a method to target all elements in the DOM with a border-radius other than 0? If anyone has suggestions, it would be greatly appreciated! ...

Can you send JSON data and redirect simultaneously using Express?

In my application, there is a registration feature that involves sending a confirmation email to the user. I am looking to achieve a similar outcome as shown below: return res.status(200).redirect('/').json({ message: 'Successfully confir ...

The session will stay active even after the skill has finished performing a task by setting the parameter "shouldEndSession

I'm encountering an issue regarding my Alexa skill certification. Although I have passed all the requirements, I received feedback that includes the following points: Upon completing a task, the session remains open without any prompt to the user. It ...

The Angular 2 application functions perfectly when running locally, but encounters issues when running on an ec2 instance

When trying to upload an Angular 2 application to an AWS EC2 t2.small instance, it is not working as expected, even though it runs successfully in a local server. Node version: v7.0.0 NPM version: 3.10.8 There has been an EXCEPTION: Uncaught (in prom ...

Cut and paste functionality for ag grid the cutting action

I am looking to enable copy, cut, and paste actions in ag grid. While ag grid enterprise features include everything I need, the cut action is missing. I believe it can be implemented by adding a keydown event handler to the component. The process would ...