Data Tables

Posted on 2016-08-17
Last Modified: 2016-09-03
I have a DataTable and am able to use some of the previous help I've received on here to add a link to one of the columns that will open a modal.  I need to able to add an event to the table row selection.  I need some examples of how to to this.

I would like to be able to add a background color to the row when it is selected.
Show a Button on the page (that is hidden on page load).
Add an onclick function for the Button that opens a modal and passes some of the data.  

Thank You!
Question by:Crystal Rouse
LVL 29

Expert Comment

ID: 41760475
Do you mean GridView control? Or something else? DataTable is just basically data...
For a GridView see the SelectedIndexChanging event:
LVL 19

Accepted Solution

Manoj Patil earned 500 total points
ID: 41760596
If you are using Javascript DataTables then you can use a css class to change the background color of the row when select that particular row.

.row_selected td {
    background-color: black !important; /* Add !important to make sure override datables base styles */

Check the URL below for changing the row color on row Click event
Try below code
$(document).ready(function() {
    var table = $('#example').DataTable();
    $('#example tbody').on( 'click', 'tr', function () {
        if ( $(this).hasClass('selected') ) {
        else {
    } );
    $('#button').click( function () {
        table.row('.selected').remove().draw( false );
    } );
} );

Open in new window


<table id="example" class="display" cellspacing="0" width="100%">
                <th>Start date</th>
                <th>Start date</th>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Garrett Winters</td>
                <td>Ashton Cox</td>
                <td>Junior Technical Author</td>
                <td>San Francisco</td>
                <td>Cedric Kelly</td>
                <td>Senior Javascript Developer</td>
                <td>Airi Satou</td>

Open in new window

Standard css file which is in JQuery DataTable Package

Author Closing Comment

by:Crystal Rouse
ID: 41782918
Thank you for your help!  It worked.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Show confirmation alert in Jquery 14 41
Don't understand jquery promises 6 36
System.Speech 2 11
jQuery Validate 4 16
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now