Adding a class to all tds in a row

Tags: #<Tag:0x00007f8b2859cf08>

I’m trying to highlight certain rows by adding a className to all the td in it.

...
var table = new Handsontable(container, {
  ...
  cells: function(row, col) {
    let cellProperties = {};
    
    if(row == 1) {
      // Highlight the second row
      cellProperties.className = 'spotlight'
    }
    
    return cellProperties;
  },
  ...
});
...

This does work but it removes some other configurations/settings that I have when setting up the columns like text alignment. Please take a look at http://jsfiddle.net/pmoLeq7c/1/ to see what I mean. The cells that were supposed to be aligned center or right are now aligned left (default alignment).

I managed to fix it by using Jquery and simply adding a class. Not sure if this is the best way to do it, I’d rather do the manipulation from the library itself because using Jquery feels hacky.

This doesn’t work well if I have a frozen column because those do not get the additional classname. http://jsfiddle.net/pmoLeq7c/4/

Another solution using Jquery to also highlight the frozen columns (still feels very hacky and I don’t like it):

http://jsfiddle.net/arb07L2h/

Great investigation @patrick.gregorio
Can I help you with anything else or you got it all covered?

@aleksandra_budnik I think I still need help, I prefer adding the class name via Handsontable not through Jquery.

Currently my code looks like this (this doesn’t even highlight the row even though this same logic works on the fiddle I linked to earlier but I’ll fix that myself):

hot.updateSettings({
  cells: function(row, col) {
    let cellProperties = {};

    if(cells[row] && cells[row][col] !== undefined && cells[row][col] != 'inherit') {
      cellProperties.renderer = experimentsRenderer;
    } else if(transfers[row]) {
      let cell = $(hot.getCell(row, col));
      cell.addClass('animal-transferred');

      let frzTR = $('.handsontable.ht_clone_left').find('table tbody tr');
      $(frzTR[row]).find('td').addClass('animal-transferred');
    }

    return cellProperties;
  }
});

Basically what this does is if the if condition is true I will use a custom made renderer called experimentsRenderer, otherwise if my else if condition is true, I simply want to add a class name to all the td elements in that row. If both of those are false then I don’t want to do anything, just render the cell as it was originally intended to be displayed.

Another option I was considering was creating another custom renderer that would do the adding of the class name because it gives me access to the td but my table contains so many different data types. All of the examples I’ve seen that define a custom renderer does this:

Handsontable.renderers.TextRenderer.apply(this, arguments);

This won’t do because my first column is a checkbox, most are text fields, some are date fields, some are numeric. I tried this and it turned my checkbox fields into a text field that displays true or false.

Okay so my current solution really doesn’t work that well… most noticeably if I enable columnSorting.

Update

To be honest I’d rather go from the original fiddle that doesn’t involve Jquery. I think that’s a better solution… I just need it to add a class name instead of replacing them.

Have you tried using properties in cells? If you go to http://jsfiddle.net/s3q698aL/ and change type numeric to text styles are still applied and dedicated alignment works well.

@aleksandra_budnik that is promising and it seems to work at first glace however please take a look at a slightly modified version of your fiddle. In here, I did change the data type from numeric to text, however I want this column to be centered so I added a className: 'htCenter' to the definition.

If I don’t set the background color of the cells (comment out the cellProp.className = getData[row].className[prop]; line), the Year column does get centered because it has the htCenter class. However if I uncomment that line which would apply the background color to the cells, the column becomes left-aligned.

There has got to be a way to just add a class and not replace the already existing classes.

What do you think about this example http://jsfiddle.net/5fz6x3hb/ (use col === 1 in cells + attach the variable as a string)

1 Like

Fine I’ll accept this solution for now but for a supposedly flexible and super customizable library, there ought to be a simpler way to just add a class. This just feels like a work-around to a real issue.

Thanks for the help anyways, I appreciate all the solutions you’ve come up with @aleksandra_budnik !

1 Like