Rimokas
Rimokas

Reputation: 15

How to change w2ui grid column header background colors

After some searches I found, that I can change w2grid columns headers background colors. I wanna to do a table with work/rest days and with additional information. It's nice to get in header already to know which day is workday, which - rest

Here some code:

w2ui[ tab_grd[ 0 ] ].on( 'load', function( event ) 
{
  event.onComplete = function() 
  {
     prep_hdr()
  }
})

preparing headers with function:

days = rsp.days
if ( Object.keys( days ).length > 0 )
{      
  for ( var u in days )
    {
      var col_id = 'd' + days[ u ].day
      var dat_tp = days[ u ].type
      var clr1   = 'white'
      var clr2   = ( dat_tp == 'W' ? '#66d9ff'  : ( dat_tp == 'R' ? '#80ffaa' : '#ff9980' ) )
      var col    = findElement( w2ui[ tab_grd[ 0 ] ].columns, "field", col_id )
      if ( col != -1 )
      {
        var idf = 'td[col="' + col + '"].w2ui-head'; 
        $( idf ).css( { background: 'linear-gradient( ' + clr1 +',' + clr2 + ' )' } )
      }
    }
  }

It's working.

But columns of days are 31 + total column + some other columns and they not appears all at the same time. If to scroll horizontally to left/right - my colorization of col headers disappears.

As I'm seeing in w2grid.js source - scroll (event) function is repainting grid, that's why my colors disappear.

How can I solve the issue? How to hold my colors in col headers?

Thanks in advance

Upvotes: 0

Views: 1766

Answers (1)

Mike Scotty
Mike Scotty

Reputation: 10782

The re-creation of the HTML elements due to the virtual scrolling can indeed be annoying.

Your best bet is to override the scroll function with your own, where you re-add the styling to the column(s):

$(function () {
    $('#grid').w2grid({
        name: 'grid',
        columns: [
            { field: 'fname', caption: 'First Name', size: '300px' },
            { field: 'lname', caption: 'Last Name', size: '300px' },
            { field: 'dummy1', caption: 'Dummy 1', size: '300px' },
            { field: 'dummy2', caption: 'Dummy 2', size: '300px' },
            { field: 'sdate', caption: 'Dates', size: '300px' }
        ],
        "records": [
        { "recid": 1, "fname": "Joseph", "lname": "Haydn", "sdate": "1732-1809" },
        { "recid": 2, "fname": "Ludwig Van", "lname": "Beethoven", "sdate": "1770-1827" },
        { "recid": 3, "fname": "Wolfgang Amadeus", "lname": "Mozart", "sdate": "1756-1791" },
        { "recid": 4, "fname": "Johann Sebastian", "lname": "Bach", "sdate": "1685-1750" },
        ],
        onRender: function(event){
            event.done(setBgColors);
        },
    });

    w2ui.grid.scroll_bak = w2ui.grid.scroll;
    w2ui.grid.scroll = function(event){
        this.scroll_bak(event);
      setTimeout(setBgColors);
    };

    function setBgColors(){
        $("#grid_grid_column_0").addClass("bg_red");
      $("#grid_grid_column_4").addClass("bg_blue");
    }

});

Fiddle: http://jsfiddle.net/dt3yv2q4/2/

Or you could just disable virtual scrolling, making the changes to your DOM elements persistent:

$(function () {
    $('#grid').w2grid({
        name: 'grid',
        disableCVS: true,
        columns: [
            { field: 'fname', caption: 'First Name', size: '300px' },
            { field: 'lname', caption: 'Last Name', size: '300px' },
            { field: 'dummy1', caption: 'Dummy 1', size: '300px' },
            { field: 'dummy2', caption: 'Dummy 2', size: '300px' },
            { field: 'sdate', caption: 'Dates', size: '300px' }
        ],
        "records": [
        { "recid": 1, "fname": "Joseph", "lname": "Haydn", "sdate": "1732-1809" },
        { "recid": 2, "fname": "Ludwig Van", "lname": "Beethoven", "sdate": "1770-1827" },
        { "recid": 3, "fname": "Wolfgang Amadeus", "lname": "Mozart", "sdate": "1756-1791" },
        { "recid": 4, "fname": "Johann Sebastian", "lname": "Bach", "sdate": "1685-1750" },
        ],
        onRender: function(event){
            event.done(setBgColors);
        },
    });

    function setBgColors(){
        $("#grid_grid_column_0").addClass("bg_red");
      $("#grid_grid_column_4").addClass("bg_blue");
    }

});

Fiddle: http://jsfiddle.net/dt3yv2q4/3/ or http://jsfiddle.net/0Lkrb19w/

Upvotes: 0

Related Questions