Display a variety of images using ng-grid in angular js

I have integrated ng-grid into my Angular application and I am looking to display different images in the ng-grid based on certain conditions or row index. Below is the HTML code snippet:

<html ng-app="myApp">
<head lang="en">
    <meta charset="utf-8">
    <title>Getting Started With ngGrid Example</title>
    <link rel="stylesheet" type="text/css" href="ng-grid.css" />
    <link rel="stylesheet" type="text/css" href="style.css" />
    <script type="text/javascript" src="jquery-2.1.4.js"></script>
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript" src="ng-grid-2.0.14.debug.js"></script>
    <script type="text/javascript" src="main.js"></script>
</head>
<body>
    <div ng-controller="MyCtrl">
        <div class="header"> <span> DealerShip </span></div>
        <div>

        </div>

        <div class="gridStyle" ng-grid="gridOptions"></div>
    </div>
</body>
</html>

Script code:

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function ($scope) {
    $scope.myData = [
        { asset: "0", sno: "FTL32541", vehicle: "SUV", fuel: "40%", distance: "120km" },
        { asset: "1", sno: "FTL18723", vehicle: "Sedan", fuel: "30%", distance: "200km" },
        { asset: "2", sno: "FTL28756", vehicle: "Coupe", fuel: "90%", distance: "420km" },
        { asset: "3", sno: "FTL48733", vehicle: "EV", fuel: "40%", distance: "210km" },
        { asset: "4", sno: "FLT38524", vehicle: "SUV", fuel: "10%", distance: "200km" },
        { asset: "5", sno: "FLT48733", vehicle: "TRuCK", fuel: "100%", distance: "720km" },
        { asset: "", sno: "FLT98755", vehicle: "Sports", fuel: "20%", distance: "320km" }
    ];
    $scope.gridOptions =
        {
        data: 'myData',
        enableFiltering: true,
        enableHiding: false,
        enableSorting: true,
        columnDefs:
            [
            { field: 'asset', displayName: 'Asset', cellTemplate: '<div class="ngCellText" ng-class="col.colIndex()"><span ng-cell-text><img class="flag" ng-src="{{ row.getProperty(\'asset\') | imagefilter}} "</img></span></div>' },
            { field: 'sno', displayName: 'Serial Number' },
            { field: 'vehicle', displayName: 'Vehicle Type' },
            { field: 'fuel', displayName: 'Fuel Level' },
            { field: 'distance', displayName: 'Distance' }
           ]
    };
});
app.filter('imagefilter', function () {

    return function (asset) {

        if (asset === '0') { return 'http://goo.gl/aFomAA'; }
        if (asset === '1') { return 'http://goo.gl/vxCnLC'; }
        if (asset === '2') { return 'http://goo.gl/aFomAA'; }
        return 'unknown';
    };

})

After implementing the filter, I encountered an issue with the generated HTML output as shown below:

<div ng-cell=""><img class="flag ng-scope" ng-src="http://goo.gl/aFomAA" <="" img="" src="http://goo.gl/aFomAA"></div>

I aim to integrate the proper image tag within the ng-grid rows.

Answer №1

Your image tag lacks proper formatting. The current code is:

<img class="flag" ng-src="{{ row.getProperty(\'asset\') | imagefilter}} "</img>

However, the correct way for a self-closing img tag is:

<img class="flag" ng-src="{{ row.getProperty(\'asset\') | imagefilter}}" />

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

utilizing Typescript object within an array of objects

How can I optimize typing this nested array of objects? const myItem: Items[] = [{ id: 1, text: 'hello', items: [{ id: 1, text: 'world' }] }] One way to approach this is by using interfaces: interface It ...

Troubleshooting: Issue with Nested ng-class in AngularJS Directives

Encountering a peculiar issue with an AngularJs Directive involving nested ng-class. Take a look at this JSFiddle showcasing the problem in a hypothetical scenario: Here. HTML <div in-directive ng-class="{ 'testing1': 1 == 1 }"></div&g ...

What is the technique to transfer the value from collection_select to the onchange method in Rails?

In my task, I need to extract the selected value from the collection_select drop-down menu and pass it to an onchange function. However, when I access the "name" attribute, the printed value is source[index]. Instead, I want to retrieve the actual value ...

Creating a javascript function to update content on click

Recently, I've been designing a webpage and encountered an issue. I want the text in a specific area to change whenever a user clicks on a link. Below is the code snippet related to the section I want to modify using a JavaScript function. <div id ...

Is there a way for me to detect click events on Windows notifications?

Currently, I am attempting to send notifications through the node-notifier package. The goal is for a click on the notification to lead to a specific link. However, I am encountering difficulties in listening to the click event - the events provided by the ...

Verifying the type and quantity of an item

Looking at a piece of code where a specific condition is being checked, I'm curious to know why someone might want to skip this particular condition. var number = /^\d+/; var type = Object.prototype.toString.call(obj); for(var key i ...

Save the current state of the collapsible navigation menu

I developed a website for a friend, but I'm encountering an issue with my js/css vertical and collapsible menu. Every time I click a link, the new page loads and the entire menu collapses again. I've tried to figure out a way to "store" the infor ...

ng filtering with a controller-defined scope

I am currently working on a webpage with AngularJS and I am looking to implement some filters on the site. Here is the HTML code I have: <div ng-repeat="data in datas | filter:{area:course} | filter:{subject:subFilter} | filter:{city:cityFilter}"> ...

Include a suffix after the user's input in a Material UI TextField

I need to implement a TextField element that displays the entered value followed by an Input Adornment. Can I have the percentage sign (%) appear after the entered value instead of at the end of the input? Currently, the percentage sign (%) appears at the ...

Dealing with Nested .nav-pills in Bootstrap 3: A Guide to Over

Struggling with nesting .nav-pills within another .nav-pills (utilizing bootstrap3). Could the issue be related to the nested JS in the pill-content? I am aiming to construct a single-page website using the default JS nav pills for seamless navigation thr ...

Executing a Python script asynchronously from a Node.js environment

I am currently managing a node.js program that handles approximately 50 different python script instances. My goal is to implement a throttling mechanism where only 4 processes can run in parallel at any given time. Initially, I attempted to create a simp ...

Angular ng-repeat app not functioning as intended due to a lack of complexity

Seems like I've stumbled upon a rather silly issue here and it might be a duplicate question, but I'm still scratching my head over it. I have this incredibly basic Angular app along with some HTML. While debugging in Chrome, I can clearly see ...

Accessing the data from an HTML5 slider using JQuery

Having some difficulty fetching a value from an HTML5 slider using JQuery. Here is my code snippet: JQuery Code: // Getting the value from slider one $("#submit").on("click", function(evt) { var sliderValue = $('#slider01').attr('value ...

Gaining entry to a JavaScript prototype method

I'm currently working on a prototype function called event. Prototype Func("input_element_id").event("keyup",function(){ alert("Works on keyup in an Input!"); } Func.prototype= { keyup: function(func){ //adding event listener and c ...

Issues with the bootstrap 4 modal update

I am currently working on an app in Spring Boot with JSP for the frontend, and I have encountered a specific issue: In my application, there is a table with multiple rows where I can click a button to update each row as needed. When I click on one row and ...

Tips for directly referencing the z-index property within a class in HTML - is that possible?

Is it permissible to include the following elements within a <div class="z-index-2"> tag? Is this considered correct or incorrect? Blockquote ...

Tips for delaying the rendering of a template in an angular ui router state until all dependencies are resolved

Is it possible to use the resolved data from a resolve block in the templateUrl function? How can this be accomplished, or is there a way to ensure that the resolution is completed before calling the templateUrl function? ...

Tips for enabling drag and drop functionality for table rows on a mobile device display

I have been experimenting with this code snippet on jsfiddle: http://jsfiddle.net/SSSUUUSSS/Bsusr/1/.. It is utilizing angularjs-ui. <tbody ui:sortable ng:model="list"> <tr ng:repeat="item in list" class="item" style="cursor: move;"> ...

Steps to make a sliding tab

I am currently faced with the challenge of coding the support tab on the right side of this page - . The tab currently slides out when clicked. I found the current tab slide-out effect here: My goal is to have both the main tab and the side tab appear wh ...

Calculating the total of time values using Javascript

I am working with a set of time values stored as JSON data: 14:49:09 00:16:46 00:00:05 My goal is to loop through these times and calculate their sum to obtain the final total time: The resulting time will be 15:06:00 using Javascript. ...