Tips on implementing two ng-repeat directives within a specific element

Inside a JSON file, there is an array that needs to be iterated within <td> tags. The functionality entails creating a table based on user input, which includes the number of rows, input columns, and output columns provided by the user. Three arrays - $rootScope.input_columns, $rootScope.output_columns, and $rootScope.rows - store the data needed to construct the table. However, the array within input_columns containing necessary information fails to populate row cells as intended.

Controller:

var app = angular.module('rulesApp');

app.controller('myController2', ['$scope', '$rootScope',function($scope, $rootScope){
var inputcol=[];
            $rootScope.input_col=$scope.no_of_input;
            $rootScope.output_col=$scope.no_of_output;
            $rootScope.rows=$scope.no_of_row;
            for(var i=0;i<$rootScope.input_col;i++){
                inputcol.push({
                    id: inputcol.length,
                    dropped: false,
                    dataType:'',
                    name:'',
                    type:'input',
                    path:'',
                    rowCellValue:[],
                    rowCellValueOutput:[]
                });
            }$rootScope.input_columns=inputcol;//here i get input_columns json, Similarly json are made for output_columns and rows
            $scope.statementSelected = function(branch, selected_branches) {
        if(branch.selected) {
            for(var i = 0; i < $rootScope.input_columns.length; i++) {  
//Here i add an array inside input_columns.rowCellValue     $rootScope.input_columns[i].rowCellValue.push($rootScope.rows[i].rowCellValue);
                    }
})

Sharing the structure of input_columns

https://i.sstatic.net/Ad36L.png

The HTML code:

<tbody>
    <tr ng-repeat="row in rows"><!--It iterates on row json -->
        <td><input type="checkbox"></td>
        <!--Here i want that in row cells input_columns.rowCellValue data gets populate on row cell for input column --<
        <td ng-repeat="col in input_columns.rowCellValue">{{(col == "") && "&lt;enter data&gt;" || (col.split("/")[3])}}</td>
        <!--Here i want that in row cells output_columns.rowCellValueOutput data gets populate on row cell for output column -->
        <td ng-repeat="col in output_columns.rowCellValueOutput" ng-click="openDialog($event)">{{(col == "") && "&lt;enter data&gt;" || (col.split("/")[3])}}</td>              
    </tr>
</tbody>

Desired table structure: https://i.sstatic.net/gfV7S.png

Current issue results in blank rows: https://i.sstatic.net/y1Uul.png

Seeking assistance in accessing input_columns.rowCellValue values. Could two ng-repeat loops solve this issue? One for input_column value and another for rowCellValue?

An attempted solution:

Modified HTML code snippet:

<tbody>
        <tr ng-repeat="row in rows"><!--It iterates on row json -->
            <td><input type="checkbox"></td>
            <!--Here i want that in row cells input_columns.rowCellValue data gets populate on row cell for input column -->
<!--here with $index I am getting first value of rowCellValue in entire column can I iterate it using rows length -->
            <td ng-repeat="col in input_columns">{{(col.rowCellValue[$index] == "") && "&lt;enter data&gt;" || (col.rowCellValue[$index].split("/")[3])}}</td>
            <!--Here i want that in row cells output_columns.rowCellValueOutput data gets populate on row cell for output column -->
            <td ng-repeat="col in output_columns" ng-click="openDialog($event)">{{(col.rowCellValueOuput[$index] == "") && "&lt;enter data&gt;" || (col.rowCellValueOutput[$index].split("/")[3])}}</td>    

        </tr>
    </tbody>

Actual table result for input_column: https://i.sstatic.net/0m9R6.png

Current outcome: https://i.sstatic.net/hWrq4.png

Seeking guidance on iterating col.rowCellValueOutput[$index] with the row length instead of $index, for each column. Any suggestions?

Answer №1

Have you considered organizing this data into a JSON object and then iterating through it? It seems like your current approach is causing the inputCol and outputCol to output all values for every row.

//pseudo code
ng-repeat row in rows
    ng-repeat input in inputs
    ng-repeat output in outputs

As it stands, each time you iterate through rows, all the data within inputs and outputs will be repeated, resulting in identical rows.

If you were to structure your JSON object as follows:

ng-repeat row in rows
    ng-repeat input in row.input
    ng-repeat output in row.output

Your JSON would resemble something like this:

var rows = [{ input: [ "row1", "col2" ], output: [ "col3", "col4" ] },
            { input: [ "row2", "col2" ], output: [ "col3", "col4" ] }];

Answer №2

After much troubleshooting, I managed to find a workaround for my issue:

<td ng-repeat="col in input_columns" title="" id="{{col}}">{{(col.rowCellValue[$parent.$index] == "") && "&lt;enter data&gt;" || (col.rowCellValue[$parent.$index].split("/")[3])}}</td>

In the JSON controller structure, the data looked something like this:

input_column({
rowCellValue[
0:dic2317/fdgdfg3535/accViolation
1:dic2317/sdg3436dg/driver
})

To iterate through the rows index, I utilized $parent.$index.

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

Infinite Scrolling in React: Troubleshooting Issues with the dataLength Parameter

A function called newsFeed is utilized within the useEffect hook to make a request to an endpoint for fetching posts made by the logged in user and users they follow. The retrieved data is then saved to the state: const [posts, setPosts] = useState([]) con ...

Converting image bytes to base64 in React Native: A step-by-step guide

When requesting the product image from the backend, I want to show it to the user. The issue is: the API response contains a PNG image if the product has an image, but returns a (204 NO Content) if the product does not have an image. So, I need to display ...

Combining data from two JSON files following a specific format

I am working with two JSON files structured as follows: File 1 [ { "line": 2, "elements": [ {} ] } ] File 2 [ { "line": 3, "elements": [ {} ] } ] My goal is to combine these files into one output file that l ...

Using ng-init to pass a JSON object

I'm attempting to pass a JSON Object to my application using ng-init and the stringify method, but I am encountering an error. Instead of working as expected, I am getting a Lexer error. Lexer Error: Unexpected next character at columns 8-8 [#] in ex ...

The TypeScript compiler is searching in an external directory for the node_modules folder

My angular 2 project is located in the directory /c/users/batcave/first_project. In that same directory, I have various files such as index.html, systemjs.config.js etc., and a node_modules folder that only contains @types and typescript. This means my @a ...

Determine the size of an SVG while taking into consideration any strokes applied

Is there a way to seamlessly position an SVG in the corner of a div, despite having a dynamically generated stroke? Calculating the distance to the outermost part of the border proves difficult when dealing with irregular shapes like stars. Here's my ...

Issue with nivo-lightbox not opening upon clicking image

I have diligently followed the instructions to set up this JavaScript plugin, but unfortunately it doesn't seem to be functioning properly. The plugin I'm using can be found here: All the links to the CSS, theme, and JavaScript files are display ...

Implementing Vuejs sorting feature post rendering

Currently, I have elements linked to @click event listeners. <th @click="sort('dateadded')" class="created_at">Date Added I am looking for a way to automatically trigger this sorting function when the Vue.js component renders, so that th ...

The code encountered an error with message TS2345 stating that the argument type '(a: Test, b: Test) => boolean | 1' cannot be assigned to a parameter type of '(a: Test, b: Test) => number'

Apologies for the lengthy subject, but I am having trouble understanding the response. Here is my code snippet: this.rezerwacjeFilteredByseaarchInput.sort(function (a, b) { if (a[5]===null) { // console.log(a[5]); return 1; } ...

Error: The React component throws a TypeError because it is unable to read the property 'map' from an undefined source

I encountered the following error TypeError: Cannot read property 'map' of undefined at ListItemFactory.ts:84:57 at The specific line where the error occurs is: return announcementitems=json.value.map((v,i)=>( To provide mor ...

How to Incorporate Routes as Subroutes in Express.js

I am currently working on constructing a modular express.js server with a well-organized structure. I have experimented with using routes similar to components in React. index.js var syncsingle = require('./XYZ/syncsingle'); app.get('/sync ...

Making an HTTP request using Kotlin in an Android application

I attempted to retrieve JSON data or something similar by using URL.readText() from java.net.url in Android Studio, but my application crashes. fun ButtonClick(view:View) { textView.text = URL("https://www.google.com/robots.txt").readText() } ...

When the key code is equal to "enter" (13), the form will be submitted

When I press the Enter key, I want to submit a form if there are no error messages present. Here is the function I have created: $(targetFormID).submit(function (e) { var errorMessage = getErrorMessage(targetDiv); if (e.keyCode == 13 && errorMessa ...

What is the process for obtaining authorization to access user information?

I am facing a perplexing issue with my app settings. Despite setting everything up correctly, I am unable to authenticate my account on my website using the Javascript SDK. Whenever I try to console.log(me), I only receive public information. Upon furthe ...

JavaScript's "for of" loop is not supported in IE 11, causing it to fail

Here is a piece of code I'm using to select and remove a d3.js node: if (d.children) { for (var child of d.children) { if (child == node) { d.children = _.without(d.children, child); update(root); ...

What is the best way to animate CSS elements using jQuery to slide in from the right, left, or bottom?

When the page is scrolled on my website, I want table_area-left to move from left to right to its current position, table_area-right to move from right to left to its current position, and morph button to move from down to up. I am struggling to find the j ...

Tips on transitioning between two tables

Recently, I created an HTML page entirely in French. Now, I am attempting to incorporate a language translation feature on the website that allows users to switch between French and English (represented by two flag icons). My concept involves using a tabl ...

If the button is clicked in jQuery, then perform a specific action; otherwise, execute

hello, I am encountering difficulties in getting this feature to function properly. I am utilizing cropbox to allow users to upload their logos. The issue arises when users do not click on the crop button again; it results in overwriting the existing file ...

When I type text into the form field, JavaScript causes a disruption in the form

I'm attempting to implement a dual-stage user onboarding procedure. Initially, users will input the industry they belong to and then provide a description. Upon clicking submit, my intention is to conceal that portion of the form and reveal the "Creat ...

Is the auto-import feature in the new VSCODE 1.18 compatible with nodelibs installed via npm?

Testing out the latest auto-import feature using a JS file in a basic project. After npm installing mongoose and saving an empty JS file for editing, I anticipate that typing const Schema = mongoose. will trigger an intellisense menu with mongoose nodelib ...