最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get Id value by clicking Edit button on the same row of DataTable - Stack Overflow

programmeradmin0浏览0评论

I use jQuery Datatable for listing records and add an Action button (Edit) for editing the record on a modal dialog. If I select a row I can get the row id value and open the related record on modal dialog. However, if I click the Edit button directly, I cannot get the Id value of the related record (on the same row) because it is not selected first when clicking Edit button. What I want to do is that: I want to get the Id value of the row on which I click the Edit button. Is it possible? If not, can I select the hovered row programmatically when I click the Edit button? (If the prior scenario is possible I would prefer it). Any idea?

function openModal() {

    var table = $('#dtbListAccount').DataTable();
    var oRow = $('this').parents('tr')[0];
    var oData = table.fnGetData(oRow);

    //code omitted for brevity
};

I use jQuery Datatable for listing records and add an Action button (Edit) for editing the record on a modal dialog. If I select a row I can get the row id value and open the related record on modal dialog. However, if I click the Edit button directly, I cannot get the Id value of the related record (on the same row) because it is not selected first when clicking Edit button. What I want to do is that: I want to get the Id value of the row on which I click the Edit button. Is it possible? If not, can I select the hovered row programmatically when I click the Edit button? (If the prior scenario is possible I would prefer it). Any idea?

function openModal() {

    var table = $('#dtbListAccount').DataTable();
    var oRow = $('this').parents('tr')[0];
    var oData = table.fnGetData(oRow);

    //code omitted for brevity
};

Share Improve this question edited Sep 8, 2016 at 18:05 Jack asked Sep 8, 2016 at 17:45 JackJack 1 6
  • 1 var oRow= $('this').parents('tr')[0]; and var oData = oTable.fnGetData(oRow); – mmushtaq Commented Sep 8, 2016 at 17:51
  • this refer to your edit button.. oData contains data of clicked row – mmushtaq Commented Sep 8, 2016 at 17:52
  • oData contains id as well at 0 index – mmushtaq Commented Sep 8, 2016 at 17:59
  • Thanks a lot but encounter "TypeError: dataTable.fnGetData is not a function" error. Could you please have a look at my Javascript method parameters above? – Jack Commented Sep 8, 2016 at 18:05
  • actually you are using higher version of datatable. Wait let me provide you an example... – mmushtaq Commented Sep 8, 2016 at 18:10
 |  Show 1 more comment

4 Answers 4

Reset to default 12

You can use this code to achieve this.

var table;
$(document).ready( function () {
 table  = $('#example').DataTable();
} );

$('body').on('click', '#btnEdit', function(){
    //to get currently clicked row object
    var row  = $(this).parents('tr')[0];

    //for row data
    console.log( table.row( row ).data() );

});

It will return row data as a string array.

Live Demo Here

Use the browser console to see the results.

Here's the full source code. Hope this helps :)

//when button (edit button here) is clicked.... Note: no need id for buttons too, just use <button> tag
$('table button').click(function() {
  var tr = $(this).closest('tr');
  var id = tr.children('td:eq(0)').text(); //get the text from first col of current row
  console.log(id); //you'll get the actual ids here
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<table>
  <tr>
    <th>Id</th>
    <th>Name</th>
    <th>Surname</th>
    <th>Action</th>
  </tr>
  <tr>
    <td>1</td>
    <td>Hans</td>
    <td>Jahnsen</td>
    <td>
      <button>Edit</button>
    </td>
  </tr>
  <tr>
    <td>2</td>
    <td>Robert</td>
    <td>Boylstat</td>
    <td>
      <button>Edit</button>
    </td>
  </tr>
  <tr>
    <td>3</td>
    <td>Jim</td>
    <td>Alexi</td>
    <td>
      <button>Edit</button>
    </td>
  </tr>
</table>

Assign the row-id(s) to the edit buttons as well, write click events for the edit buttons which, based on the id of the button clicked on, triggers the edit functionality / view.

You could assign the row-id(s) to the buttons either when rendering itself, or write a small function that does the same on page load.

If the id is on the parent container then find it's value and use it. If it's a sibling then do the same.

发布评论

评论列表(0)

  1. 暂无评论