AngularJS grid designed to emulate the functionalities of a spreadsheet

I have been facing challenges with Angular while attempting to recreate a spreadsheet layout in HTML using ng-repeat. Despite extensive research, I have not found a solution.

My goal is to display data in a table format as shown below:

<table>
  <tr>
    <th>Account</th>
    <th>Jan</th> 
    <th>Feb</th>
    <th>Mar</th>
    <th>Apr</th>
    <th>May</th>
  </tr>
  <tr>
    <td>Sales</td>
    <td>50</td>
    <td>150</td>
    <td>250</td>
    <td>350</td>
    <td>450</td>
  </tr>
  <tr>
    <td>Variable Costs</td>
    <td>150</td>
    <td>250</td>
    <td>150</th>
    <td>150</td>
    <td>250</td>
  </tr>
</table>

There are 3 key elements that I need to incorporate into the table.

While I can easily create the columns using ng-repeat, my JSON data structure looks like this:

["Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"]

and my Angular code snippet is shown below: Account Description {{value }}

I'm also able to generate the rows using another JSON array:

["Sales", "Variable Costs", "Advertising"]

      <tr ng-repeat="(header, value) in tm1rows" >
        <td>{{value }}</td>

However, I am struggling to populate the table cells with values corresponding to a specific Account and Month combination from the provided JSON:

Example JSON data:

[{
        "account2": "Sales",
        "month": "Jan",
        "TM1CubeValue": 9090
    },
    {
        "account2": "Variable Costs",
        "month": "Jan",
        "TM1CubeValue": null
    },
    {
        "account2": "Sales",
        "month": "Feb",
        "TM1CubeValue": null
    },
    {
        "account2": "Sales",
        "month": "Feb",
        "TM1CubeValue": 1999
    },
    {
        "account2": "Variable Costs",
        "month": "Feb",
        "TM1CubeValue": 99
    }
]

Any assistance on how to solve this issue would be highly appreciated.

I believe I have provided sufficient information, but I am willing to provide more details if necessary.

Cheers!

Answer №1

After adjusting your data according to the comments in the question, you can utilize ng-repeat along with filters to populate the table and apply filtering based on the 'account2' type in both the th and td elements as demonstrated below:

 <table>
    <thead>
      <tr>
        <th>Account</th>
        <th ng-repeat="i in months">{{i}}</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Sales</td>
        <td ng-repeat="i in tableData | filter : {'account2' : 'Sales'}">{{i.TM1CubeValue}}</td>
      </tr>
      <tr>
        <td>Variable Costs</td>
        <td ng-repeat="i in tableData  | filter : {'account2' : 'Variable Costs'}">{{i.TM1CubeValue}}</td>
      </tr>
    </tbody>
  </table>

Remember to store your months array and actual data in dedicated $scope variables.

app.controller('MainCtrl', function($scope) {

  $scope.months = ["Jan",
    "Feb",
    "Mar",
    "Apr"];

  $scope.tableData = [{
    "account2": "Sales",
    "month": "Jan",
    "TM1CubeValue": 9090
  }, {
    "account2": "Variable Costs",
    "month": "Jan",
    "TM1CubeValue": null
  }];
});

An interactive example can be found in this Plunker.

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

How does the 'order' stack up against the 'products' array? Display the names of the products that are included in the order

I have an array of items for sale: const products = { { id: product1, name: 'Product A', price: 100 }, { id: product2, name: 'Product B', price: 200 }, { id: ...

Wrap the words around to fit a rectangle with a specific ratio, rather than a specific size

Does anyone have a solution for breaking text at word boundaries to perfectly fit a rectangle with a specific approximate ratio, such as 60:40 (width:height)? Please note that this is not just about setting a width limit (e.g. 80 characters or 600px) and ...

Ionic applications utilize nested views that load within one another

Take a look at my Plunker example to see the code snippet below in action. .state('tabs', { url: "/tab", abstract: true, cache: false, templateUrl: "tabs.html" // controller: &a ...

Exploring the OAuth 2.0 integration with OpenID Connect, Loopback, and Keycloak

I'm having trouble establishing a connection to Keycloak from Loopback. I've been experimenting with the keycloak-connect library available at: https://github.com/keycloak/keycloak-nodejs-connect This snippet shows my current implementation in ...

Confusion surrounding the concept of returning an arrow function from a Vuex storage getter

I delved into a Vuex course and the journey was smooth sailing until they introduced an arrow function in a getter, then utilized it in a computed property and action. Behold the code: item structure: const _products = [ { id: 1, title: "iPad 4 Mini", ...

Greetings: Obtaining an array of text within the <td> tags

Here is the HTML Source: <td bgcolor="#ffffbb" colspan=2><font face="Verdana" size=1>2644-3/4<br>QPSK<br><font color="darkgreen">&nbsp;&nbsp;301</font> - 4864</td> I am looking to extract text array wit ...

Encountering difficulty extracting information from JSON file within AngularJS factory

I am struggling with a Json file named discover.json located at json-files/discover.json {"data": [ {"username": "aky123", "name": "ajay"}, {"username": "sky123", "name": "sanjay"} ]} Below is my factory setup: var myAppServices=a ...

My collection consists of objects arranged in this manner

let attributeSet = [{ "id": 1, "value": 11 }, { "id" : 1, "value": 12 }, { "id" : 1, "value" : 13 }, { "id": "2", "value& ...

Transforming Color with JQuery on the Fly

Check out this Jquery script that gradually changes the color of an object from (0, 0, 0) to (255, 255, 0). $(document).ready(function(){ var r = 0; var g = 0; changeColor(r, g); }); function changeColor(r, g){ var newColor = "(" + r + ", ...

Is there a way to ensure this filter functions consistently within the controller?

I am trying to filter data in my controller Currently, this code works: <input style=" margin-left: 15px; margin-top :50px; width:31%;" class="form-control" ng-model="ValueSearch" type="text" placeholder="Sort..." id="searchCompanies" autofocus /&g ...

Serialization of ISO8601 dates with a JAX-RS client in a container setting

I am currently working on a project that involves utilizing a JSON REST API with a POST /items endpoint. This specific endpoint requires an object structure like: {"name": "item_name", "timestamp": "2018-01-01T01:01:01.001"} In order to interact with thi ...

To achieve two-way binding with a scope variable, it is necessary to enclose it within an

I am in need of creating a basic search feature for some items. I have designed a simple control with a button that clears the search query: <div class="item item-input item-button-right"> <i class="icon ion-ios-search placeholder-icon">&l ...

What is the best approach to retrieve a username from a SQL database using AJAX within a Flask application

I have been attempting to retrieve the username column information from SQL using ajax, but only the username of the first ID is being returned. However, I need all usernames to be fetched. Below is the model: class users(db.Model): id=db.Column(db.I ...

Using the arrow keys to navigate through a list of items without using jQuery

Exploring ways to develop a basic autocomplete feature without relying on third-party dependencies has been my recent project. So far, I have managed to populate a results list using an ajax call and complete fields with mouse onclick events for each optio ...

Why do we even need to use $validators and $setValidity in our code?

There seems to be some confusion surrounding the use of $validators and $setValidity. I've noticed that both these functions seem to achieve the same outcome, so do we really need to use both? Please correct me if I'm mistaken. Even without the $ ...

Executing multiple AJAX calls at the same time in PHP

I have a puzzling question that I can't seem to easily solve the behavior of this situation There are two ajax call functions in my code: execCommand() : used for executing shell commands through PHP shell_exec('ping 127.0.0.1 > ../var/l ...

Retrieve information from an HTML form

Currently, I have a piece of Javascript that fetches the HTML for a form from the server using an XMLHttpRequest and then displays the form on the page. I am looking for a solution to extract the data that would be sent via POST if the form were submitted ...

What is the best method for implementing a recursive loop to parse a JSON file?

I implemented the Nestable2 plugin into my Django project to create a tree structure. Whenever a user modifies the order of the tree nodes, the plugin sends me a JSON data via Ajax to the server. The JSON format returned by Nestable2 is as follows: [{"i ...

Change Nested Array Structure to JSON Hierarchy with PHP

I am working with an array in a nested set model, and I need to convert the array to JSON format with parent-child relationships at infinite levels. Here is an example of the array: array(6) { [0]=> array(8) { ["title"]=> string(5) "cat ...

Updating JSON objects within JSONB columns in Postgres can be

{ "trip": { "arrival": { "time": "2020-09-03T10:05:00.000Z", "category": "Scheduled", "driver": "sunnia", "comp ...