The dynamic data for ng-repeat failed to load properly due to an

Hey guys, I've encountered a problem that I can't seem to figure out. It appears that Google Chrome rearranges JSON data when rendering it in a way that affects 'ng-repeat'. As a result, when I load the data into the grid, the columns get reversed.

'use strict';

angular.module('grid', [])
.run(templateRun)
.directive('grid', Grid);

function templateRun ($templateCache) {
    $templateCache.put('grid2.html', '<table><thead><tr><th ng-repeat="(key,value) in option.columns" ng-click="sort(value.predicate)"><strong>{{value.text}}</strong><th><tr></thead><tbody><tr ng-repeat="data in option.data"><td ng-repeat="field in data">{{field}}</td></tr></tbody></table>');
}

function Grid ($templateCache, $rootScope, $compile, $rootElement) {

    return{
        restrict: 'E',
        scope: {
            option: "=data"
        },
        template: $templateCache.get('grid2.html'),
        link: function (scope, element, attr) {

        }

    }

}


angular.module("app", ["grid"])
.controller("HomeCtrl", HomeCtrl);

function HomeCtrl ($scope) {

    $scope.people = [
        {
            name: "John",
            occupation: "Programmer",
            age: 5
        }, 
        {
            name: "Jill",
            occupation: "Analyst",
            age: 10
        }, 
        {
            name: "Jeff",
            occupation: "Sales",
            age: 2
        }, 
        {
            name: "Joan",
            occupation: "Designer",
            age: 50
        }
    ];

    $scope.option = {
        data: $scope.people,
        columns: [
            {
                text: "Name"
            },
            {
                text: "Occupation"
            },
            {
                text: "Age"
            }
        ]
    }

}


<!doctype html>
<html lang="en" ng-app="app">
<head>
    <meta charset="UTF-8">
    <title>Grid</title>
</head>
<body ng-controller="HomeCtrl">

    <grid data="option"></grid>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script type="text/javascript" src="grid.js"></script>
</body>
</html>

Error image: https://i.sstatic.net/RpVNe.png

Answer №1

There are multiple potential solutions to address this issue.

One approach involves creating a mapping between the column names and the corresponding JSON field names. This mapping can then be utilized to determine which field should go in each cell of the table.

    $scope.option = {
        data: $scope.people,
        columns: [
            {
                text: "Name",
                dataProp: "name"
            },
            {
                text: "Occupation",
                dataProp: "occupation"
            },
            {
                text: "Age",
                dataProp: "age"
            }
        ]
    }

The tbody section of the HTML template

<tbody>
  <tr ng-repeat="data in option.data">
    <td ng-repeat="col in option.columns"> {{data[col.dataProp]}}</td>
  </tr>
</tbody>

See a functional example here.

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

Checking for non-overlapping number ranges in an array in React/Javascript before submitting

I am faced with a challenge involving a list of values containing start and end address values. I need to ensure that when submitting these values, there are no existing ranges or overlaps within them. [ { "id": 23, "startAddress&quo ...

Quickest method for deriving a boolean value from multiple boolean inputs

Within a document, the keys isOccupied and vacant are being destructured. const { isOccupied, vacant } = doc || {}; boolDetermination = (isOccupied, vacant) => { if (isOccupied && isOccupied === vacant) { < --- Return isOccupied value ...

How can we universally detect and resolve the user's language within a React/Next.js application using an Apollo client HOC?

Currently, I am developing an I18n module utilizing the Next.js framework (v5). The challenge I am facing is determining the user's default language universally in order to display the UI in that language. While it is relatively simple to resolve th ...

Updating Rails record using Ajax with select_tag and onchange functionality

I'm working on an index view where I want to update a table data dynamically using select_tag onchange feature without the need to refresh the page. Do I need to use Coffeescript to detect the onchange event, capture the new value, and then send it to ...

Transform the entire division into a clickable link, excluding a specific subdivision that should have its own separate link

I need to create a product layout page where products will be displayed with an image, person's name, title, and description. The challenge is that all of these elements should have one common link except for the person's name that needs a separa ...

Issue encountered while attempting to utilize Semantic elements

I'm currently working on implementing a Transition component from Semantic UI React. I have the Semantic UI library installed and importing it as follows: import { Transition, } from "semantic-ui-react"; import 'semantic-ui-css/semantic.min ...

How come my product details page keeps automatically scrolling to the bottom every time I visit it?

I am facing an issue where clicking on a card to navigate to the product details page automatically takes me to the bottom of the next page without scrolling. Below is a snippet of my code: import React from "react"; import { Link } from "re ...

What is the best way to send an object key/value pair to a Vue template?

I've made progress on my component, but I'm facing an issue where the links are going to http://localhost:5173/[object%20Object]. It seems like I've reached a mental roadblock. This is what my component looks like: <template lang="p ...

Logging on the client side with node.js without the need for additional modules

Do you think it's a good idea to wrap my minified JavaScript code in a try-catch block so that if a client-side error occurs, it sends a POST request to my server and appends the error to a log file on the server? The main concern here is security - ...

Using Angular directives with Kendo UI Grid to create a foreign key column

I'm currently working on building a Kendo Grid that incorporates 2 foreign key columns using the Angular directives for Kendo. The challenge I am facing is that while one column works perfectly fine, the other does not (each works independently of the ...

How to successfully integrate the three.js example of webgl_loader_obj_mtl.html into an ASP.NET WebForm, even when encountering issues with mtlLoader.setPath

While attempting to integrate the webgl_loader_obj_mtl.html example from three.js into an ASP.NET WebForm, I encountered an issue. Upon running the HTML, Visual Studio 2015 failed at mtlLoader.setPath. Has anyone else experienced the same problem? Addition ...

Creating subtle shadows for glb models in Three JS while maintaining soft lighting

Seeking advice on implementing proper lighting in my three.js project. I am new to working with 3D models and currently struggling to achieve the desired lighting effects. The image linked below illustrates what I aim to accomplish: https://i.sstatic.net/ ...

Is there a method to incorporate a click event for the confirm button in the ElMessageBox UI element?

When I try to remove data from the table, I need a warning message to appear in the center of the screen first. The delete function is already set up, but I'm struggling to figure out how to implement a confirm button click event with ElMessageBox. I ...

What steps can I take to resolve a dependency update causing issues in my code?

My program stopped working after updating one of the dependencies and kept throwing the same error. Usually, when I run 'ng serve' in my project everything works fine, but after updating Chartist, I encountered this error: An unhandled exception ...

Run a JavaScript function just a single time

Here is a straightforward JavaScript function I've been working on: <script type="text/javascript> var popupValue = '0'; if(popupValue == '0') { $(document).ready(function () { $(document).on( ...

Keep the footer at the bottom of the screen without needing to define a 100vh in Material UI

In the process of developing my react app with MUI framework, I encountered various challenges in creating a sticky footer at the bottom of my screen. After exploring different solutions, one approach that I found most satisfactory is as follows: export de ...

What does dist entail?

I am currently utilizing gulp to create a distribution folder (dist) for my Angular application. After consolidating all the controllers/services JS files and CSS, I am now faced with handling the contents of the bower folder. In an attempt to concatenat ...

employing arrays in JavaScript

Hello there! I am new to programming and recently started learning JavaScript. I am working on a simple program where the user can select a name from a list of candidates and then choose a category to receive information about that candidate. However, th ...

NodeJS: Use setInterval to continuously execute a loop as long as a specific variable remains true

function setNormal() { console.log(1) } function setAlert() { console.log(2) } function alertFunction() { alertVar = setInterval(alertFunc, 600); } function alertFunc() { setAlert() setTimeout(setNormal, 300) } alertFunction() }); I ...

Tips on stopping or unbinding a previous $watch in AngularJS

Currently, I am utilizing $watch in a dynamic manner which results in the creation of another $watch on each call. However, my intention is to unbind the previous $watch. $scope.pagination =function(){ $scope.$watch('currentPage + numPerPage', ...