Getting Data from Selected Radio Buttons in AngularJS Using ng-repeat

I am looking to retrieve the data (id, name) of a selected row using a radio button. Here is what I have so far:

<tr ng-repeat="list in listTypes">
    <td>{{list.TaskId}}</td>
    <td>{{list.Comments}}</td>
    <td>{{list.RecordDate}}</td>
    <td>{{list.StartDate}}</td>
    <td>{{list.DueDate}}</td>
    <td>{{list.AssignTo}}</td>
    <td>{{list.AssignBy}}</td>
    <td><label class="switch">
        <input type="radio" name="switch-radio1" checked="" value="0" ng-value="true" >
        <span></span>
    </label></td>
</tr>

I need to extract the data taskId and comments from the selected row when the radio button is selected. What JavaScript function should I use for this? Thank you for any assistance.

Answer №1

If you want to utilize ng-model with your checkbox, you can do so by implementing the following:

<tr data-ng-repeat=" item in itemList">
    <td>{{item.ID}}</td>
    <td>{{item.Description}}</td>
    <td>{{item.DateCreated}}</td>
    <td>{{item.StartDate}}</td>
    <td>{{item.EndDate}}</td>
    <td>{{item.AssignTo}}</td>
    <td>{{item.AssignBy}}</td>
    <td>
         <label class="switch">
             <input type="checkbox" name="item-checkbox" ng-model="item.selected" ng-change="onItemCheck(item)">
         </label>
    </td>
</tr>

Your controller function will then handle the selected items as shown below:

$scope.onItemCheck = function(selectedItem) {
    // Access the selected item here
    console.log(selectedItem.selected);    // Will be true if selected, false otherwise
};

Answer №2

To begin, make sure to assign an ng-model to your radio button and update the ng-value attribute to reference the list's TaskId.

<input type="radio" name="switch-radio1" ng-model="selectedItem" checked="" value="0" ng-value="list.TaskId" >

Once you have access to the list's TaskId, utilize Array.prototype.filter to search for the corresponding data within listTypes.

Answer №3

I have discovered a more efficient method than others. When utilizing "ng-model," there is no need for the name attribute, and by using "$parent," I believe we can access a single model to fulfill the role of the name attribute as well as bind data. This is due to the fact that each item has its own scope within ngRepeat, requiring a higher level approach to reach the defined single model.

The ngRepeat directive creates a template for each item in a collection. Each template instance has its own scope, where the loop variable represents the current collection item, and $index refers to the item's index or key. https://code.angularjs.org/1.7.5/docs/api/ng/directive/ngRepeat

$scope Properties : $parent indicates the parent scope. https://code.angularjs.org/1.7.5/docs/api/ng/type/$rootScope.Scope#$parent

In the HTML template:

<table>
    <tr data-ng-repeat="item in listTypes">
        <td>{{list.TaskId}}</td>
        <td>{{list.Comments}}</td>
        <td>{{list.RecordDate}}</td>
        <td>{{list.StartDate}}</td>
        <td>{{list.DueDate}}</td>
        <td>{{list.AssignTo}}</td>
        <td>{{list.AssignBy}}</td>
        <td>
             <label class="switch">
                 <input type="radio" ng-model="$parent.rdoModel" ng-value="item" >
             </label>
        </td>
    </tr>
</table>
 <button type="button" ng-click="Ok(rdoModel)">OK</button>

In the AngularJS controller script :

$scope.rdoModel={};
$scope.Ok = function(item){
var data = item ; // accesses all data of the list item
};

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 you identify the reason for the hydration issue in my next.js project?

My ThreadCard.tsx component contains a LikeButton.tsx component, and the liked state of LikeButton.tsx should be unique for each logged-in user. I have successfully implemented the thread liking functionality in my app, but I encountered a hydration error, ...

Analyzing input from the user for string comparison

I am trying to create a code that activates when the user inputs a specific symbol or string. The issue is that it seems to be disregarding the if statements I have implemented. Here is the challenge at hand: Develop a program that can determine the colo ...

I am looking to transfer information in CodeIgniter from a view utilizing AJAX post, handle that information in the controller, and then return the resultant array back to the view

I have been searching the entire internet, but unfortunately, I couldn't find a helpful solution. Any assistance would be greatly appreciated. Thank you in advance. MY HTML <div class="row"> I am unsure whether the form tag is required in my ...

Issue with modal input causing directive template to not render

I am working on an angular-bootstrap modal where I created a custom directive to automatically focus on the input field when opened. However, after adding the directive template to my input tag, I couldn't see it when inspecting the element. Here is t ...

I need to retrieve the Instagram follower count for a specific user using JavaScript based on their user ID

I'm looking to design a form that allows users to input their Instagram ID and receive the exact number of followers without the "k" abbreviation. However, I am unsure how to achieve this. Any guidance on how to accomplish this would be greatly apprec ...

Error encountered: jquery form validation fails to register changes

I am currently developing a calculator-like code where users will submit a form multiple times, and I need to save the result of calculations only if there are changes in the form. For instance, when a user clicks the "Calculate" button for the first time, ...

json array: Tips for adding elements to a new array

In order to achieve my goal, I am looking to create a JSON array structure similar to the one shown below: var args = [{ name: 'test', value: 1 }, { key: 'test2', value: 2}]; How can I modify the code snippet provided below to generat ...

The function json.stringify fails to invoke the toJson method on every object nested within the main object

When using IE 11, I encountered an issue where calling Stringify on my objects did not recursively call toJson on all objects in the tree. I have implemented a custom toJson function. Object.prototype.toJSON = function() { if (!(this.constructor.name == ...

script locate the div ID within a given text and clear its content

My string contains some dynamic HTML with a div element having an id of "time", Here's an example: myString = "<div class="class">blahblah</div><div id="time">1:44</div>" How can I generate a new string that is identical to ...

TinyMCE - Utilizing selection.setContent along with getContent for the Warp Button

I am looking to implement a button that will wrap content with all tags. Snippet of Code: editor.addButton('MobileToggleArea', { text: '<M>', icon: false, onclick: function (){ editor.selection. ...

Mastering the Art of Parsing Complex JSON Data

I received a JSON output that looks like this. Using getjson, I'm trying to extract the datetime and value fields (italicized and bolded) such as [16:35:08,30.579 kbit/s],[16:35:38,23.345 kbit/s]. Is there any solution to achieve this? Thank you. { ...

JavaScript generated form fails to submit

I am currently facing an issue with submitting form data to a PHP file when it is generated using JavaScript. I am unsure of how to resolve this problem. The form submission works fine on a test .html file by itself, but not when it is generated by JavaScr ...

Activate typeahead-on-select in Angular UI Typeahead when ng-model changes

I have a specific directive set up with angular ui typeahead. The myModel is utilizing two-way binding. In the controller, I am planning to update the value of myModel and then trigger typeahead-on-select every time the myModel changes. <input type="te ...

Eliminating the table header in the absence of any rows

I have successfully implemented a Bootstrap table in my React application, where users can add or delete rows by clicking on specific buttons. However, I want to hide the table header when there are no rows present in the table. Can anyone guide me on how ...

Running a JavaScript script after loading a page via AJAX is not functioning as expected

As I am attempting to retrieve a page using AJAX, I've encountered an issue where any Javascript code included on that page fails to execute. Why is this happening? The simple JavaScript code present in my ajax page is as follows: <script type=" ...

The validation feature in ASP.NET MVC does not seem to be functioning properly while using

I'm struggling to make the bootstrap modal and asp.net mvc validation work together seamlessly. My form is quite complex with validation displayed in a bootstrap modal. However, when I press the submit button, the validation doesn't seem to be fu ...

Enhance your Next.js routing by appending to a slug/url using the <Link> component

In my Next.js project, I have organized my files in a folder-based structure like this: /dashboard/[appid]/users/[userid]/user_info.tsx When using href={"user_info"} with the built-in Next.js component on a user page, I expect the URL to dynamic ...

Peculiar redirection encountered while handling a form with checkbox option

When I try to submit the form in Chrome, the PHP file does not get called and I am redirected to another HTML page. However, in Firefox, when I select three checkboxes, it redirects me to that same HTML page as in Chrome. I even tried using radio buttons i ...

Retrieving a json file from a local server by utilizing angularjs $http.get functionality

After fetching a JSON file from localhost, I can see the data when I console log. However, when I inject the Factory into my controller, it returns a null object. This indicates that the variable errorMessage does not receive the JSON object because the ...

Menus that mimic the style of Google Translate's select options

I'm looking to design an HTML select menu using CSS in a similar fashion to Google Translate's style on Google Translate. I've searched for tutorials online, but they all involve jQuery. Is there a way to achieve something similar using only ...