Develop a table with dynamic features in Angular according to the number of rows selected from a dropdown menu

I am currently exploring AngularJS 1.6 and tackling the challenge of dynamically populating a table with rows based on the number selected in a dropdown list, ranging from 1 to 12. Here's the code I have up until now:

 <body ng-controller="myController">
    <div>
        <p>Blue Corner Boxer: </p> <input type="text" ng-model="nameBlue">
        <br>
        <p>Red Corner Boxer: </p> <input type="text" ng-model="nameRed">
        <br>
        <p>Number of Rounds:</p> <select ng-model="selectedRounds"><option ng-repeat="x in rounds">{{x}}</option></select>
    </div>

    <div>
            <table class="table table-striped">
                    <thead>
                        <tr>
                            <th style="text-align:center">{{nameBlue}}</th>
                            <th style="text-align:center">Round</th>
                            <th style="text-align:center">{{nameRed}}</th>
                        </tr>
                    </thead>
                    <tbody class="tablerows">
                       <tr ng-repeat="x in selectedRounds">
                           <td>Test</td>
                           <td>Test</td>
                           <td>Test</td>
                       </tr>
                    </tbody>
                </table>

                <h2 style="text-align: center">Final Score: </h2> {{scoreBlue1 + ScoreBlue2}}
    </div>
</body>

In the JavaScript file:

//Creating the module
var myApp = angular.module("myModule", []);



//Defining the controller and initializing the module simultaneously
myApp.controller("myController", function ($scope) {
    $scope.message = "AngularJS tutorial";
    $scope.score = [1,2,3,4,5,6,7,8,9,10];
    $scope.rounds = [1,2,3,4,5,6,7,8,9,10,11,12];
});

Currently, adding anything between 1 and 9 selects one row in the table, while selecting 10 through 12 adds two rows. Therefore, I believe I need to figure out how to generate an array of length equal to "selectedrounds" for repeating rows with the repeater.

Thank you!

Answer №1

If you are in need of an array solely for the purpose of iteration without concerning yourself with the data within it, here is a simple way to achieve this:

Within your controller:

$scope.selectedRounds = 0;
$scope.getRoundsArray(){
   return new Array($scope.selectedRounds);
}

This creates an array with the specified length and all elements set to 'undefined'. For example, creating an array with a length of 3 will result in: ['undefined', 'undefined', 'undefined'].

In your view:

<tr ng-repeat="x in getRoundsArray() track by $index">

The "track by $index" is necessary because your array consists of only 'undefined' values. This ensures that Angular does not encounter issues with duplicate keys.

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

Using Ajax to insert data into WordPress

Looking to incorporate data into the WordPress database using Ajax integration. functions.php function addDataToDB(){ global $wpdb, $count; $count = 25; $wpdb->insert( 'custom_table', array( 'slid ...

Combining package.json commands for launching both an Express server and a Vue app

I recently developed an application using Vue.js and express.js. As of now, I find myself having to open two separate terminal windows in order to run npm run serve in one and npm start in the other. My ultimate goal is to streamline this process and have ...

Having trouble retrieving the API URL id from a different API data source

For a small React project I was working on, I encountered a scenario where I needed to utilize an ID from one API call in subsequent API calls. Although I had access to the data from the initial call, I struggled with incorporating it into the second call. ...

Challenges with cross domain iframes

I am trying to find a way to send a message from an iframe to the parent page at regular intervals, for example: Iframe Domain = www.abc.com Parent Domain = www.xyz.com I have looked into the following resources: Cross domain iframe issue If anyone ha ...

Adjusting the letter spacing of individual characters using HTML and CSS

Is there a way to set letter-spacing for each character with different sizes as the user types in a text box? I attempted to use letter-spacing in CSS, but it changed all characters to the same size. Is there a possible solution for this? Here is the code ...

A step-by-step guide on sending a fetch request to TinyURL

I have been attempting to send a post request using fetch to tinyURL in order to shorten a URL that is generated on my website. The following code shows how I am currently writing the script, however, it seems like it's not returning the shortened URL ...

Error: Attempting to access the 'url' property of an undefined variable, despite specifically checking for its undefined status

Within my React application, I am utilizing the following state: const [functions, setFunctions] = useState([{}]); I have created a test to check if a specific property is undefined: if (typeof functions[functionCount].url !== "undefined") { ...

The process of combining identical data values within an array of objects

Can anyone help me with merging arrays in JavaScript? Here is an example array: [{ "team": team1, "groupname": "group1", "emp-data": [{ "id": 1, "name": "name1", }], }, { "team": team1, "groupname": "group1", " ...

Struggling with implementing the use of XMLHttpRequest to transfer a blob data from MySQL to JavaScript

I have a blob stored in my local WAMP64/MySQL server that I need to retrieve and pass to an HTML file using XMLHttpRequest. I know I should set responseType="blob", but I'm not sure how to transfer the blob from PHP to JavaScript in my HTML file. Any ...

Discover the process of loading one controller from another controller in Angular JS

Is it possible to load an entire controller1 from a different controller2, not just a function? ...

Console log is not displaying the JSON output

I am currently working on implementing a notification button feature for inactive articles on my blog. I want to ensure that the admin does not have to reload the page to see any new submitted inactive articles. To achieve this, I am looking to use Ajax, a ...

Show a loading image after clicking on an image or button using JavaScript

I recently created an App using PhoneGap and JqueryMobile. Transitioning between multiple HTML pages by clicking on images seems to be causing slow loading times, leaving end users unaware of what's happening in the background. I am looking for a way ...

Tips for transforming a JSON response into an array with JavaScript

I received a JSON response from an API: [ { "obj_Id": 66, "obj_Nombre": "mnu_mantenimiento_de_unidades", "obj_Descripcion": "Menu de acceso a Mantenimiento de Unidades" }, { "obj_Id": 67, "ob ...

Utilizing nested grouping in mongoose schemas

I am facing a challenge while attempting to execute a nested group query in MongoDB. Is there a way to group data by both date and campaign ID, ensuring that each campaign ID contains a nested array of creatives with their respective data (views and clicks ...

Are the sorting algorithms for BackboneJS and AngularJS considered stable sorting methods?

I'm considering incorporating BackboneJS and AngularJS into my app. However, I'm curious about the stability of sorting algorithms in these frameworks. Specifically, I would like to know if they maintain the order of previously sorted columns wit ...

retrieve the month and year data from the input date

I encountered a situation where I'm working with the following unique HTML code: <input type="date" id="myDate"/> <button type="button" class="btn btn-secondary" id="clickMe">MyButton</ ...

Setting up authorization levels for roles in Discord.js

Hi everyone, I came across this script that deals with users posting invite links. How can I whitelist specific channels to prevent the bot from banning or kicking users for posting invite links? Any help would be greatly appreciated. Thank you. adminCli ...

Steps for extracting URL parameters from AWS API Gateway and passing them to a lambda function

After successfully setting up my API gateway and connecting it to my lambda function, I specified the URL as {id} with the intention of passing this parameter into the lambda. Despite numerous attempts using both the default template and a custom one for ...

Optimizing Performance by Managing Data Loading in JavaScript Frameworks

I am new to JavaScript frameworks like React and Vue, and I have a question about their performance. Do websites built with React or Vue load all data at once? For example, if I have a page with many pictures, are they loaded when the component is used or ...

Struggling to properly implement an "Errors" Object in the state function of a React Login Form Component

The issue arose while I was following a React tutorial. My objective is to develop a basic social media web application using Firebase, React, MaterialUI, and more. I am currently at around the 5:40:00 mark and have successfully resolved all previous pro ...