如何通过单击同一行DataTable上的编辑按钮来获取Id值 [英] How to get Id value by clicking Edit button on the same row of DataTable

查看:3248
本文介绍了如何通过单击同一行DataTable上的编辑按钮来获取Id值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 jQuery Datatable 列出记录,并添加一个动作按钮(编辑),以在模态对话框如果我选择一行,我可以获得行id值,并在模态对话框中打开相关记录。但是,如果我直接单击编辑按钮,则无法获取相关记录的Id值(位于同一行),因为单击编辑按钮。我想要做的是:我想获得我点击编辑按钮的行的Id值。可能吗?如果没有,当我点击编辑按钮时,是否可以以编程方式选择悬停的行? (如果以前的情况是可能的话,我更喜欢)。任何想法?

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
};

推荐答案

您可以使用

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() );

});

在线演示

使用浏览器控制台查看结果。

Use the browser console to see the results.

这篇关于如何通过单击同一行DataTable上的编辑按钮来获取Id值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆