delegate click on tr

delegate click on tr

MrThomasMrThomas Posts: 7Questions: 0Answers: 0
edited March 2012 in General
How do you delegate 'click' to the TR generated data in the datatable. I have got a php file that has to load with itemID passed through the query.I need to be able to do some thing like this but how dont know how to get the itemID

[code]
$('#tbUsers tr').live( 'click', function () {

window.location.href="viewItem.php?id="+itemID;
});
[/code]

Please find the code I have got so far

JSON data from the php file

[code]
{
"users":[
{"itemID":"1","itemName":"item1","itemPrice":"30","itemUnits":"2"},
{"itemID":"2","itemName":"item2","itemPrice":"25","itemUnits":"1"}
]
}

[/code]


[code]


itemName
itemPrice
itemUnits




$('#tbUsers').dataTable( {
"sAjaxSource": "getUsers.php",
"sAjaxDataProp": "users",
"aoColumns": [
{ "sTitle": "itemName", "mDataProp": "itemName" },
{ "sTitle": "itemPrice", "mDataProp": "itemPrice" },
{ "sTitle": "itemUnits", "mDataProp": "itemUnits" }
]
});
});

[/code]

Replies

  • jinjungjinjung Posts: 24Questions: 0Answers: 0
    edited March 2012
    Hi MrThomas,
    I think that the best way is to populate the id with your itemID, then fetch it in your event.
    For populating tr id, you have to push "DT_RowId" in your JSON with your itemID as value, datatables will insert it automatically for each tr, it's just wonderfull.

    [code]
    {
    "users":[
    {"DT_RowId":"1","itemName":"item1","itemPrice":"30","itemUnits":"2"},
    {"DT_RowId":"2","itemName":"item2","itemPrice":"25","itemUnits":"1"}
    ]
    }
    [/code]
    Result will be :
    [code]

    item1
    30
    2


    item2
    25
    1

    [/code]

    Finaly, get your tr id like this :
    [code]
    $('#tbUsers tr').live( 'click', function () {
    var nTr = this;
    window.location.href="viewItem.php?id="+nTr.id;
    });
    [/code]
    Have a good day.
    Jinjung
This discussion has been closed.