Enhancing the appearance of individual cells in jQuery DataTables

While working with jQuery DataTables, I have successfully managed to access row data and apply styling to the entire row.

Below is the code snippet used to initialize the datatable:

 var $dataTable = $('#example1').DataTable({
   "data": data,
   "dataType": "json",
   "iDisplayLength": 25,
   "order": [[6, "desc"]],
   "scrollY": 550,
   "scrollX": true,
   "bDestroy": true,
   "stateSave": true,
   // this part of the code styles the row
   "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull)
   {
     if (aData[12] == "Y"){$('td', nRow).css('background-color', '#EE6363');}
     if (aData[9] == "Y"){$('td', nRow).css('font-weight', 'bold');}
   }
 });

The first if statement in the code above checks if the data in the 12th column named REJECTED is "Y", resulting in the entire row turning red.

The second if statement examines whether the data in the 9th column labeled URGENT is "Y", making the text bold for the entire row.

I am now looking to modify my code to make only the cell in that specific column red, instead of the whole row, particularly for the first row.

How can I update the existing code to achieve this desired effect?

Answer №1

I haven't had a chance to test this yet...

Here's a possible solution to try out:

if (data[12] == "Y"){$('td', row)[12].css('background-color', '#EE6363');}

You could also experiment with this alternative approach:

if (data[12] == "Y"){$('td', row).parent().children()[12].css('background-color', '#EE6363');}

If using zero-based indexing:

if (data[12] == "Y"){$('td', row)[11].css('background-color', '#EE6363');}

Or you might consider:

if (data[12] == "Y"){$('td', row).parent().children()[11].css('background-color', '#EE6363');}

Answer №2

This snippet of code will specifically modify the designated cell:

 if (data[12] == "Y"){$('td', row).eq(1).css('background-color', '#BF5FFF');}

By incorporating eq(1), I am able to pinpoint and enhance the appearance of the second cell within the table. This method proved successful for me.

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

Upon invoking the useEffect function, the default value can be seamlessly established within the input field of the antd library

I have a function called loadProfile that I need to execute within the useEffect hook. When this function is triggered, I want the name Mario to be automatically displayed in the input field labeled name. How can I set this default value using the antd lib ...

Using an HTML5 image icon as an input placeholder

Is there a way to incorporate an image icon into an input placeholder and have it disappear when the user starts typing, across all browsers? In trying to achieve this, I successfully implemented a solution for webkit (Safari+Chrome) using ::-webkit-input ...

Is there a way to specifically select the input field that the user has typed into using a selector?

Is it possible to use javascript/jquery to specifically target the input field that a user has just typed in? ...

Can you guide me on implementing CSS Houdini in Next.js?

Exploring the world of CSS Houdini in my Next.js project has been quite an adventure. After successfully installing the necessary CSS Houdini package and css-paint-polyfill using yarn, I decided to follow the webpack guidelines provided at . Below is a sn ...

Is the component experiencing issues with its style functionality?

I am attempting to add CSS styles to app.component.ts using the :host property, but I am not seeing the desired results. The CSS is being applied, but not correctly. .ts export class AppComponent { title = "Default Title" data = 'This is defaul ...

The message vanishes upon refreshing the page

I've developed a socket.io web app. When I click on the button to send a message, the message appears briefly but disappears when the page refreshes unexpectedly. How can I prevent this random refreshing and ensure that socket.io saves my messages? B ...

"Rest assured that Coldfusion 9 users may encounter a JSON parsing

I have a function that performs the following operation: <cffunction name="foo" access="remote" returnformat="JSON" > <cfreturn ["000", "001", "002"]> <cffunction> When I retrieve this array using jQuery.ajax, I noticed in Firebu ...

I'm experimenting with crafting a new color scheme using MUI, which will dynamically alter the background color of my card based on the API

I am attempting to create a function that will change the colors based on the type of Pokemon. However, I'm not sure how to go about it. Any suggestions or ideas!? Check out where I'm brainstorming this logic [This is where the color palette sh ...

Transforming API Response into a structured format to showcase a well-organized list

When I make an API call for a list of properties, the data comes back unorganized. Here is how the data from the API looks when stored in vuex: posts:[ { id: 1; title: "Place", acf: { address: { state: "Arkansas", ...

I am sending JSON as form data using JavaScript and then accessing it in PHP. During this process, the quotation marks are being replaced with their HTML entity equivalent

After converting an array into JSON, I send it as a value of a text box using the post method. In a PHP file, when trying to print it out, it displays some encoding issues. var json_arr = JSON.stringify(info); The generated JSON looks like this: {"1":"1 ...

What is the best way to retrieve a list of customers within a specified date range?

How can I retrieve a list of customers within a specified date range? My frontend react app includes starting and ending date pickers, but I'm unsure how to query the data using mongoose in my express app. Below you'll find my mongoose model and ...

The Ultimate Guide to Setting up SVG's Viewbox, Width, and Height for Scalability and Responsiveness

As a beginner in SVG design, I find it challenging to scale, zoom in/out with the container and ensure responsiveness across different devices. I wonder if it's possible to create an SVG that adapts to all device sizes and effectively handles browsin ...

Updating Cart Array in Angular 4 when Adding a New Item

I'm currently using angular 4 and I have encountered an issue in the code where adding a new product to the cart overwrites the existing item instead of appending it to the array. Here is a screenshot illustrating the problem cart.service.ts export ...

The color of the left arrow on the tooltip cannot be altered

Currently, I am using a tooltip that is positioned on the right side: <i class="fas fa-question-circle text-blue" data-toggle="tooltip" data-placement="right" title="hello" ></i> I have attempted to modify the color of the too ...

DreamFactory's REST API POST request for rest/user/session consistently encounters errors in Internet Explorer 9

In Firefox, Chrome, and Safari, the initial POST rest/user/session request works perfectly fine. However, in Internet Explorer 9, it consistently returns an error. When the dataType is specified as "json," IE9 encounters a 'no transport' error w ...

The issue with jquery's marginTop functionality appears to be malfunctioning

Is there an easy way to vertically center text of unknown height within a div of known height? I've experimented with the css display: table-cell option without success, so I'm currently using jQuery to detect the height and apply a top margin if ...

Tips for avoiding the 'ResizeObserver loop finished with unhandled notifications.' error when using React with mui/material/Table

This is our current code snippet: import Table from '@mui/material/Table'; import TableBody from '@mui/material/TableBody'; import TableCell from '@mui/material/TableCell'; import TableContainer from '@mui/material/TableC ...

Converting the AppDelegate.m file to AppDelegate.mm and updating the AppDelegate.h file during the transition from React Native 0.66.4 to 0.71.3

Original code in AppDelegate.h: #import <React/RCTBridgeDelegate.h> #import <UIKit/UIKit.h> #import <UserNotifications/UserNotifications.h> @interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificat ...

Merge the variables extracted from an array of objects

I need to extract specific data from an array of objects and perform a calculation. For example, the provided data is as follows: const item = [{ "act": "Q", "line": 1, &quo ...

What is causing this code to not produce the expected result of 8.675?

Recently, I encountered a challenge on freecodecamp I managed to write the code successfully, but unfortunately, my browser crashed and I lost all the progress. When I attempted to rewrite the code, it was returning an incorrect answer (a value of 2). I& ...