简体   繁体   English

如何从数据表单元格获取内部html值

[英]how to get the inner html value from Datatable cell

I am newbie to DataTable. 我是DataTable的新手。 here I am trying to get the of first cell value of a row when I click the viewlink associated with the row, instead of the value I am getting [object object]. 在这里,当我单击与该行关联的视图链接时,我试图获取该行的第一个单元格值,而不是获取的[object object]值。

heres my code 这是我的代码

        $(document).ready(function() {


            // Delete a record
            $('#example').on('click', 'a.editor_view', function (e) {
                e.preventDefault();
                var rowIndex = oTable.fnGetPosition( $(this).closest('tr')[0] );
                 aData = oTable.fnGetData($(this).parents('tr')[0]);
                alert(aData);
            } );

            // DataTables init

            var oTable=$('#example').dataTable( {
                "sDom": "Tfrtip",
                "sAjaxSource": "php/browsers.php",
                "aoColumns": [
                    { "mData": "browser" },
                    { "mData": "engine" },
                    { "mData": "platform" },
                    { "mData": "grade", "sClass": "center" },
                    {
                        "mData": null, 
                        "sClass": "center",
                        "sDefaultContent": '<a href="" class="editor_view">view</a> / <a href="" class="editor_remove">Delete</a>'
                    }
                ]
            } );
        } );

HTML Table: HTML表格:

<table cellpadding="0" cellspacing="0" border="0" class="display" id="example" width="100%">
<thead>
    <tr>
        <th width="30%">Browser</th>
        <th width="20%">Rendering engine</th>
        <th width="20%">Platform(s)</th>
        <th width="14%">CSS grade</th>
        <th width="16%">Admin</th>
    </tr>
</thead>
<tfoot>
    <tr>
        <th>Browser</th>
        <th>Rendering engine</th>
        <th>Platform(s)</th>
        <th>CSS grade</th>
        <th>Admin</th>
    </tr>
</tfoot>

Now when I Click on the view I need to Navigate to another page with the id like view.php?id=125 现在,当我单击视图时,我需要导航到另一个具有id的页面,例如view.php?id = 125

Thank you 谢谢

$('#example').on('click', 'a.editor_view', function (e) {
  e.preventDefault();
  var rowIndex = oTable.fnGetPosition( $(this).closest('tr')[0] );
  aData = oTable.fnGetData(rowIndex,0);
  alert(aData);
} );

From the api docs: 从api文档中:

fnGetData Input parameters: fnGetData输入参数:
{int|node}: A TR row node, TD/TH cell node or an integer. {int | node}:TR行节点,TD / TH单元节点或整数。 If given as a TR node then the data source for the whole row will be returned. 如果将其指定为TR节点,则将返回整行的数据源。 If given as a TD/TH cell node then iCol will be automatically calculated and the data for the cell returned. 如果给出为TD / TH单元节点,则将自动计算iCol并返回该单元的数据。 If given as an integer, then this is treated as the aoData internal data index for the row (see fnGetPosition) and the data for that row used. 如果以整数形式给出,则将其视为该行的aoData内部数据索引(请参见fnGetPosition)和该行使用的数据。

{int}: Optional column index that you want the data of. {int}:要获取其数据的可选列索引。

Assuming your first row is your id, would you want to include the links in your dataTable initializer like this? 假设第一行是您的ID,您是否要像这样在dataTable初始化程序中包含链接?

$(document).ready(function () {
    var oTable = $('#example').dataTable({
        "aoColumnDefs": [{
            "fnRender": function (oObj) {
                var id = oObj.aData[0];
                var links = [
                    '<a href="/view.php?id=' + id + '" class="editor_view">View</a>',
                    '<a href="/delete.php?id=' + id + '" class="editor_remove">Delete</a>'];
                return links.join(' / ');
            },
                "sClass": "center",
            "aTargets": [4]
        }, {
            "sClass": "center",
            "aTargets": [3]
        }]
    });
});

see: http://jsfiddle.net/9De6w/ 看到: http : //jsfiddle.net/9De6w/

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

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