Utilize AngularJS's OrderBy feature to iterate through the list for navigation in ascending order by Index

I am looking to customize the order of an Object-array using ng-repeat based on their index keys in ascending order.

$scope.paneContainer = {books:[{}] } ;  
$scope.paneContainer.books[0].science = { title:"science", content:"web/app/views/partials/science.html"  };
$scope.paneContainer.books[0].alphabets = { title:"alphabets", content:"web/app/views/partials/alphabets.html"  };
$scope.paneContainer.books[0].folks = { title:"folks", content:"web/app/views/partials/folks .html" };

Implementation on the front end:

<ul class="ui-tabs-nav">
    <li ng-repeat="tab in paneContainer.books[0] track by $index | orderBy:$index " class="">
        <strong>{{tab.title}}</strong></a>
    </li>
</ul>

The expected output should be:

  • Science
  • Alphabets
  • Folks
  • Currently, Angular arranges the list alphabetically.

    Answer №1

    As Mike mentioned, the concept of order does not inherently apply to objects. If you prefer not to convert it into an array (although that is typically the simplest and most effective solution), you can instead add a displayOrder property to the object and then utilize a filter to organize it by displayOrder.

    angular.module('app').filter('orderByDisplayOrder', function () {
        return function (obj) {
            var array = [];
            Object.keys(obj).forEach(function (key) {
                array.push(obj[key]);
            });
            array.sort(function (a, b) {
                return a.displayOrder - b.displayOrder;
            });
            return array;
        }
    });
    

    You can then apply this filter for sorting purposes.

    <ul class="ui-tabs-nav">
    <li ng-repeat="tab in paneContainer.books[0] track by $index | orderByDisplayOrder" class="">
        <strong>{{tab.title}}</strong></a>
    </li>
    

    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

    Storing data retrieved from an asynchronous call in AngularJS to a variable

    Here is the code where I am attempting to utilize promise to store data from an asynchronous call in a variable, but it's not functioning as expected. I am relatively new to promises and after some research, I learned that promises can be helpful in s ...

    Adding elements to a list using the appendChild method

    Hey there! I have a bunch of images and I'm trying to create a navigation list item for each image. Here's the code I currently have: var bigImages = $('#imagesList li img'); // grabs the Big Images Next, I've set up a ul with t ...

    Having difficulty implementing dynamic contentEditable for inline editing in Angular 2+

    Here I am facing an issue. Below is my JSON data: data = [{ 'id':1,'name': 'mr.x', },{ 'id':2,'name': 'mr.y', },{ 'id':3,'name': 'mr.z', },{ & ...

    Which is better: Utilizing Ajax page echo or background Ajax/direct HTML manipulation?

    I am facing a dilemma and I could really use some guidance. Currently, I am in the process of developing an ordering system using PHP/Smarty/HTML/jQuery. The main functionality revolves around allowing sellers to confirm orders on the site. My goal is to ...

    The initial ajax request successfully connects to the file, but encounters a 404 error upon the second attempt

    I am currently encountering an issue with a jQuery post function. The function is designed to run multiple times and then stop, which it has successfully done in the past. However, now the problem arises when the function tries to execute itself again afte ...

    Creating a flexible image layout within a container with relative positioning

    I attempted to create a basic slideshow with images sliding from right to left as an animation: let slides = document.querySelectorAll('.img-container'); let l = slides.length; let i = 0; setInterval(function(){ i = (i + 1) % l; if(i == 0){ f ...

    Reload Popup Box

    I am currently developing a website using Django and I am in need of a popup window that can display logging messages and automatically refresh itself every N seconds. In order to achieve this, I am utilizing the standard Python logger, JavaScript, and Daj ...

    Identifying whether a child component is enclosed within a specific parent using React

    Is there a more elegant and efficient method to determine if the parent of SeminarCard is Slider? Currently, I am passing this information through a prop. The prop value (true/false) is used to provide additional padding. When used independently: <Semi ...

    Eliminate the need for background video buffering

    I have a piece of code that I'm using to remove all the created players for my video elements. The code works fine, but I'm facing an issue where the video keeps buffering in the background after it's changed via an ajax call success. Can yo ...

    Firefox experiencing trouble handling flexbox overflow

    Having some trouble with a test project that involves using flexbox. The task at hand is to create a dashboard with multiple lists of cards arranged side-by-side with infinite overflow. I was able to achieve this layout, however, the issue arises when eac ...

    Error: An unexpected identifier was found within the public players code, causing a SyntaxError

    As a newcomer to jasmine and test cases, I am endeavoring to create test cases for my JavaScript code in fiddle. However, I'm encountering an error: Uncaught SyntaxError: Unexpected identifier Could you guide me on how to rectify this issue? Below is ...

    Invisible and Unrestricted automatic playback

    Why is auto play muted in both Firefox and Chrome? How can we code it so that browsers don't block it? Here's the code I'm using: <audio id="audio1" src="https://notificationsounds.com/storage/sounds/file-sounds-1217-relax ...

    Electron's start script leads to project failure

    Despite extensively searching the internet for a solution, I have yet to find a definitive answer that actually works. My last resort was downloading the example from Electron's website and directly inserting the scripts into my project. However, whe ...

    Enable automatic full-screen mode on Google Chrome upon page load

    I would greatly appreciate it if you could provide an answer to this question, even if it is marked as a duplicate. I have tried various solutions and sought help, but unfortunately, nothing seems to be working for me... What I really need is for the brow ...

    What is the best way to display a JavaScript loop on a webpage instead of just in the console?

    As a beginner in programming, I have taken on the challenge of creating my own FizzBuzz program. The goal is to write a program that displays numbers from 1 to 100. However, for multiples of three, it should output "Fizz" instead of the number; for multipl ...

    Tips for iterating through/range over an array depending on the initial point?

    var ZODIAC = ["RAT", "OX", "TIGER", "RABBIT", "DRAGON", "SNAKE", "HORSE", "SHEEP", "MONKEY", "ROOSTER", "DOG", "PIG"]; var STARTING_ZODIAC = "MONKEY"; Is there a way to output the elements in the array that start with Monkey and end with Sheep? ...

    ng-if directive does not show data in AngularJS

    I have a dynamic collection of images and videos that I want to display one at a time. Specifically, when I click on an image ID, I want it to show the corresponding image, and when I click on a video ID, I want it to show the relevant video. Below is the ...

    What's with the lack of acknowledgment when I triumph in rock, paper, scissors, lizard, spock?

    My game is running smoothly, except for when lizard or spock are involved and I win. For some reason, the outcome does not display correctly, even though it works fine when I lose. I've double-checked for typos but couldn't find any. If someone c ...

    Creating directional light shadows in Three.JS: A Step-by-Step Guide

    Can shadows be generated using a DirectionalLight? When I utilize SpotLight, shadows are visible. However, when I switch to DirectionalLight, the shadow functionality doesn't seem to work. ...

    AngularJS - Issue with retrieving the most recent entry during $routeChangeStart event

    I am utilizing the $routeChangeStart function to redirect authorized users to specific URLs and prevent unauthorized access to special pages. In addition, I have dynamically generated pages that can be accessed via their unique page slugs. To achieve this ...