Difficulty in displaying or arranging data based on specific criteria in AngularJS

I am facing an issue with filtering data based on specific criteria in AngularJS. My requirement is to display all fields with filled data first, followed by fields with the value "-". However, since the filter is applied within ng-repeat, I am aware that running the filter only once is not possible due to AngularJS's 'dirty checking' mechanism.

'appStatusData'= 
             Data-A: "0.000"
             Data-Date: "2014-09-07 00:00:00.0"
             Data-B: "111"
             Data-C: "-"
             Data-D: "0517"
             Data-E: "462.000"
             Data-F: "-"
             Data-G: "No"
             Data-H: "-"

Desired Output:

            Data-A: "0.000"
            Data-Date: "2014-09-07 00:00:00.0"
            Data-B: "111"
            Data-D: "0517"
            Data-E: "462.000"
            Data-G: "No"
            Data-C: "-"
            Data-F: "-"
            Data-H: "-"

<ion-view view-title="Application Status" name="permit-view">
<ion-header-bar class="bar-stable bar-subheader">
    <div class="title" style="color: #2A4660"><u>Header</u></div>
</ion-header-bar>
<ion-content class="padding" has-subheader=true>
    <ion-list>
        <ion-item class="item" ng-repeat="(key, val) in (appStatusData | showDecimalData | showDataByNotNullFirst ) ">
            <b>{{key}} </b>&nbsp;&nbsp;&nbsp;{{val}}
        </ion-item>
    </ion-list>
    <button class="button button-full button-dark" ng-click="showList();">Show Details</button>
</ion-content>

Javascript: I have tried the following approach which is not yielding the desired results as 'present' and 'empty' arrays are coming up as undefined.

angular.module('myApp')
.filter('showDataByNotNullFirst', function($filter) {
    return function(array) {
        var present, empty, result;
        //console.log('Key is: ' + key + ' Value is: ' + value);
        $filter('filter')(array, function(key, val) {
            if (val != '-') {
                present[key] = val;
            } else {
                empty[key] = val;
            }
        });
        result = present.concat(empty);
        return result;
    };
});

Could someone please assist me in resolving this issue or offer suggestions for a solution?

Answer №1

In order to address this issue, I would suggest handling it within the controller.

Initially, you will be required to transform your object properties into an array.

var statusArr = [],
sortedArr = [];


// Iterate through each property within your object
for(var propertyName in appStatusData) {
    var obj = {
        name: propertyName,
        value: m[propertyName]   
    }
    statusArr.push(obj);
}

Subsequently, iterate over each item in the statusArr array. If the item equals '-', push it into the new array. If it is not a '-', unshift it at the beginning of the array.

For further reference on JavaScript Array methods.

angular.forEach(statusArr, function (s) {
    if (s === '-') {
        // append it to the 'right' side of the array
        sortedArr.push(s);
    } else {
        // insert it on the 'left' side of the array
        sortedArr.unshift(s);
    }
});

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

Can a person select a characteristic like "height" using Javascript?

Is it doable to set a height for an image in CSS, then detect this gradient using JS and double the width based on the height x2.25? Could this be achieved? ...

Error handling proves futile as Ajax upload continues to fail

Utilizing a frontend jQuery AJAX script, I am able to successfully transfer images onto a PHP backend script hosted by a Slim framework app. However, there is one specific image (attached) that is causing an issue. When the backend attempts to send back a ...

Different approach to handling response interceptors in AngularJS framework

Is there an alternative to $httpProvider.responseInterceptors since it has been discontinued in AngularJS V1.3? The interceptors that were functioning with Angular JS 1.2 are no longer operational in version 1.3 var angularErrorHandling = angular.module( ...

The User creation feature is malfunctioning within the node.js framework with express

I am facing an issue with my createUser function in the code snippet below. The createUser function is not working as expected, even though the deconstruction of const {firstName, id} = req.body is functioning correctly and displaying the name and id. It s ...

The content inside the bootstrap modal is not visible

My goal is to trigger a modal window when a specific div is clicked using bootstrap modal. However, upon clicking the div, the screen darkens but the modal body fails to appear. Code: <html> <head> <title></title> ...

Vercel Alert: (Azure) Missing OpenAI API key

After successfully implementing the openAI API in my Next.js application using the langchain library, everything worked flawlessly on localhost. However, upon deploying to Vercel (ProVersion), I encountered an error: Error: (Azure) OpenAI API key not fou ...

Removing a data entry from an array of objects using javascript

I have retrieved an array of objects from an api, and while I am able to successfully fetch the data, I am looking for a way to automatically remove objects with a "FINISH" status after a certain period of time. Initially, all records should be displayed, ...

PHP - display message that JavaScript functionality is disabled

Greetings everyone ! I am currently developing a board using bootstrap, and I have a database filled with information about sounds (name / genre / SoundCloud link ...) On my PHP page, I am echoing a button that calls a JS function with a PHP argument insi ...

Jquery failing to trigger click() on a div with an id

Within my erb file, there exists a script that looks like the following: function checkJquery() { if(window.jQuery) { jQuery(document).ready(function() { alert('onready'); $('div#habla_topbar_div').click(function( ...

Updating a React component upon pressing a button to trigger a re-render

Currently, I am in the process of developing a React component that allows users to input an ID and retrieve relevant information upon submission. The code for this component is structured as follows: export default class IDContainer extends React.Componen ...

The items in the array cannot be identified

var userInput = prompt('Select a fruit:'); var fruitList = ["apple", 'grapes', "orange"]; for(var i=0; i<fruitList.length; i++); if(userInput == fruitList[i]){ alert("Apologies, we are currently out of ...

Guide to setting up Nginx to host Rails application and Angular website on one domain

I am eager to configure Nginx to serve both my Rails application (using passenger) and an Angular front end. Essentially, any requests made to / should be routed to Rails, while requests to /admin/* should be directed to the Angular app. I stumbled upon ...

Unable to show additional tooltip information on the price history chart

In an attempt to enhance the tooltip of a price-history chart (stockChart), I am trying to display extra data. Here is a jsfiddle example: http://jsfiddle.net/z10dLcj8/1/ $(function(){ var priceHistoryObjArray = [ [1379883600000,47.19,'extra ...

Can a TypeScript file be created by combining a declaration file and a .js file?

It is commonly understood that declaration files are typically used for libraries rather than projects. However, let's consider a scenario where an existing JavaScript project needs to be migrated to TypeScript by creating d.ts files for each source ...

Encountering an issue with a Vercel deployment of a Next.js project due to an unexpected token error: 'export'

Every time I attempt to compile the latest version of my Next.js project, it throws a build error with the following log. Interestingly, when I execute npm run dev and view the project on the local port, everything functions perfectly. Have any insights? ...

Disable navigation bar icon for a specific page

I'm looking to disable the menu icon specifically on the menu page (normal page, not modal), but keep it active on all other pages. Using Meteor, React, and React-Router for my project. The diagram below illustrates the basic structure. Currently, I ...

What is the best way to retrieve the parameter of ng2-file-upload using endback?

I am attempting to retrieve a parameter using PHP in order to save it into a folder. However, my code is not working as expected. Here is the code snippet: Using the Ionic framework this.uploader.onBeforeUploadItem = (item: any) => { this.uploader ...

What is the best way to utilize an event listener in JavaScript to dynamically update the background color of my CSS?

I am trying to modify the background color of a list item, but I am encountering an issue with an if-statement. When I remove the if-statement and directly set the background color, it works fine. However, when the if-statement is included, it fails to w ...

Enclose every line of the paragraph within a <span> element

My <div> element is configured to display a paragraph without any line breaks, similar to the example below: <div> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dum ...

How can I close a dialog within an iframe in jQuery?

Suppose I initiate a dialog in this way: $('<iframe id="externalSite" class="externalSite" src="http://www.example.com" />').dialog({ autoOpen: true, width: 800, height: 500, modal: true, resizable: ...