Creating and maintaining a table in AngularJS based on specific tag values

Hello, I am new to working with AngularJS and have come across a scenario that I need help with.

My goal is to create a table with multiple tbody elements (which are essentially rows, but using tbody for optimal functionality with angularjs 1.0.7).

Each row should be generated based on the items I have available.

Initially, when the page loads, there are no rows/tbody elements. I plan to connect to a backend via websocket to receive data pushes. With each push, I want to add a new row/tbody if it doesn't already exist with the same key from the backend. If the key does exist, I would like to update the existing row/tbody.

For a clearer picture, here is the code snippet I am using:

<tbody ng-repeat="quote in model.messages.message">             
    <tr>                                                                            
        <td bgcolor="#FFFFFF" valign="bottom" colspan="5"><font size="2">{{quote.value1}}</font></td>
    </tr>
    <tr>
        <td bgcolor="#FFFFFF" align="right"><font size="2">{{quote.value2}}</font></td>
        <td bgcolor="#FFFFFF" align="right"><font size="2">{{quote.value3}}</font></td>
    </tr>                               
</tbody></table>

In this code, the "key" referred to is quote.value1.

Answer №1

It all depends on how your server returns the data structure. Instead of using quote.value1, why not create an array of values to make your JSON cleaner like this:

Let's assume modal.messages =

   [{
      "id":1, 
      "values":
          [{"vid":1, "m":"This is a message value"},
            {"vid":2, "m":"THis is a second message"}]}, 
    {"id":2, "values":[...]}, ...]

Each message now has a values array.

You can then use ng-repeat over the values as follows:

     <tbody ng-repeat="quote in model.messages">             
            <tr ng-repeat ="message in quote.values">
               <td >{{message.m}}</td>
            </tr>
     </tbody>

This allows you to add the retrieved values to the correct part of the data and update the view accordingly.

In the controller, you can push new data to the array or update existing values. For example, if you receive data like this:

     data = {"vid":3, "m": "Another great message from the server"};

In your controller, you need to determine if it's a new message or an update. Adding a new one is simple:

     your_array.push(data);

This will append it to the array and update the view. I can demonstrate how to achieve something similar without using websockets.

    $scope.getUpdateData = function(quote){
      $http.post('YOUR_SERVER_URL')
        .success(function (data) {
         quote.values.push(data); //if it is new 
         //check if the id exists and replace it otherwise.
       });
    }

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

JavaScript functions do not work within the HTML code

I am currently working on my debut website and I'm facing an issue with the JavaScript file not functioning properly. Is there a chance you can help me troubleshoot this? I'm still in the learning stages of programming. This is the HTML content ...

Switch the monitored variable's value in VueJS

` ` I have a data variable that is of boolean type, and I have included this in a watch hook. When the value changes to true, I execute certain tasks within the watch function and everything works perfectly.` `watch: {` ` boolVar: func ...

Error: The JSON data type is not recognized - Asp.Net MVC 4

I've been struggling to send a complex JSON object to my action with no success. Below is my javascript code: $.ajax({ url: "http://localhost:52593/" + urlAction.Controller + "/" + urlAction.Action, type: type, dataType: dataType, da ...

What is the best way to receive a user's input date in Dynamics 365 using HTML/JavaScript?

My HTML webform is set up to capture user input like address, card number, city, and state in text format. However, within Microsoft Dynamics 365, I have a custom entity with fields for this information. When a user completes the webform and submits it, a ...

Using Backbone.js to Persist Information on the Server

Currently, I'm in the process of integrating my Backbone.js application with the server. One thing to mention is that I have customized my sync function in the collection to utilize jsonp: window.Project = Backbone.Model.extend({ initialize:func ...

Sending information from a controller to the $uibModal controller in AngularJS

I have a situation where I have an HTML page with a button called "alta". When this button is clicked, it opens another template as a popup. In this popup, I need to access the values of the fields on the original page. Here is the function for the "alta" ...

invoke a function upon successful completion of an ajax call in a datatable

Can we trigger a JavaScript function after a successful AJAX call in a datatable? Here is the code I am attempting to use: var dataTable = $('#app-config').dataTable( { "bAutoWidth": false, ...

Having trouble adjusting the label fontSize in ReactJS when using semantic-ui-react?

Is there a way to decrease the size of the label for a form input? In this scenario, attempting to set the fontSize does not work: <Form.Input label="Username" style={{fontSize: '10px'}} /> Does anyone have any suggestions on how to res ...

I am facing an issue where my dropdown is not being populated by the angularjs ng

I've encountered an issue with my directive that has an isolated scope. When using it inside an ng-repeat, I am unable to access any of my scope values except for the one within the ng-repeat. Specifically, I am trying to access the isolated scope pro ...

Developing JavaScript code for preventing blocking

Managing a large number of file requests from a tiny server has been a challenge for me. With approximately 100 files to fetch, my current approach using JavaScript's forEach loop is causing the server to crash due to the heavy load. links.forEach ...

Combining two JSON objects with sailsjs-nodejs to create a single merged object

Hello everyone, I am a beginner with Sailsjs-Nodejs. Currently, I have two JSON Objects in my controller that I need to merge/join in order to create a third object to send as a response. The output when using res.send(obj1) is: [ { total_fare: "37 ...

Cookie set upon clicking a particular element exclusively

Looking to toggle a class when clicking an element and preserve it post page load using jQuery or JavaScript. Here's what I currently have, successfully adding the class upon click but not retaining it after reload: jQuery("a.action.towishlist").clic ...

Executing multiple AJAX requests with promises in Angular based on an array of values

I have been working on implementing multiple ajax calls in sequence using Angular. Interestingly, everything seems to be working fine when I use $.ajax, but when I switch to using $http for server requests in Angular, things start to go awry. Both methods ...

Exploring the World of Subclassing Arrays in JavaScript: Uncovering the TypeError of Array.prototype.toString's

Can JavaScript Arrays be subclassed and inherited from? I am interested in creating my own custom Array object that not only has all the features of a regular Array but also contains additional properties. My intention is to use myobj instanceof CustomArr ...

Using jQuery to locate and delete multiple attributes from div elements

My goal is to locate all div elements with the class name "comment-like" that also have data-id attributes set to 118603,1234,1234,118601,118597. If a div contains any of these data values, then I want to remove that data attribute. I've attempted th ...

universal live media streaming application

I am currently developing a client-server solution with the following behavior: - The server side (C++) sends frames in a standard format. - The client side (C++) receives, decodes, and displays these frames. My goal is to integrate this solution into a c ...

Creating a jQuery AJAX data object that contains numerous values for a single key

My goal is to make an ajax call with multiple values in the same key within the data object. var data = { foo: "bar", foo: "baz" } $.ajax({ url: http://example.com/APIlocation, data: data, success: function (results) { console.log(res ...

Using the .ajax() function with jQuery to retrieve data and then using .find() on that data

Currently I am facing an issue while trying to extract the body tag from the result of the .ajax() call. Instead of getting the desired result, I only see undefined logged into the console... This is the section of code causing the problem: $(document).r ...

Does the DOMContentLoaded event fire only after all script tags have been downloaded?

Imagine this situation: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <title>Document</title> </head> <body> <script src="js/heavy_js_file.js" defer></script> &l ...

Mastering JQuery: Techniques for Managing Events Across Numerous Elements

My view page utilizes Ajax and JQuery to retrieve data from Codeigniter's controller, then presents it in HTML format. Below is the code for the Ajax post: $(document).ready(function(){ var fileId = 0; var wrapper = $("#preference"); ...