Adding Button into the DataTable Row

Adding Button into the DataTable Row

vicky200456vicky200456 Posts: 8Questions: 1Answers: 0
edited March 2014 in General
Hi everyone,

I use Datatable to present the Employee information ,and using the Bootstrap CSS framework
Format Such as:
[code]

IDNameAction
0001Peter

[/code]

It will have one button in each row
when i clicked the button, the Modals(as dialog) will be shown on the page. So user can edit the information by each employee .

i added this script and there haven't response.
[code]
oTable.$('tr').find('button').click(function() {
var data = oTable.fnGetData(this);
var id = data[0];
alert(id);
});
[/code]

what should i do?

Regards
Rick.

Replies

  • robertbrowerrobertbrower Posts: 158Questions: 1Answers: 0
    Your table structure should include thead and tbody I think.

    I use the fnDrawCallback option to apply event handlers to html elements that exist inside my table.

    , "fnDrawCallback": function (oSettings) {

    var $table = $(oSettings.nTable).dataTable();

    $table.find("button").click(.........


    Maybe this will work for you.
  • vicky200456vicky200456 Posts: 8Questions: 1Answers: 0
    edited March 2014
    re robertbrower:

    thanks for your information.
    i rewirted the table structure as:
    [code]


    IDNameAction


    0001Peter

    [/code]


    But, i have no idea how apply fnDrawCallback option to my situation.
    i need to get the value of first cell in each row for query the specific employee. So i can present the correct detail in the dialog.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Rick, your static event looks okay in this case - fnDrawCallback is needed for non-HTML table cases (in certain cases anyway - a delegate event is usually better to use since it won't leak memory if the events aren't unbound).

    Your function isn't working because of this:

    > oTable.fnGetData(this);

    You are passing in the `button` element - which DataTables know nothing about. Try using:

    [code]
    oTable.fnGetData( $(this).closest('tr')[0] );
    [/code]

    Allan
  • vicky200456vicky200456 Posts: 8Questions: 1Answers: 0
    re Allan,

    Excellent!!Thanks for your solution.

    Now i can get the correct value by clicked the button in each row
    [code]
    oTable.$('tr').find('button').click(function() {
    var data = oTable.fnGetData($(this).closest('tr')[0]);
    var id = data[0];//first cell
    $('#empID').text(id);
    });
    [/code]


    Rick
This discussion has been closed.