QuerySelector of LWC Tree Grid Row

javascriptlightning-web-components

I'd like to change the background color of a specific row in a tree grid, but how can I select the row?

My current attempt fails as there is never a value assigned to row.

renderedCallback() {
  const row = this.template.querySelector('[data-row-key-value="ROW_KEY"]');
  if (row) {
    console.log('row found');
    row.style.backgroundColor = 'yellow';
  }
}

Here is the template

<template>
  <div class="slds-p-around_medium">
    <template if:true={gridData}>
      <lightning-tree-grid
        columns={gridColumns}
        data={gridData}
        key-field="Id"
        is-loading={isLoading}
        hide-checkbox-column=true
        ontoggle={handleRowToggle}
        expanded-rows={currentExpanded}
        show-row-number-column=true
      >
      </lightning-tree-grid>
    </template>
  </div>
</template>

Best Answer

Shadow DOM is a standard that encapsulates the internal document object model (DOM) structure of a web component. Encapsulating the DOM gives developers the ability to share a component and protect the component from being manipulated by arbitrary HTML, CSS, and JavaScript. The internal DOM structure is called the shadow tree. The shadow tree affects how you work with CSS, events, and the DOM.

Component's DOM elements & CSS are isolated to its own, meaning you can't modify it from outside. So what you're trying to do is security feature & designed this way.

Recommended

So the straightforward & legit answer to your question is you can't do this & should roll your own tree-grid component & you can styling it the way you want. You can utilize the template provided here & add functionality to it.

SUBJECT TO CHANGE

Salesforce doesn't recommend doing it this way & is subject to change. This may fail in future SF release if they modify the internal which you targetted from the static resource. So use it on YOUR OWN RISK. The other way to style these component would be through a static resource which applies to the document level. You can write the same style in .css file upload it as static resource & load it in your lwc.

customTreeGrid.css

tr[data-row-key-value="ROW_KEY"]{
   background-color: yellow;
}

customLwc.html

<template>
  <div class="slds-p-around_medium">
    <template if:true={gridData}>
      <lightning-tree-grid
        columns={gridColumns}
        data={gridData}
        key-field="Id"
        is-loading={isLoading}
        hide-checkbox-column=true
        ontoggle={handleRowToggle}
        expanded-rows={currentExpanded}
        show-row-number-column=true
      >
      </lightning-tree-grid>
    </template>
  </div>
</template>

customLwc.js

import { loadStyle } from 'lightning/platformLoader';
import  CUSTOM_STYLES from 'lightning/resourceUrl/customTreeGrid';
export default class CustomLwc extends LightninEl....{
   isLoaded = !1;
   async renderedCallback(){
      if(!this.isLoaded){
       await loadStyle(this, CUSTOM_STYLES);
       this.isLoaded = !0;
      }
   }
 }
Related Topic