Conditional Fomatting trouble

Conditional Fomatting trouble

SoundwavesSoundwaves Posts: 12Questions: 3Answers: 0

So I have been using the below procedure for my datatable and it has been working great. I've recently added on the buttons portion to hide columns. This works fine as well, however the code I am using to conditional format the rows relies on the index of the column. Once I start hiding rows, that no longer works.

So the optimal solution would be to get these values by the field name instead of the index. To cary it one step further, if possible I would like to remove the buttons for the two columns I am getting values from. So the user can't hide them.

I have had no luck implimenting either of these items.
Any help would be apreciated,
Brandon

I am running DataTables 1.10.16 and Buttons for DataTables 1.4.2

$(document).ready(function () {
                    var table = $('#npwoList').dataTable({
                        dom: 'Bfrtip',
                        columns: [
                            { 'data': 'WO_Id' },
                            { 'data': 'Priority' },
                            { 'data': 'Date_Submitted' },
                            { 'data': 'Submitted_By' },
                            { 'data': 'Task' },
                            { 'data': 'ChargeNumber' },
                            { 'data': 'Summary' },
                            { 'data': 'Department' },
                            { 'data': 'Machine' },
                            { 'data': 'Full_Name' },
                            { 'data': 'Status' },
                            { 'data': 'Requested_Completion' },
                            { 'data': 'Completion_Date' },
                            { 'data': 'Late' }
                        ],
                        bServerSide: true,
                        sAjaxSource: 'Handlers/WODataHandler.ashx',
                        deferRender: true,
                        lengthMenu: [
                        [10, 25, 50, 100, 1000],
                        ['10 rows', '25 rows', '50 rows', '100 rows', 'Show all']
                        ],
                        stateSave: true,
                        buttons: ['pageLength',
                             {
                                 extend: 'colvis',
                                 collectionLayout: 'fixed two-column',
                                 postfixButtons: ['colvisRestore']
                             }
                        ],
                        language: {
                            buttons: {
                                colvis: 'Hide/Show Columns'
                            }
                        },
                        "scrolly": 400,
                        "stateSave": true,
                        "order": [[0, "desc"]],
                        "stateSaveParams": function (settings, data) {
                            data.start = 0;
                        },
                        "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                            $(nRow).find('td:eq(13)').addClass('clear');
                            if ($(nRow).find('td:eq(10)').text() == 'In-Process' &&
                                $(nRow).find('td:eq(13)').text() > 0) {
                                $(nRow).addClass('green');
                            }
                            if ($(nRow).find('td:eq(10)').text() != 'Complete' &&
                                $(nRow).find('td:eq(13)').text() < 0) {
                                $(nRow).addClass('red');
                            }
                            if ($(nRow).find('td:eq(10)').text() != 'Complete' &&
                                $(nRow).find('td:eq(10)').text() != 'In-Process' &&
                                $(nRow).find('td:eq(13)').text() > 0) {
                                $(nRow).addClass('black');
                            }
                        }
                    });
                    $('#npwoList tbody').on('click', 'tr', function () {
                        var id = $('td', this).eq(0).text();
                        document.getElementById('<%=txtWorkOrderId.ClientID%>').value = id;
                        var clickButton = document.getElementById("<%= btnOpenNPWO.ClientID%>");
                        clickButton.click();
                    });
                });

Replies

  • allanallan Posts: 62,338Questions: 1Answers: 10,228 Site admin

    You need to use the API to get the nodes regardless of column visibility. e.g.:

    rowCallback: function ( row, data, displayIndex ) {
      var api = this.api();
      $(api.cell( displayIndex, 13 ).node().addClass('clear');
      ...
    }
    

    Regards,
    Allan

  • SoundwavesSoundwaves Posts: 12Questions: 3Answers: 0

    I just gave that a try, I assume I can use .text() with that as well to retrieve values?

    I am getting an "Object doesn't support property or method 'addClass'" error when I run it, no output.

    It's breaking on the first line

    $(api.cell( displayIndex, 13 ).node().addClass('clear'));
    
  • allanallan Posts: 62,338Questions: 1Answers: 10,228 Site admin

    The parenthesis grouping is wrong - it should be:

    $(api.cell( displayIndex, 13 ).node()).addClass('clear');
    

    Allan

  • SoundwavesSoundwaves Posts: 12Questions: 3Answers: 0

    Works lIke a charm!

    Thanks so much,
    Brandon

This discussion has been closed.