【问题标题】:How to get a details for a specific cell of a row in jquery datatables dynamically如何动态获取jquery数据表中行的特定单元格的详细信息
【发布时间】:2012-09-23 06:35:38
【问题描述】:

我在检索 jquery 数据表行中特定单元格的详细信息时遇到问题。我正在尝试做的是针对 Datatable 中一行的特定单元格,我正在尝试检索特定于它的所有数据并将其显示为输出

例如,从数据库中检索所有国家/地区名称并显示在数据表的特定列中。现在我想要的是,当我单击国家名称时,应显示与其对应的所有城市。我希望它足够有意义。我需要在不刷新页面的情况下动态执行 dis 。

这里是代码的一瞥:

$(document).ready(function() {
oTable = $('#datatables').dataTable( {
    alert("hi")
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "../getcities.php",
    "aData" : "POST","getcities.php?cname="+cname
    "aoColumns": [
        { "sClass": "center", "bSortable": false },
        null,
        { "sClass": "center" },
    ]
} );

$('#datatables tbody tr td').live( 'click', function () {
    alert("hi")
    var nTr = this.parentNode.parentNode;

    // I need to display the data here in a dialog box
} );
}

上面的代码可能不完全正确,因为我说我还是 jquery 数据表和 Ajax 的新手 这是我正在尝试的 html:

<div>                           
<table id = "datatables" class="display">
<thead>
    <tr>
        <th></th>
        <th>Country Name</th>
    </tr>
<thead>
<tbody>
    <?while($row=mysql_fetch_array($result)){
    <tr>                            
        <td class="center" id="cname" value="cname"><?= $row['cname']?></td>
    </tr>
    <?}}?>
</tbody>

getcities.php 中的代码:

// Here I am trying to store all the cities corresponding to the country name in an array and returning it
<?php
 country = $_POST['cname'];
 var arr = new Array();
 arr = mysql_fetch_array(mysql_query("SELECT cityname FROM dbtable WHERE cname = '.country.'"))

return arr; 
?>

由于我是数据表的新手,所以我不明白如何实现这一点。我使用 PHP 作为脚本语言

请帮忙

【问题讨论】:

  • @Registered User : 立即查看我的更新

标签: jquery datatables


【解决方案1】:

试试下面的代码

$('#datatables tbody tr td img').live( 'click', function () {
    var iPos = oTable.fnGetPosition( this );
    if(iPos!=null){
        var aData = oTable.fnGetData( iPos );//get data of the clicked row
        var colData = aData[1];//get column data of the row
    }
} );

另外,看看这个: How to select a row in Jquery datatable

【讨论】:

  • 我收到此错误:DataTables 警告(表 id = 'datatables'):从第 0 行的数据源请求未知参数 '1'
  • 查看我的更新答案,oTable.fnGetData( iPos )(b.t.w aData[1] 是第 2 列)
  • 我尝试使用 .on() ffunction 而不是委托,但我再次遇到同样的错误'
  • @Neal 说,如果你使用像这样的委托 $("#datatables tbody").delegate("tr", "click", function() {
  • @Neal google 稍微了解一下,例如 datatables.net/forums/discussion/5331/…,也可以试试var colData = aData[0](获取第一列而不是第二列)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-07
  • 2019-03-01
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
相关资源
最近更新 更多