How to customize the appearance of a single cell in ng-grid with Angular 6

I'm currently attempting to access a single cell on an ag-grid using Angular 6. The HTML structure is as follows:

 <ag-grid-angular 
 style="width: 700px; height: 200px;" 
 class="ag-theme-balham"
 [rowData]="rowData" 
 [columnDefs]="columnDefs"
 rowSelection="single"
 colDef.editable=true
 (gridReady)="onGridReady($event)"
 >
 </ag-grid-angular>

The TypeScript class code is:

 ngOnInit() {

  }
  onGridReady(params){
    this.gridApi = params.api
    this.columnApi =  params.columnApi
  }
  updateRow(){
    var rowNode = this.gridApi.getRowNode(0);
    rowNode.setData(
      { make: 'changed', model: 'changed', price: 10 },
    )

  }
  updateCell(){
    var rowNode = this.gridApi.getRowNode(0);
    this.gridApi.getRowNode(0)
    rowNode.setDataValue("model","mobile")
  }
  addColumn(){
    var rowNode = this.gridApi.getRowNode(0);
    var x = parseInt(rowNode.data.price)
    var rowNode = this.gridApi.getRowNode(1);
    var y = parseInt(rowNode.data.price)
    var rowNode = this.gridApi.getRowNode(2);
    var z = parseInt(rowNode.data.price)
   console.log("price", x ,y, z)
   this.total = x+y+z


  }

  columnDefs = [
   // use this for the whole column   {headerName: 'Make', field: 'make',cellStyle: {  'border-bottom': '3px double'} },
   {headerName: 'Make', field: 'make',cellStyle: {
    // you can use either came case or dashes, the grid converts to whats needed
    backgroundColor: '#aaffaa' // light green
}},
    {headerName: 'Model', field: 'model'},
    {headerName: 'Price', field: 'price', editable: true},
    {headerName: '', field: '', editable: true}
];

rowData = [
    { make: 'Toyota', model: 'Celica', price: 35000, cellClass: "col-1" },
    { make: 'Ford', model: 'Mondeo', price: 32000 },
    { make: 'Porsche', model: 'Boxter', price: 72000 }
];

I have successfully targeted a column using cellstyle but I am now trying to add borders to each individual cell. Is there a way to target a specific cell similar to how I've done with:

var rowNode = this.gridApi.getRowNode(1);

and apply a style to the rowNode or is there another method to accomplish this?

Answer №1

When referencing the guide, it is stated that you have the ability to add styles to a single cell by using the provided parameter or utilizing cellClassRules.

  columnDefs = [
   // apply to entire column   {headerName: 'Make', field: 'make',cellStyle: {  'border-bottom': '3px double'} },
   {headerName: 'Make', field: 'make',cellStyle:function(params) {
    // usage of camel case or dashes are accepted, as the grid will adjust accordingly
//Customize an individual cell with one class and the entire column with another
return (params.colDef===1?'my-class-1':'my-class-2');
}},
    {headerName: 'Model', field: 'model'},
    {headerName: 'Price', field: 'price', editable: true},
    {headerName: '', field: '', editable: true}
];

Answer №2

After experimenting, I discovered that by utilizing the following code snippet:

{headerName: 'Model', field: 'model', cellClass:function(params) {
  console.log("params ", params);
  console.log("params value", params.value);
  console.log("params colDef", params.node.rowIndex);
  // you can use either camel case or dashes, the grid converts to what's needed
  // Style the specific cell with one class and the whole column with another
  return (params.node.rowIndex===1?'my-class-1':'my-class-2')}},

It became clear that the CSS styling should be placed in styles.css as opposed to the components css file in order for it to work effectively. This way, I was able to target individual cells precisely.

Answer №3

To add a border to each cell individually, you can apply a basic CSS solution like this:

.ag-cell {
    border: solid 1px blue !important;
}

If you prefer to only add the border to selected cells, then you can use the following CSS:

 .ag-cell-focus {
    border: solid 1px blue !important;
}

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

CSS not working when attempting to override Angular (13) Material file

In my Angular (13) application with Material, I have developed a CSS file specifically for overriding certain Material styles. This CSS file is imported into the styles.scss file as the last line. However, despite importing the external file, the CSS def ...

Tips for preventing breaks in typography

I have implemented a material ui Typography component, but it's causing a line break even though there is enough space. Here is the code snippet: <Box flexDirection="row"> <Typography> Gender: <RadioGroup row ...

How to ensure an image or ad stays in a fixed position on the screen

Is there a way to keep a part of the screen fixed, regardless of what other elements are in that space? Do I need to merge code with the HTML code of the ad or image to achieve this? ...

Creating a Background Cutout Effect with CSS Using Elements instead of Images

I'm working on creating a unique visual effect that simulates a "window" where the div placed above all other elements acts like a window, allowing the background color to show through. Take a look at this example for reference. My goal is to make th ...

Place picture in the bottom right corner of the div without using absolute positioning

I am looking for a way to position an image in the lower right corner of a div, but I want to avoid using absolute positioning. The reason for this is that when I use absolute positioning, the text wrap is affected due to the float right applied to the ima ...

Dependency injection in Angular is a powerful design pattern that

I recently completed an Angular 2 website, but I've run into some issues that I cannot seem to debug. I've spent the past week trying to find a solution but have had no luck. Below, I've included some snippets of my code. Feel free to ask fo ...

Cannot close the Bootstrap dropdown after selecting an option

I am encountering an issue with a dropdown list that has a select feature using Bootstrap 3.4.1. The problem is that the dropdown remains open after selection and does not close unless I click outside of the list. Here is the HTML code: <div id="c ...

The placement of footers remains consistent

Struggling with the placement of my footer on a website I'm designing. The footer should be at the bottom of the page with a set percentage margin, but it's not working as expected. Here is a link to My website Reviewing the footer CSS: .foote ...

Angular's "change" output automatically triggers input events to propagate implicitly

Encountering an unusual behavior, I have a basic component that wraps a text input: export class InplaceInputComponent { @Input() value: string; @Output() change = new EventEmitter<string>(); editedValue: string; inputChange(event: any) { ...

Dynamic Display Picture Banner - Adjustable Layout

My home page features a full screen image header that I'm working to make responsive. The width of the image is being cut off on various screen sizes. I've attempted adjusting the height and width settings, but I want the image itself to resize ...

Selectize-dropdown menu shines brightly as it opens upwards

In my sleek dashboard design, I have implemented a few dropdown menus using selectizeInput. These menus are currently positioned at the bottom of the page, but I want them to open in an upward direction instead of downward. While I found a workaround for ...

Do changes in Input fields reflect in the parent component?

I was under the impression that I could share data with child components using @Input() directive and communicate data back to the parent component with @Output() along with the appropriate emit. However, I recently discovered that modifications made to th ...

Conceal User Interface Angular Tabulation

I'm working on setting up UI Bootstrap tabs, and for reference you can find the plunker here. My goal is to hide the tabs since I will be utilizing a button to switch between them. Despite adding the following CSS class to my file, the styling is not ...

Node-Sass does not support compiling or transpiling the "@use" rule for modules

I am attempting to transpile/compile the code snippet provided on [https://sass-lang.com/documentation/at-rules/use#load-paths] // src/_corners.scss $radius: 3px; @mixin rounded { border-radius: $radius; } // style.scss @use "src/corners"; .button { ...

Exploring Alternative Methods to Navigate Through Elements Using JQuery UI Autocomplete

After testing two different approaches: <div id = "team-search-container"> <label for="team-search" class = "text-center"> <input type = "text" id = "team-search"> </label> </div> With the following code sni ...

AngularTS regex that enforces the use of a decimal point in numbers

I am working on a requirement where the input should only accept decimal characters, negative or positive. I need to use regex to make the decimal point mandatory, however it is currently allowing negative whole numbers which is not the desired behavior. I ...

Having trouble getting the carousel slider to function properly on meteor?

I am currently working on my portfolio page using Meteor, and I want to include a carousel slider gallery. The problem I'm facing is that while the first gallery works fine, the second one does not load correctly and just displays a list of pictures. ...

Obtaining a phone number from a contact in Nativescript Angular: A step-by-step guide

Upon executing the following code: let desiredFields = ['display_name','phone','thumbnail','email','organization']; console.log('Loading contacts...'); let timer = new Date().getTime(); Contact ...

In Typescript, is it possible to utilize the value from a particular key in a type declaration as the type for another key within the same declaration?

Consider a scenario where I am designing a Variable type that includes a key named type with a value of type string. Is there a method to extract the value from the type key and utilize it as the type for another key within the declaration, without resorti ...

Error message: The iOS system was unable to open the backing store due to a QuotaExceeded

Our Web-Application is quite large and is built on Angular with PWA capabilities enabled. Everything runs smoothly, including the offline mode functionality. We utilize PDFTron-Webviewer, which is cached by the Service-Worker to ensure availability in Off ...