简体   繁体   English

用户单击时,使用Jquery检索动态表的行信息

[英]Retrieve row info of a dynamic table using Jquery when user clicks on it

I am new to Jquery. 我是Jquery新手。 I am learning the things one by one. 我正在逐一学习事物。 my requirement is to create a dynamic jquery datatable.So I created in the following way: The variable formattedCKEY is a dynamic value that I am using for generating the table id's dynamically 我的要求是创建一个动态的jquery数据表。因此,我以以下方式创建了:变量formattedCKEY是一个动态值,用于动态生成表ID。

var issueTableId = "issuetable_"+formattedCKEY;
var $issuetable = $("
<table  id='" + issueTableId + "' class='stripe nowrap'  style='position:relative; top:0px; width:95%;'></table>
");
$issuetable.append('
<thead>
   ').children('thead')
   .append('
   <tr />
      ').children('tr')
      .append('
      <th nowrap>Select</th>
      '+
      '
      <th nowrap>Priority</th>
      ' +
      '
      <th nowrap>Issue ID</th>
      ' +
      var $tbody = $issuetable.append('
<tbody />
   ').children('tbody');
   $.each(cases, function () {
   $tbody.append('
   <tr />
      ').children('tr:last')
      .append('
      <td nowrap><input type="checkbox" />&nbsp;</td>
      ')
      .append('
      <td nowrap>' + $(this).find("PRTY").text() + '</td>
      ')
      .append(sourceRow)
      .append('
      <td nowrap>' + $(this).find("ISSUEID").text() + '</td>
      ');
      });

Here I am struggling with selecting the row information when the user click on that particular row. 在这里,当用户单击特定行时,我很难选择行信息。 can anybody help me out how to do this when there are dynamic table id's? 有动态表ID时,有人可以帮我怎么做吗?

I have tried with following code so far: 到目前为止,我已经尝试使用以下代码:

 //DataTables aplies style and behavior to <table>
 var table = $("#" + issueTableId).DataTable({
     "scrollY": 315, // inconsistent IE7/other
     "scrollX": true,
     "searching": false,
     "paging": false,
     "info": false
 });

 table.rows().every(function(rowIdx, tableLoop, rowLoop) {
     var row = this.row(rowIdx).node();
     // add contextMenu to each rows onclick
     $(row).click(displayResultslistPopup);
     // add onclick for each rows checkbox
     $(row).find('input:checkbox').click(function(event) {
         event.stopPropagation();
         $('#SAcheckBox').prop('checked', false);
     });
     // use presence of the attachmentImage.png to add jquery click event for td:
     $(row).find('td:has(img)').click(function(event) {
         event.stopPropagation();
         var id = $(this).parent().attr('id');
         issueAttachmentAttachmentLookUp(id);
     });
 });

But somehow table.rows().every() is not working for me. 但是以某种方式table.rows().every()对我不起作用。 My intention is to register the click event on table row and retrieve the necessary row information. 我的意图是在表行上注册click事件并检索必要的行信息。

If you want to bind a function to your row click, you can use click() function of jquery and pass a callback function. 如果要将函数绑定到行单击,则可以使用jquery的click()函数并传递回调函数。

$tbody.append('<tr />')
    .click(rowClickCallback)
    .children('tr:last')....

function rowClickCallback(e) {
    console.log(e.target); // It returns the TD which user clicked
    console.log(e.target.parentElement); // It returns the TR which user clicked
}

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

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