Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is their a way where I can bind an Id with row in datatables?

I get 3 columns from my database and display 2 in datatable to user, but what I want is to attach 1st column (which is Id) to whole row so that when user clicks on a particular row, it takes user to different page depending on Id.

ex

"aoColumns": [
                    { "mData": "Id",
                        "bSearchable": false,
                        "bSortable": false,
                        //"bVisible": false,
                        "fnRender": function (oObj) {
                            return '<a href=\"Details/' + oObj.aData[0] + '\">Id</a>';
                        }
                     },

                    { "mData": "FullName" },
                    { "mData": "Age" }
                ]

and this is what I get in Json

{"sEcho":"1","iTotalRecords":1,"iTotalDisplayRecords":1,"aaData":[{"Id":425,"FullName":"xxx","Age":21,}]}

also oObj.aData[0] always comes out as undefined? I am showing FullName and Age but want user to click on row level.

Any help will be appreciated

Thanks

share|improve this question

1 Answer

up vote 1 down vote accepted

From the DataTables web site:

Often when using server-side processing you will find that it can be useful to have a specific ID on each row (the row ID from the database for example). By assigning the ID you want to apply to each row using the property DT_RowId of the data source object for each row, DataTables will automatically add it for you.

http://datatables.net/release-datatables/examples/server_side/ids.html

In short, you want to use "DT_RowID" for the name in your JSON array, not simply "ID".

You can then bind an event like so:

jQuery('#table tbody tr').live('dblclick', function () { viewRow(this.id); } );

The "live" means this event will occur for any row that is added to the DataTable dynamically, not just rows that were there when the table was created. If you are never adding rows dynamically, you could just bind to the dbclick event itself.

Hope this helps.

bqb

share|improve this answer
worked like a charm – Jay May 15 at 10:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.