Vamshi Vangapally
Vamshi Vangapally

Reputation: 2032

Add an ID to first tr in jquery datatables

var row = [teams[i].team_id, teams[i].team_name,...];
var a = oTable.fnAddData(row);

I'm using jquery datatables and adding teams data to datatables in above mentioned way, but I need the first column to be an auto increment one(I'll use a loop), but I want to add team_id as id for the tr element that carries this auto-incremented value in first column.

How do I add "id" to the tr while datatable is populated.

I need output as <tr id="[team_id]">1</tr>

Upvotes: 0

Views: 1292

Answers (3)

Jay Rizzi
Jay Rizzi

Reputation: 4304

By including DT_Rowid as a property on the data source object, it will automatically create a table row id without any code whatsoever

For example, from an ajax source:

{
  "sEcho": 1,
  "iTotalRecords": "57",
  "iTotalDisplayRecords": "57",
  "aaData": [
    {
      "DT_RowId": "row_7",
      "DT_RowClass": "gradeA",
      "0": "Gecko",
      "1": "Firefox 1.0",
      "2": "Win 98+ / OSX.2+",
      "3": "1.7",
      "4": "A"
    },

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

Note that DT_Rowid is case sensitive, so alias database queries with proper capitalization where necessary

Upvotes: 1

Optimus Prime
Optimus Prime

Reputation: 6907

The introduction of jQuery API page says:

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\.

So you may then use

$('tr:first').attr('id','\\[team_id\\]')

Upvotes: 0

limovala
limovala

Reputation: 469

f there is no other table in the page you then you can use

 $('table').find('tr:first').attr('id',team_id);

or add an id to the table and say tableID

 $('#tableID').find('tr:first').attr('id',team_id);

Upvotes: 0

Related Questions