Organizing Results into Divs Based on Column Value Using AngularJs

I've been racking my brain trying to figure out the best way to organize and display my results in separate divs, specifically using Bootstrap col-md-4's. This is for a chat messaging app that I'm in the process of developing. I have rooms and messages, and I want each room to only display its messages grouped together in divs.

The structure of my results ($scope.messages) is as follows:

messages = [
    {"room_id":1,"message_id":2,"message_content":"hiiii","message_time":"2015-11-28 23:22:57","user_name":"Aaaa1aa"},
    {"room_id":1,"message_id":1,"message_content":"hi there","message_time":"2015-11-28 23:22:40","user_name":"aaaa2aa"},
    {"room_id":2,"message_id":3,"message_content":"hyyy","message_time":"2015-11-28 23:23:07","user_name":"Guest"},
    {"room_id":2,"message_id":4,"message_content":"aaaaa","message_time":"2015-11-28 23:23:20","user_name":"aaaa2aa"}
];

I aim to display each set of results grouped by the room_id column so that the output looks like this when looping through the data:

<div class="col-md-4"> Room Id - 1
aaaa2aa - hi there
Aaaa1aa - hiiii
</div>

<div class="col-md-4"> Room Id - 2
Guest - hyyy
aaaa2aa - aaaaa
</div>

After trying various approaches, I attempted the following code snippet, but unfortunately, it did not yield any results.

<div class="col-md-4" ng-repeat="data in messages">
<div ng-repeat="data in data[$index]">

{{data.user_name}}
{{data.message_content}}

</div>
</div>

Any help would be greatly appreciated.

Here is what finally worked for me, but please note that you need to include the module filter as a dependency first:

<div class="col-md-4" ng-repeat="data in messages | groupBy: 'room_id'">
    {{data[$index].room_id}}
<div ng-repeat="data in data">
    {{data.user_name}}
    {{data.message_content}}
    </div>
</div>

Answer №1

To categorize the results, one can utilize the groupBy function:

(k, v) in messages | groupBy: 'room_id'

Another approach is to employ the filter function:

<div class="col-md-4" ng-repeat="data in messages | filter: '{'room_id': 2}'"> //display only the items with room_id set to 2 and so forth

</div>

Nevertheless, this technique may lead to repetition and excessiveness.

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 jQuery to generate a JSON object dynamically based on the values entered in each input field

I'm facing a situation where I need to extract data from a JSON format using PHP. However, I'm struggling with how to structure the Javascript object in order to dynamically create the JSON format. Here is my current scenario: <input title=" ...

Tips for concentrating on the initial input field produced by an ng-repeat in AngularJS

Currently, I am dynamically generating input fields using ng-repeat and everything is working smoothly. However, my requirement is to set focus on the first input that is generated by this ng-repeat based on their sequenceId values. So far, I have attempte ...

Button activates SVG animation

Currently, I am working on an animation for a button using SVG. The animation utilizes the classes .is-loading and .is-success. However, when I click on the button, the animation does not execute as expected. I'm having trouble identifying the error w ...

PHP displaying incorrect value after modifying value in JavaScript

One issue I am facing is with an html page that submits a form through javascript. Prior to the submission of the form, I modify the value of a hidden tag, which should then be retrievable in php. However, when attempting to retrieve the value in php, it a ...

Inquiries about utilizing setTimeout with backbone.js and effectively managing timeouts

One of the questions I have is related to clearing timeouts using clearTimeout(content.idTimeout) for a specific idTiemout. But how can I clear all timeouts at once? Here is the model I am working with: var ContentModel = Backbone.Model.extend({ URL: "htt ...

Add JavaScript code to your project without bundling it as a module

Is it possible to incorporate a JavaScript library into webpack that is not structured as a UMD-compatible module (AMD, CommonJS)? I want the library to be included in a <script> tag only when necessary and managed by webpack without passing through ...

Exploring the capabilities of Serenity/Js for conducting tests with the protractor-Jasmine framework. Are there any specific features in Serenity that cater to

I am in the process of integrating serenity reports into my tests built using the protractor-jasmine framework. So far, all my research has only shown examples of serenity integration with the protractor-cucumber framework. Below is a sample config file ...

Image swaps with timer for button

I am working on a project where I have a page with 5 image buttons arranged horizontally. The objective is to have the images on each button change sequentially every 3 seconds in a continuous loop. Here is my code: $(document).ready(function (){ Beg ...

How can a JavaScript array be transmitted as a JSON value?

Is there a method to pass a JavaScript array as a JSON variable in my AJAX query? ...

Looking to retrieve object values in JavaScript in a dynamic manner?

Imagine having an array of objects with nested objects. The data is always changing, meaning the keys may vary each time. For instance: [ { "uuid": "53e7202c-28c8-4083-b910-a92c946a7626", "extraIdentifiers": { "National ID": "NAT2804" }, ...

Error encountered: index.html:17 - An InvalidStateError was thrown when attempting to execute the 'send' function on the XMLHttpRequest object. The object's state must be OPENED in order to send the Ajax request

When attempting to run index.html on a local host with xampp, an error is encountered: index.html:17 Uncaught InvalidStateError: Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED.sendAjax @ index.html: ...

Counting gettext values up to a specified number of occurrences

When clicking a button, the elements within this div receive number values. If a specific pattern is reached in the text of these elements, the test should be ended. For instance, if there are 5 elements under the "someelement" div and three of them conta ...

The progress bar in Next JS 13 seems to be malfunctioning and is not displaying any

After transitioning to the new Next JS 13 app directory, I encountered an issue where the progress bar at the top does not function when the page loads. In the previous version, Next JS 12, there was a package named nprogress and nextNprogress that simpli ...

ag-Grid open-source server-side filtering

Currently, I am utilizing the ag-grid community version for my project. I am inquiring whether it includes support for server side filtering and paging. If not, what alternative methods can be used to implement filtering with the ag-grid community versio ...

Creating a reusable anonymous self-invoking function

Here is a function that I am working with: (function(e, t) { var n = function() { //code, code, code }; //code, code, code e.fn.unslider = function(t) { //code, code, code }; })(jQuery, false) To execute this function, I have impleme ...

Exploring route grouping with angular ui-router

Is it possible to group routes in ui-router? Consider the following routes in my app: #/products/p1 #/products/p2 #/products/p3 One way to define states is as follows: .state('products.p1') .state('products.p2') .state('product ...

Choosing to collapse a nested JSON arrangement in a targeted manner

Facing a challenging problem here and feeling clueless on how to tackle it. Any guidance or pointer in the right direction would be highly appreciated. My dataset looks like this: data = { "agg": { "agg1": [ { "keyWeWant": " ...

What is the procedure for attaching console.log to "l" in vue.js?

The code in main.js includes the following: window.l = function () { } try { window.l = console.log.bind(console) } catch (e) { } It functions properly in non-Vue applications. However, when trying to use it in a Vue action/method like this: l("test") ...

The disappearing act of Redux state after being added to a nested array

When attempting to update my redux state, I am facing an issue where the state disappears. My approach involves checking for a parentId - if one exists, I insert the payload into the parent's children array. However, if no parentId is provided, I simp ...

What could be causing Next.js to throw an error upon completion of the MSAL OAuth process?

I encountered an error while building a website using next.js. The site is set up for production, and after the authentication process with MSAL for Azure AD integration, I am facing the below error during the OAuth loop. As a beginner in next.js coming fr ...