Emphasize a Row Based on a Certain Criteria

One of the challenges I am facing is how to emphasize a specific row in a table based on certain conditions. Currently, I am utilizing Jqxgrid and have made some modifications in the front-end to achieve the highlighting effect:

TypeScript:

carsDataAgain = [
    {
      year: '1967',
      make: 'Pontiac',
      model: 'GTO',
      id: '1002',
    },
];

getCarsData(val: string) {
    if (this.carsDataAgain.find((i) => i.model === val.model)) {
      return 'Matched';
    } else {
      return 'Unmatched';
    }
}

HTML:

<jqxGrid
  [theme]="'material'"
  [width]="getWidth()"
  [source]="hello"
  [columns]="columns"
  [pageable]="true"
  [autoheight]="true"
  [sortable]="true"
  [selectionmode]="'singlerow'"
  [altrows]="true"
  [enabletooltips]="true"
  [editable]="true"
  [filterable]="'true'"
  [columngroups]="columngroups"
  [keyboardnavigation]="'false'"
  enablekeyboarddelete="false"
  [ngClass]="{
    primary: getCarsData(hello) === 'Matched',
    secondary: getCarsData(hello) === 'Unmatched'
  }"
>
</jqxGrid>

In the front-end, I utilized the following approach using ngClass:

[ngClass]="{
    'primary': getCarsData(hello) === 'Matched',
    'secondary': getCarsData(hello) === 'Unmatched'
}"

Essentially, I passed the data source into the method and checked the returned value to determine which row should be highlighted. In this case, I am checking for the existence of a specific model ('GTO') in the carsDataAgain array to trigger the highlighting effect. If a matching model is found, the first row should be highlighted. Here's a demo that illustrates my current progress: Highlight A Row

I am curious if there are any alternative methods to implement this feature or if there are any aspects that I may have overlooked?

Answer №1

Two key issues were identified in your solution:

  1. The class was mistakenly added to the entire table instead of a specific row
  2. Furthermore, there was a recurring call to getCarsData due to the angular render cycle, as evidenced by the console log.

To address this, I made modifications in your StackBlitz project which can be found here:

https://stackblitz.com/edit/jqxgrid-editable-njsbfh

The solution involved creating an arrow function and assigning it to the "cellclassname" property when defining the grid columns.

  cellclass = (row, column, value, rowData) => {

    if (this.carsDataAgain.find((i) => i.model === rowData.model)) {
      return 'green';
    }
  };
  ...
  columns: any[] = [
  {
      text: 'Year',
      datafield: 'year',
      width: 40,
      cellclassname: this.cellclass,
    },
    { text: 'Id', datafield: 'id', width: 40, cellclassname: this.cellclass },
    {
      text: 'Model',
      datafield: 'model',
      width: 100,
      cellclassname: this.cellclass,
    },
    {
      text: 'Make',
      datafield: 'make',
      width: 100,
      cellclassname: this.cellclass,
    },
  ];

In addition, for jqxgrid to recognize the CSS classes defined in the app component, it is necessary to include:

  encapsulation: ViewEncapsulation.None,

This will make the styles from app.component.css global. Alternatively, you could define the classes in a separate global CSS file without needing to change the encapsulation setting.

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 the WebSocket interface be substituted with WebSocket itself?

I'm currently in the process of setting up a WebSocket.Server using ws, Express 4, NodeJS, and TypeScript by following a guide. However, I've encountered an issue with the provided code not working as expected from the tutorial found at . In ord ...

Implementing chance.js in an Angular 4.x Component's ngOnInit() Method

I just started using angular 4.x and I'm trying to load change.js in the ngOnInit() function. I've attempted a few things but nothing seems to be working. Here is the code snippet: This is the code I have so far: import { Component, OnInit, In ...

Avoiding Re-renders in an Angular2 Countdown Component

I am facing an issue with my Angular2 master component that includes multiple child components and a standalone countdown component called "clock". The countdown component updates its label every second, causing unnecessary re-rendering of the master compo ...

Retrieving information from a Kendo grid cell

I am working on a web application that utilizes Kendo Grid. How can I retrieve the values of the "Ticket No" from the selected checkboxes? https://i.stack.imgur.com/aPOje.png This is my code: var grid = $("#poGrid").data("kendoGrid"); grid.items().filte ...

What is the best method for saving information from a service to a class or interface in Angular 2?

I am currently in the process of developing a new web application and I am fairly inexperienced with Angular2. I am encountering an issue with one of my components that acts as a form for users to update their data. The problem lies in accessing specific ...

Why does Typeorm consistently encounter insertion failures when attempting to insert into two tables, and why does Math.random() continuously return the same number?

Here is the code snippet I am working with: import { getRepository } from "typeorm"; import { NextFunction, Request, Response } from "express"; import { Users } from "../entity/Users"; import { Verify } from "../entity/Ve ...

Utilize Angular's effect() function to reset a form when a specific signal becomes true

Is my approach to using signals correct in this scenario? I have a form that needs to reset when the success signal is true. I implemented this with an effect, but the Angular documentation is not very clear on this topic yet (refer to here). **Do you bel ...

I've added a check, so why is TypeScript still complaining about the possibility of my property being undefined?

const handleLinkClick = (index: number) => () => { const hasUrl = !!searchItems[index]?.url; if (hasUrl) { navigateToLink(searchItems[index]?.url); } else { setItemSelected(index); } }; However, the issue I encountered is: (property) ...

Sending data from app.config file to component

I have a function in my app.config file that retrieves the current environment using window.location.hostname. I am looking for a way to pass this value to another component within my application. How can I accomplish this? Here is an excerpt from my app. ...

Guide on sending information through a POST request from Angular to a .Net API

My attempt to make a simple request is failing because the value(property) in API is null. Any suggestions on how to troubleshoot this? C# [Route("getpagefields")] [AcceptVerbs(WebRequestMethods.Http.Post)] public IHttpActionResult GetPageFields ...

Angular2 components or module npm packages offer a wide range of functionality and

Looking to develop an npm package containing my Angular2 components. Should I create an angular module and export it, or is exporting just the components sufficient? What advantages does each method offer? Providing examples would be greatly appreciated ...

Is it feasible to deliver push notifications from one application on a single device to the identical application on a different device using Ionic 4?

Is it possible to send push notifications from one app on a device to the same app on another device using Firebase Cloud Messaging in Ionic 4? I'm hoping to be able to create a text and a simple button in one app, and automatically send FCM push not ...

Guidelines for securing login access where the "IsApproved" field must be true before authorization

During the account registration process, I initially set the default value to false for the field IsApproved. I need to create security rules that allow login only for users with IsApproved:true, and redirect those with IsApproved:false to the accessdenied ...

Exploring Angular Unit Testing: A Beginner's Guide to Running a Simple Test

I'm diving into the world of angular unit testing and looking to set up my first successful test. Here's what I've come up with: import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { AppComponent } fro ...

The error message "Property 'pipe' is not found on 'ReadableStream<Uint8Array>'" indicates that the pipe method cannot be used on the given type

Within a function resembling Express.js in Next.js, I am working on fetching a CSV file with a URL that ends in .csv. Using the csv-parser library to stream the data without persisting the file and transform it into an array. Here is an excerpt of the code ...

Exploring for JSON keys to find corresponding objects in an array and adding them to the table

I'm currently working on a project where I need to extract specific objects from a JSON based on an array and then display this data in a table. Here's how my situation looks: playerIDs: number[] = [ 1000, 1002, 1004 ] The JSON data that I am t ...

Drag and drop functionality in Angular 2 using TypeScript

Has anyone experimented with the drag and drop functionality using Angular2 RC with TypeScript? Thanks, Sanket ...

Transferring information between pages within Next.js 13

I am currently working on a form that is meant to generate a Card using the information inputted into the form, which will then be displayed. While I have successfully implemented the printing feature, I am having difficulty transferring the form data to t ...

The presence of 'touched' within Angular validation is causing a delay in method execution

Upon utilizing this validation method, it became apparent: <label>Password</label> <input type="password" formControlName="password" class="form-control" [ngClass]="{ 'is-invalid': f.password.touc ...

The "number" input type is functioning correctly on Chrome, but on Firefox, it is accepting characters other than numbers

While developing in Angular, I encountered a challenge where I needed to create an input that only accepts numeric characters. Initially, I tried using type="number" which worked perfectly in Google Chrome but surprisingly allowed non-numeric characters ...