Author Topic: Header cell color  (Read 8552 times)

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 371
    • View Profile
Header cell color
« on: October 01, 2025, 07:34:08 pm »
Would it be possible to set a specific background-color on a column header cell if a column has a filter applied?
Purpose would be to quickly see what columns that has filter applied.
« Last Edit: October 01, 2025, 07:36:41 pm by queensgambit9 »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6552
    • View Profile
Re: Header cell color
« Reply #1 on: October 02, 2025, 11:57:43 am »
Use columnTemplate and the filter event to apply conditional styles to header cells based on active filters. The example below highlights a header in red when its filter has a value.

styleHead Getter: Dynamically calculates the style. It checks if a filter rule with a value exists and returns a background color.

filter Event: After filtering, it refreshes each header cell individually. Using refreshHeaderCell prevents the loss of focus in the filter input fields, which a full refreshHeader would cause.

Code: [Select]
columnTemplate: {
get styleHead(){
let val = this.filter?.crules?.[0].value;
if(val != null && val.length ){
return {
'background-color': 'red'
}
}
}
},
filter(){
this.colModel.forEach((col, ci)=>{
this.refreshHeaderCell({
rowIndx: 0,
colIndx: ci
})
})
},

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 371
    • View Profile
Re: Header cell color
« Reply #2 on: October 02, 2025, 09:15:12 pm »
Thanks.
Seems when then filter is removed it keeps the modified color and do not resume to "standard" color.

using:
Code: [Select]
columnTemplate: { width: 115, dataType: 'string', filter: { crules: [{ condition: 'contain' }], selectGridObj: remoteOptions('a.table') },
get styleHead(){
let val = this.filter?.crules?.[0].value;
if(val != null && val.length ){
return {
'color': 'blue'
}

}
},
filter(){
this.colModel.forEach((col, ci)=>{
this.refreshHeaderCell({
rowIndx: 0,
colIndx: ci
})
})
},
« Last Edit: October 02, 2025, 11:20:05 pm by queensgambit9 »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6552
    • View Profile
Re: Header cell color
« Reply #3 on: October 03, 2025, 06:28:26 pm »
Yes, sometimes it was failing as the filter event is not fired if the filtered data in the grid is not changed.

I've changed it to beforeFilter event and updated this example:

https://paramquery.com/pro/demos/filter_initial

Please let me know if you still face any issues.
« Last Edit: October 03, 2025, 06:52:51 pm by paramvir »

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 371
    • View Profile
Re: Header cell color
« Reply #4 on: October 06, 2025, 02:20:34 pm »
On export, I get:

Code: [Select]
pqgrid.min.js:9 Uncaught invalid color: red

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6552
    • View Profile
Re: Header cell color
« Reply #5 on: October 06, 2025, 02:39:11 pm »
Use hexa notation "#ff0000" instead of color names: red

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 371
    • View Profile
Re: Header cell color
« Reply #6 on: October 06, 2025, 05:40:38 pm »
Works. Other issue seems to be that the sorting of a column is lost when the code is executed.
« Last Edit: October 06, 2025, 05:46:32 pm by queensgambit9 »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6552
    • View Profile
Re: Header cell color
« Reply #7 on: October 07, 2025, 10:44:13 am »
Adding this._trigger('refreshHeader') fixed it

https://paramquery.com/pro/demos/filter_initial

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 371
    • View Profile
Re: Header cell color
« Reply #8 on: October 09, 2025, 01:43:24 pm »
Thanks. How do you remove formatting when exporting to excel? (using paramquery-10.1.0)?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6552
    • View Profile
Re: Header cell color
« Reply #9 on: October 09, 2025, 07:47:04 pm »
There are 2 ways:

1) Use eachCellHead callback parameter in https://paramquery.com/pro/api#method-exportData

var data = grid.exportData({ format: 'xlsx',  eachCellHead: function(cell){
  cell.css = ""; //clear the css for each header cell.
}});

2) use a global variable skipFilterHeaderStyle, toggle it to true before exporting data
and in get styleHead() don't return anything if skipFilterHeaderStyle is set to true.
« Last Edit: October 09, 2025, 07:55:48 pm by paramvir »

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 371
    • View Profile
Re: Header cell color
« Reply #10 on: October 13, 2025, 12:54:26 am »
Thanks.
« Last Edit: October 13, 2025, 01:56:50 am by queensgambit9 »

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 371
    • View Profile
Re: Header cell color
« Reply #11 on: October 17, 2025, 05:33:18 pm »
Seems when the filter condition is "In between" and the "to" field is set, the color is not applied to the column header.

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 371
    • View Profile
Re: Header cell color
« Reply #12 on: October 20, 2025, 01:53:45 pm »
Is there an updated code available to address this issue?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6552
    • View Profile
Re: Header cell color
« Reply #13 on: October 20, 2025, 06:58:59 pm »
Yes please use value2 to check 2nd value of the between condition.

Code: [Select]
get styleHead() {
    let crule = this.filter?.crules?.[0],
        val = crule?.value,
        val2 = crule?.value2; //2nd value for between condition.

    if ((val != null && (!$.isArray(val) || val.length)) || (val2 != null)) {
        return {
            'background-color': 'lightyellow'
        }
    }
}