简体   繁体   English

DataTables jQuery标识行ID

[英]DataTables jquery identifying row ID

I am using DataTables and capturing am img click on either edit or delete icon's. 我正在使用DataTables并捕捉到img,单击“编辑”或“删除”图标。 My problem is, I need to capture the row ID as well.. I tried putting the row ID in the href tag but npt able to extract it.. 我的问题是,我需要捕获行ID。我尝试将行ID放在href标签中,但是npt能够提取它。

The current code is 当前代码是

 $('#datatable tbody tr a.delete img').live( 'click', function (e) {
 var rowID = $('a').attr('href');
 alert(rowID);

     if (!fancyConfirm(rowID, "Are you sure you want to delete this record?", function(ret) { alert(rowID) })) 
          e.preventDefault();

     });

You can see the actual page at http://www(@)fisheragservice(@)com/tm/users(@)html Please replace the (@)'s with .'s because the page contans actual email addresses I rather not have a spma bot find.. 您可以在http:// www(@)fisheragservice(@)com / tm / users(@)html中查看实际页面。请用。替换(@)的。因为页面包含实际的电子邮件地址而不是有一个spma bot发现..

You care selecting all the anchors using $('a') to get the rowID. 您需要使用$('a')选择所有锚来获取rowID。 That will not work. 那不管用。 Instead use the parent().attr() function in the click handler. 而是在点击处理程序中使用parent()。attr()函数。 ie: 即:

$('#datatable tbody tr a.delete img').live( 'click', function (e) {
 var rowID = $(this).parent().attr('href');
 alert(rowID);

     if (!fancyConfirm(rowID, "Are you sure you want to delete this record?", function(ret) { alert(rowID) })) 
          e.preventDefault();

     });

Can't you just attach the click event on the link instead of the img inside it and then use this ? 您能否仅将click事件而不是链接中的img附加到链接上,然后使用this

 $('#datatable tbody tr a.delete').live( 'click', function (e) {
 var rowID = $(this).attr('href');
 alert(rowID);

 if (!fancyConfirm(rowID, "Are you sure you want to delete this record?", function(ret) { alert(rowID) })) 
      e.preventDefault();

 });

Example link 示例链接

I am unsure if you are looking for the actual table row, or if the row has some meaningful information like a database key. 我不确定您是否要查找实际的表行,或者该行是否具有一些有意义的信息(例如数据库键)。

if you indeed looking at a row id, consider the example below, which uses the fnRender attribute. 如果您确实在查看行ID,请考虑下面的示例,该示例使用fnRender属性。

var oTable = $('#inventory_list').dataTable ({
'bServerSide'    : true,
'bAutoWidth'     : false,
'bJQueryUI'      : false,
'sPaginationType': 'full_numbers',
'sAjaxSource'    : '/inventory/listall',
'aoColumns'      : 
[
{
  'bSearchable': false,
        "bSortable": false,
        "fnRender" : function ( oObj )
        {
          var colval = '<div class="editcol"><a href="/inventory/edit/'  +
                oObj.aData[0] + '">' + 
                '<img src="/img/edit.png" alt="edit"><a/>' +
                '<a href="/inventory/delete/' + oObj.aData[0] + '">' +
                '<img src="/img/delete.png" alt="delete"><a/>' +
                '</div>';

                return colval;
        }
},
null,
null,
null,
null,
null,
null,
null,
null,
null
]
});

The first column (out of 10) contains the edit/delete icons. 第一列(总共10个)包含编辑/删除图标。

The listall call to the server return the inventory id in the first cell, which is referenced by oObj.aData[0], ans used to build the URL. 对服务器的listall调用在第一个单元格中返回清单ID,该ID由oObj.aData [0]引用,用于构建URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM