styling background color in print in search filtering

styling background color in print in search filtering

spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

Hi, the link https://datatables.net/forums/discussion/79772/styling-background-color-in-print

That I have work around with display background color on each cell.

But there is other problem.

In the search function, when you type as the datatable will perform reduce the row which you can see screenshow as Showing 1 to 8 of 8 entries (filtered from 227 total entries)

When I click print, the background color is not the match to the value as it went mixed up like glitch.

I've research and I realise the color only run 1 to 8 rows out of 227 row to display on filtered search in print.

I wonder if you can help how to correct the background color on print?

Answers

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    I can customize the background color on cell no problem. Just search filter I need help with.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    How are you applying the background colour? Can you link to an example please?

    Allan

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    customize: function (doc, row) {
    //header color
    $(doc.document.body)
    .find("table thead tr th")
    .addClass(
    "datatable-header-background-color",
    );

    var _table = $("#example").DataTable();
    
    //count how many row on table
    var v = _table.column(1).data().toArray();
    
    for (var i = 0; i < v.length; i++) {
    
        //custom the color for print review
        $(doc.document.body)
            .find("table tbody tr:nth-child(" + (i + 1) + ") td:nth-child(4)")
            .addClass(CodeColor(_table.cell(i, 3).data()));
    }
    

    },

    Note, the CodeColor() is the function that match the value (switch) which it will use the css for background-color.

    So when you input search function. it show what I need to look. But when in print. it show different color background that not match to value.

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    By the way in previous post that I have problem with bootstrap4 and found other way to bypass it but this is search filtering.

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    Look like I've found a way to use rows and every function.

    var _table = $("#example").DataTable();

    _table.rows({
    search: "applied",
    })
    .every(function (
    rowIdx,
    tableLoop,
    rowLoop,
    ) {
    (how to insert background color on specific column)
    });

    So only thing is how to addClass on speciifc column on bold above.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Use cell().node(). You have the row index from rowIdx and you know your column index, so:

    _table.cell(rowIdx, colIdx).node().classList.add('myBoldClass');
    

    should hopefully do it.

    Allan

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    did use this and it return error:

    TypeError: Cannot read properties of undefined (reading 'classList')

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    If you can link to a page showing the issue I can take a look and see what is going wrong.

    Thanks,
    Allan

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    Hi allan,

    I have working on it all day. So I found a way to use rowLoop

    _table.rows({
    search: "applied",
    })
    .every(function (
    rowIdx,
    tableLoop,
    rowLoop,
    ) {

    $(doc.document.body)
    .find(
    "table tbody tr:nth-child(" +
    (rowLoop + 1) + // rowLoop is depend row in loop. ** Do not use rowIdx which is start from begin.
    ") td:nth-child(4)",
    )
    .addClass(CodeColor(_table.cell(i, 3).data()));
    ),
    );

    });

Sign In or Register to comment.