Showcase pictures within an angular smart table

Is it possible to display images in a column within an ng smart table? We have several columns consisting mostly of data, with one column dedicated to displaying images. Following the ng smart table concept, I attempted to implement the code below which currently only shows a URL instead of the actual image.

https://i.stack.imgur.com/I3K1c.png

home.component.html

<ng2-smart-table [settings]="settings" [source]="notifications"></ng2-smart-table>

home.component.ts

import { myListDB } from "../../../shared/tables/myList";


export class MyBookingsComponent implements OnInit {

  public pageTitle = "My Home Page";

  public notifications = [];

  constructor() {
    this.notifications = myListDB.data;
}

  public settings = {
      editable:false,
      actions: {
          position: 'right',
          add: false,
          edit: false,
          delete: false
      },
      columns: {
          no: {
              title: 'No'
          },          
          brandlogo: {
              title: 'Brand Logo',
          },
          brand: {
            title: 'Brand'
          },
          title: {
            title: 'Campaign Name'
          }
      },
  };

  ngOnInit(){

   }

}

myList.ts

export class myListDB {
    static data = [
      {
        no: 1,
        brandlogo: "assets/images/brands/brand1.jpg",
        brand: "ABC Company",
        title: "XYZ Campaign"
      }

]
}

Answer №1

Here is my solution to resolve the issue at hand!

home.component.ts (Insert a 'type: html' in the brandLogo image array within the code)

import { myListDB } from "../../../shared/tables/myList";


export class MyBookingsComponent implements OnInit {

  public pageTitle = "My Home Page";

  public notifications = [];

  constructor() {
    this.notifications = myListDB.data;
}

  public settings = {
      editable:false,
      actions: {
          position: 'right',
          add: false,
          edit: false,
          delete: false
      },
      columns: {
          no: {
              title: 'No'
          },          
          brandlogo: {
              title: 'Brand Logo',
              type: 'html'
          },
          brand: {
            title: 'Brand'
          },
          title: {
            title: 'Campaign Name'
          }
      },
  };

  ngOnInit(){

   }

}

myList.ts (Include an img tag with the specified src as written in HTML)

export class myListDB {
    static data = [
      {
        no: 1,
        brandlogo: "<img src='assets/images/brands/brand1.jpg' class='imageClass'>",
        brand: "ABC Company",
        title: "XYZ Campaign"
      }

]
}

The desired outcome should resemble the following:

https://i.stack.imgur.com/dyjX5.png

Answer №2

To implement a custom component, follow these steps:

  • Create the custom component by:

Creating a New Custom Component (image-thumbnail.column.ts)

import { Component, Input, OnInit } from '@angular/core';
@Component({
 selector: 'image-thumbnail-column',
 template: `
   <div>
     <img [src]="value" />
   </div>
`,
})
export class ImageThumbnailColumn implements OnInit {
  @Input() value: string;
  @Input() rowData: any;
  ngOnInit(): void {
    console.log(this.value);
    console.log(this.rowData);
}
}
  • Add type:"custom" to the image column (column property) as the second step.

  • As the third step, assign renderersComponent: ImageThumbnailComponent to the image column in the smart table configuration. This step is crucial because when setting the type of the column to custom, the smart table relies on this property to communicate data to the component.

Updating the Home Component (home.component.ts)

import { myListDB } from "../../../shared/tables/myList";
import  { ImageThumbnailColumn } from "./image-thumbnail.column.ts";

export class MyBookingsComponent implements OnInit {

  public pageTitle = "My Home Page";
  
  // Other properties and methods
  
}

// Smart table settings with custom component usage

  • Ensure that your custom component receives two objects: value and rawData. Inside the component, define @Input() value: InterfaceOfValueForColumn; and @Input() rawData: InterfaceOfEachRawData;

  • Finally, include the custom component in the entry components of the module being utilized. For instance, if the smart table is used in home.module.ts:

@NgModule({
  ...
  entryComponents: [ImageThumbnailColumn],
  ...
})
export class HomePageModule {
}

In this scenario, the 'value' is a string containing the URL displayed in the attached picture. You can then showcase these images creatively according to your preferences.

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

Exploring the power of NestJS integration with Mongoose and GridFS

I am exploring the functionality of using mongoose with NestJs. Currently, I am leveraging the package @nestjs/mongoose as outlined in the informative documentation. So far, it has been functioning properly when working with standard models. However, my p ...

Leverage the compiler API to perform type inference

Exploring TypeScript's compiler API for basic type inference has proven to be a challenge with limited helpful information found in documentation or online searches. My goal is to create a function inferType that can determine and return the inferred ...

What is the best way to transform this JSON data into an array of key-value pairs in JavaScript?

Dealing with nested JSON data can be challenging, especially when trying to extract key-value pairs efficiently. If anyone has suggestions on how to simplify this process and improve readability, please share your insights. The goal is to transform the ne ...

Handling Promises in Angular 1 App using Typescript ES6/2015

Currently, I am working on an Angular 1.5 application that utilizes Typescript. My main concern is finding the most efficient way to handle ng.IPromise compared to Promise (ES6 promise). Personally, I would prefer to solely deal with the ES6 Promise type. ...

I'm having trouble opening a new Angular project even though both my Node.js and npm are up to date. Can anyone help me

Just starting my Angular journey. I have successfully installed the latest version of node.js with npm and then added Angular CLI to it. All good until I typed this command in the node.js prompt: ng new my-app But now I'm stuck here! Any ideas on wh ...

Delay in Angular routing

As I am still learning Angular, I am trying to familiarize myself with its routing functionality. In my click-through wizard, the final step includes a 'Complete' button that should post to the database and then route the user to a specific page ...

How to disable the onChange event in PrimeNG p-dropdown?

I'm currently utilizing PrimeNG's dropdown component. Each option in the list includes an icon that, when clicked, should trigger a specific method. Additionally, I need to execute another method when the onChange event of the dropdown is trigger ...

`Inconsistencies in console.log output with Angular Firestore``

I'm currently working on retrieving the id of selected data, but when I test it using console.log, it keeps outputting twice. The image below illustrates the console.log output. https://i.stack.imgur.com/IARng.png My goal is to fetch the id once and ...

Troubleshooting Node Server Startup Problems in AngularJS 2

I've been working on an Angular 2 sample app that was functioning perfectly until recently. However, when I attempt to run it now, a particular error pops up in the terminal: app/idea.ts(3,8): error TS2304: Cannot find name 'date'. The con ...

How can I encode and decode a base64 string using AngularJS1 and TypeScript?

I am currently working with Angular1 using TypeScript and I have a question that needs some clarification. Within the environment that I am operating in, is there a method available to encode and decode a string in base64? Despite conducting extensive re ...

Collaborate and reuse Typescript code across various Node projects

Imagine we have a project structured like this: webapps ProjectA SomeClass.ts Package.json ProjectB SomeClass.ts Package.json Common LoggingClass.ts Package.json The Common "LoggingClass" needs to import a module from NPM. Let's say that ...

The Problem of Unspecified Return Type in Vue 3 Functions Using Typescript

Here is the code snippet I am working with: <template> <div> <ul v-if="list.length !== 0"> {{ list }} </ul> </div> </template> < ...

What is the best way to initiate a refetch when the need arises to follow a different path?

I have encountered a situation where I am able to pass the refetch function on a child component. However, an issue arises when transitioning to another page and implementing redux. This is particularly problematic when attempting to open a dialog for a ne ...

"Enhance your user experience with an Angular material list featuring the ability

I am looking to enhance an angular selection list by allowing users to add new items without losing their current selections. My usual approach involves adding a method in the component to update the data list and then recreating the MatSelectionList bound ...

How to modify the appearance of the md-tab-header component in Angular2

Currently, I am working on a project that was passed down to me by a former colleague. It is a gradual process of discovery as I try to address and resolve any issues it may have. One particular element in the project is a md-tab-header with a .mat-tab-he ...

What are the steps to integrate <br> in a JavaScript code?

I have recently started learning about web development and I'm facing a challenge with this implementation. var obj = [[{ name: "John", age: 30, city: "New York"}, { name: "Ken", age: 35, city: "New Orleans"}]]; ...

Using TypeScript with React and Material-UI: Issue with undefined theme in createStyles()

Currently, I am delving into React with TypeScript and utilizing the Material UI framework for the frontend. In my quest to activate media queries, an error has crossed my path: Uncaught TypeError: Cannot read property 'up' of undefined ...

Learn how to merge two objects and return the resulting object using TypeScript functions within the Palantir platform

I am looking to generate a pivot table by combining data from two objects using TypeScript functions. My plan is to first join the two objects, create a unified object, and then perform groupBy operations along with aggregate functions like sum and min on ...

Obtaining Data from a Database Using Angular

I have developed a front-end website using Angular as my framework, integrated with node.js. To interact with the database, I created a "server.ts" file and connected it successfully to my DB. Now, my goal is to fetch data from the database and display i ...

Issue: ng test encountered a StaticInjectorError related to FormBuilder

I recently started using ng test to run tests on my Angular application. I utilized the Angular-Cli tool to create modules and components, and the .spec.ts files were generated automatically. During one of the tests, I encountered the following error: ...