james Makinde
james Makinde

Reputation: 993

Apply background color to datatables row when expanded

When the row is expanded, I want to change the background color of the row, but that doesn't happen all the time. I apply the class, but its not working as well. It only highlights the first row when I expand another row. Is there something I'm doing wrong. My code is below.

var table = $('#meterDataTable').DataTable();
var tr = $(this).closest('tr');
var row = table.row(tr);

if (row.child.isShown()) {
    // This row is already open - close it
    row.child.hide();
    tr.removeClass('shown');
    tr.removeClass('highlightExpanded');
} else {
    // Open this row
    var elementName = row.data()[0];
    row.child(metersAttributesCache[elementName]).show();
    tr.addClass('highlightExpanded');
    tr.addClass('shown');
    //$('#unitAttributeDataTable').DataTable({ "bFilter": false });
    $('.toggleCheckBox').bootstrapToggle({});
}

Upvotes: 3

Views: 3611

Answers (1)

Gyrocode.com
Gyrocode.com

Reputation: 58930

CAUSE

Your code seems to be missing some parts, it should be in the click event handler attached to td.details-control. Also make sure you're using !important in your CSS rule for .highlightExpanded to make sure it takes precedence.

DEMO

/* Formatting function for row details - modify as you need */
function format ( d ) {
    // `d` is the original data object for the row
    return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
        '<tr>'+
            '<td>Full name:</td>'+
            '<td>'+d.name+'</td>'+
        '</tr>'+
        '<tr>'+
            '<td>Extension number:</td>'+
            '<td>'+d.extn+'</td>'+
        '</tr>'+
        '<tr>'+
            '<td>Extra info:</td>'+
            '<td>And any further details here (images etc)...</td>'+
        '</tr>'+
    '</table>';
}
 
$(document).ready(function() {
  
    var table_data = [{"name":"Tiger Nixon","position":"System Architect","salary":"$320,800","start_date":"2011/04/25","office":"Edinburgh","extn":"5421"},{"name":"Garrett Winters","position":"Accountant","salary":"$170,750","start_date":"2011/07/25","office":"Tokyo","extn":"8422"},{"name":"Ashton Cox","position":"Junior Technical Author","salary":"$86,000","start_date":"2009/01/12","office":"San Francisco","extn":"1562"}];
  
    var table = $('#example').DataTable( {
        "data": table_data,
        "columns": [
            {
                "className":      'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
            },
            { "data": "name" },
            { "data": "position" },
            { "data": "office" },
            { "data": "salary" }
        ],
        "order": [[1, 'asc']]
    } );
     
    // Add event listener for opening and closing details
    $('#example tbody').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var row = table.row( tr );
 
        if ( row.child.isShown() ) {
            // This row is already open - close it
            row.child.hide();
            tr.removeClass('shown');
            tr.removeClass('highlightExpanded');
        }
        else {
            // Open this row
            row.child( format(row.data()) ).show();
            tr.addClass('shown');
            tr.addClass('highlightExpanded');
        }
    } );

 
} );
td.details-control {
    background: url('https://raw.githubusercontent.com/DataTables/DataTables/1.10.7/examples/resources/details_open.png') no-repeat center center;
    cursor: pointer;
}
tr.shown td.details-control {
    background: url('https://raw.githubusercontent.com/DataTables/DataTables/1.10.7/examples/resources/details_close.png') no-repeat center center;
}

tr.highlightExpanded {
  background-color:#F4CCCC !important;
}
<link href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>

<table id="example" class="display">
<thead>
    <tr>
        <th></th>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Salary</th>
    </tr>
</thead>

<tfoot>
    <tr>
        <th></th>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Salary</th>
    </tr>
</tfoot>
</table>

Upvotes: 2

Related Questions