Substituting missing data with the string "null" using AngularJS

I am struggling to display a table in an HTML page using ng-repeat. The issue arises when most of the entries in the table are null, as I am unable to replace them with either an empty space or the string "null". I attempted to use {{ row || 'null' }} but it did not provide the desired result. Whenever the table is generated, it becomes messy if there are a large number of null values present.

     <table>
        <tr>
            <th ng-repeat="colname in sqldata.collist">{{colname}}</th>
        </tr>
        <tr ng-repeat="rows in sqldata.tablist">
            <td ng-repeat="row in rows">{{ row || 'null' }}</td>
        </tr>
    </table>

https://i.sstatic.net/WQNJo.jpg

Answer №1

Have you tried using the classic ng-show and ng-hide technique to display content only if a value is 'null'?

Swap out

{{ row || 'null' }}

with

<div ng-show="row">{{row}}/div>
<div ng-hide="row">null</div>

Answer №2

Opting for a filter is a superior choice. This approach eliminates the need for duplicate DOM elements, utilizing a hidden and shown element instead. It also ensures that your view remains free of excess logic.

Furthermore, implementing a filter standardizes the 'no data' message throughout your application, making it applicable to almost all data binding scenarios.

<div ng-app="test" ng-controller="ClientCtrl">
    <div ng-repeat="elem in elems">
        {{elem | isempty}}
    </div>
</div>

Your JavaScript code:

angular.module('test', []).filter('isempty', function() {
    return function(input) {
        return isEmpty(input) ? 'No Value' : input;
    };

    function isEmpty (i){
        return (i === null || i === undefined);
    }
})

Link to JSFiddle example: http://jsfiddle.net/5hqp5wxc/

Documentation: https://docs.angularjs.org/guide/filter

Answer №3

One alternative is using ng-if
 <table>
        <tr>
            <th ng-repeat="colname in sqldata.columns">{{colname}}</th>
        </tr>
        <tr ng-repeat="rows in sqldata.tablelist">
            <td ng-repeat="row in rows">
              <span ng-if="row" >{{ row }} </span>
              <span ng-if="!row" > empty </span>
            </td>
        </tr>
    </table>

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 can I maintain the consistent speed of the Javascript timer even when the .deck is clicked?

Currently, I'm working on creating a memory card game. In this game, the deck of cards is represented by the class .deck. Strangely enough, every time I click on a card, the timer starts to speed up instead of keeping a consistent pace. How can I tack ...

Spin a dice counter and victory is claimed when the player lands on the lucky number of 21

Looking to create a dice counter game where you roll the dice as many times as you want and aim to hit 21 to win. If you go over 21, the game will notify you that you've lost and prompt you to try again. I'm having trouble figuring out how to dis ...

Problem resolved: Assign the nearest *multiple of ten* to a number if it surpasses the last *multiple of ten*

Consider this scenario: You have a number, let's say 12.5, and you want to round it up to the next multiple of ten (20). Or if you have a number between 20 and 30, it should be rounded up to 30. How can this be achieved using JavaScript? I have ...

Generate a Calendar Table using JavaScript in the Document Object Model (DOM

I have set up a table with 6 rows and 5 columns. The purpose of the table is to represent each month, which may have varying numbers of days. I want each column to display dates ranging from 1-30 or 1-31, depending on the specific month. Here's what ...

Encoding plain text characters in Javascript involves converting text into a format that can

I am currently developing a project in Javascript that utilizes AngularJS. Upon making an HTTP request to retrieve data, I notice that all characters are displaying properly. For instance, when I receive a string through AJAX like "räksmörgås", it appea ...

Bidimensional Selenium matrix

Could someone please help me understand how to extract values from a bi-dimensional array using Selenium? I have a resources.js file with the array created, and need to access it in Selenium. Here is the structure of the array: The array consists of 4 col ...

Attempting to toggle between four different stylesheets by utilizing a button and javascript functionality

After searching around, I have managed to find solutions on toggling between 2 CSS files. However, my requirement is a bit different - I need to use just one button to switch between 4 distinct CSS files. The primary CSS file that is loaded with the HTML i ...

Transfer an image and additional data to a remote server using AJAX and JSON

As I face a problem that seems unsolvable (which might also be your situation if you are reading this), I am seeking assistance in sending JSON data to a remote server via AJAX. This JSON data includes an image and a string: { "question": "Your Square ...

Managing the dropdown state within a dynamically generated list of dropdowns with VueJS

I'm currently facing an issue with managing the open/close state and value for multiple dropdowns in a dynamically created list. When I use a single dropdown, I can utilize an "isOpen" variable within data(), but in a list scenario, all the dropdowns ...

Tips for getting the sum of an array using the reduce method in Vue.js and returning the result array

As someone new to JavaScript, I apologize if my description of the issue is not clear. It's challenging for me to explain the problem I am facing. I have a computed function where I use the reduce method to iterate over my objects, perform calculatio ...

Lost Vuex struggles when it is manually navigating a route in Vue-router

Exclusively on Safari browser, I encounter an issue when manually entering the URL in the navigation bar after logging in. For example, if I type "http://x.x.x.x:8080/gestione", the browser loses the vuex store state (specifically the gest_user module with ...

Tabulator: Unable to download - Download type not found in database

Seeking assistance with downloading/exporting data to Excel using Tabulator (4.9) and Vue. Despite including SheetJS in my project as recommended by Tabulator's page, I am facing difficulties. The error XLSX is not defined occurs when I use: import & ...

Leveraging the power of cannon.js for detecting collisions within a three.js gaming environment

Trying to figure out a way to handle collisions between objects in my three.js project. Considering using cannon.js for its support of necessary primitives, but don't always need all the physics overhead, just collision detection in many cases. Don&ap ...

Ways to determine when some, yet not all, variables are identical to `null`

There has been an issue with the if statement in detecting whether some variables are equal to null, but not all. Can anyone assist me with this problem? else if (co != 2 || o != 0 || d != 0 || e != 6) if (c ...

Is there a need to make changes to a current gruntfile? How typically does the editing process unfold?

When it comes to an already established application, is there a specific procedure for declaring bower packages so that they are managed properly during the execution of "grunt build"? Do any modifications need to be made to the existing gruntfile? What ...

Encountered an error while trying to create a new NestJS project using yarn: Command execution failure - yarn install --

Having trouble installing a new NestJs project using yarn. Operating system : Microsoft Windows [version 10.0.19044.3086] Node version : 18.17.1 npm version : 9.6.7 yarn version : 1.22.19 Nest cli version : 10.1.16 I attempted to install by running : npm ...

Is there a way to modify the current image in firebase cloud storage?

I integrated the react-native-firebase</cod e> library into my React Native app.</p> <p>I am using Firebase Storage to store images in the cloud, and it is working perfectly. However, I have encountered an issue with the Edit function ...

Having trouble getting my Node.js application to deploy properly on Heroku

Check out my repository here: https://github.com/jacklemasters/tech-blog Although the app runs smoothly on my local machine, I can't figure out why it's not working on Heroku. I've tried moving the code completely and creating a new reposit ...

I am encountering an issue with my ui-router resolve where it is not returning any information despite no

I am struggling to connect my MongoDb and API REST with my Angular application. It seems like there is an issue with resolution. Currently, I am following a MEAN application tutorial on this website. This snippet shows my ui-router configuration. var ap ...

Creating responsive images in a navbar with HTML and CSS

I need help with this code snippet: <div> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="container"> <div class="d-flex flex-row justify-content-center align-items-center"> <a cla ...