Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I make a row clickable in Datatables?

Posted on 2017-08-21
2
Low Priority
?
111 Views
Last Modified: 2017-08-21
I am using jQuery Datables with Ajax, so my rows are created dynamically when the datatable loads. Does anyone know how I can make the row, or a column clickable? I'd like to make it so when a user clicks a row, or one of the items in a column it links them to another page. In the URL they click, I will need to include data from one of the columns in that row. For example, if someone clicked the first row below, I'd want to send them to a url: www.myshop.com/newcar.html?ID=77

ID     Carname    Cartype
-------------------------------------
77     carA            chevy
76     carB            dodge
0
Comment
Question by:earwig75
2 Comments
 
LVL 10

Accepted Solution

by:
Michael Vasilevsky earned 1000 total points
ID: 42263565
Use the mousedown event to open a new window, e.g.:

$( "#myRow" ).on("mousedown", ".clickableRow", function(event) {
        window.open('www.myshop.com/newcar.html?' + $(this).attr('id'), '_blank');
});

Open in new window


Where myRow is the ID of the container holding your rows, clickableRow is a class assigned to all your clickable rows, and 'id' is the id of the row, e.g. 77.

Hope this helps!

MV
1
 

Author Comment

by:earwig75
ID: 42263570
Note: I ended up using this, but Michael's example is also valid.
$(document).ready(function() {
    var table = $('#example').DataTable();
     
    $('#example tbody').on('click', 'tr', function () {
        var data = table.row( this ).data();
        alert( 'You clicked on '+data[0]+'\'s row' );
    } );
} );

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…
Suggested Courses

824 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