如何使用 jQuery DataTables 捕获选定行中的数据

发布于 2024-11-01 05:21:51 字数 2738 浏览 1 评论 0原文

我有这样的数据表设置:

$(document).ready(function() {
    $('#RectifiedCount').dataTable( {
        "bJQueryUI": true,
        "bProcessing": true,
        "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        "bStateSave": true,
        "sDom": '<"H"if>tr<"F"lTp>',
        "aoColumns":[
                     {'sname':'count_id', 'sType':'numeric', 'bVisible':false},
                     {'sName':'count_type', 'sType':'string','bVisible':true},
                     {'sName':'count_date', 'sType':'date','bVisible':true},
                     {'sName':'count_count', 'sType':'numeric','bVisible':true},
                     {'sName':'count_notes', 'sType':'string','bVisible':true}
                     ],
        "oTableTools": {
            "sRowSelect": "single",
            "sSwfPath": "media/swf/copy_cvs_xls_pdf.swf",
            "aButtons": [ {sExtends :'select_none' , 'sButtonText':'Clear Selection'}],
            "fnRowSelected": function(node){
                var s=$(node).children();
                if($(s[0]).text()=='Delivery') return ;
                $('select[name="count_type"]').val($(s[0]).text());
                $('input[name="count_date"]').val($(s[1]).text());
                $('input[name="count_count"]').val($(s[2]).text());
                $('textarea[name="count_notes"]').val($(s[3]).text());
            }
        },
        'sScrollX':'100%'
    });
});

当我选择一行时,我想将该行的单元格的值复制到一些与“sName”属性命名相同的表单字段中。我有两个问题:

  • 是否有 TableTools 方法用于访问所选行中单元格的值?像 node['sName_whatever'].value 这样的东西会很好。
  • 如何获取 bVisible=false 的单元格的值?

ETA解决方案

(省略不重要的内容)

$(document).ready(function() {
    rctable=$('#RectifiedCount').dataTable( {
        "aoColumns":[
                     {'sname':'count_id', 'sType':'numeric', 'bVisible':false},
                     {'sName':'count_type', 'sType':'string','bVisible':true},
                     {'sName':'count_date', 'sType':'date','bVisible':true},
                     {'sName':'count_count', 'sType':'numeric','bVisible':true},
                     {'sName':'count_notes', 'sType':'string','bVisible':true}
                     ],
        "oTableTools": {
            "sRowSelect": "single",
            "fnRowSelected": function(node){
                aData = rctable.fnGetData(node); //nice array of cell values
                if(aData[0]=='Delivery') return ;
                $('select[name="count_type"]').val(aData[0]);
                $('input[name="count_date"]').val(aData[1]);
                $('input[name="count_count"]').val(aData[2]);
                $('textarea[name="count_notes"]').val(aData[3]);            }
        }
    });
});

I have this datatable setup:

$(document).ready(function() {
    $('#RectifiedCount').dataTable( {
        "bJQueryUI": true,
        "bProcessing": true,
        "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        "bStateSave": true,
        "sDom": '<"H"if>tr<"F"lTp>',
        "aoColumns":[
                     {'sname':'count_id', 'sType':'numeric', 'bVisible':false},
                     {'sName':'count_type', 'sType':'string','bVisible':true},
                     {'sName':'count_date', 'sType':'date','bVisible':true},
                     {'sName':'count_count', 'sType':'numeric','bVisible':true},
                     {'sName':'count_notes', 'sType':'string','bVisible':true}
                     ],
        "oTableTools": {
            "sRowSelect": "single",
            "sSwfPath": "media/swf/copy_cvs_xls_pdf.swf",
            "aButtons": [ {sExtends :'select_none' , 'sButtonText':'Clear Selection'}],
            "fnRowSelected": function(node){
                var s=$(node).children();
                if($(s[0]).text()=='Delivery') return ;
                $('select[name="count_type"]').val($(s[0]).text());
                $('input[name="count_date"]').val($(s[1]).text());
                $('input[name="count_count"]').val($(s[2]).text());
                $('textarea[name="count_notes"]').val($(s[3]).text());
            }
        },
        'sScrollX':'100%'
    });
});

When I select a row, I want to copy the values of the cells of that row into some form fields that are named the same as the 'sName' attributes. I have 2 questions:

  • is there a TableTools method for accessing the value of a cell in a selected row? Something like node['sName_whatever'].value would be nice.
  • how can I get the value of the cells where bVisible=false?

ETA solution

(leaving out the unimportant stuff)

$(document).ready(function() {
    rctable=$('#RectifiedCount').dataTable( {
        "aoColumns":[
                     {'sname':'count_id', 'sType':'numeric', 'bVisible':false},
                     {'sName':'count_type', 'sType':'string','bVisible':true},
                     {'sName':'count_date', 'sType':'date','bVisible':true},
                     {'sName':'count_count', 'sType':'numeric','bVisible':true},
                     {'sName':'count_notes', 'sType':'string','bVisible':true}
                     ],
        "oTableTools": {
            "sRowSelect": "single",
            "fnRowSelected": function(node){
                aData = rctable.fnGetData(node); //nice array of cell values
                if(aData[0]=='Delivery') return ;
                $('select[name="count_type"]').val(aData[0]);
                $('input[name="count_date"]').val(aData[1]);
                $('input[name="count_count"]').val(aData[2]);
                $('textarea[name="count_notes"]').val(aData[3]);            }
        }
    });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

累赘 2024-11-08 05:21:51

我做了以下事情:

 oTable = $('#RectifiedCount').dataTable( ....);

 $('#RectifiedCount tbody tr').live('click', function (event) {        
    var aData = oTable.fnGetData(this); // get datarow
    if (null != aData)  // null if we clicked on title row
    {
        //now aData[0] - 1st column(count_id), aData[1] -2nd, etc. 
    }
});

I did the following:

 oTable = $('#RectifiedCount').dataTable( ....);

 $('#RectifiedCount tbody tr').live('click', function (event) {        
    var aData = oTable.fnGetData(this); // get datarow
    if (null != aData)  // null if we clicked on title row
    {
        //now aData[0] - 1st column(count_id), aData[1] -2nd, etc. 
    }
});
油饼 2024-11-08 05:21:51

与挂钩点击事件相比,有更好的方法,只使用 jquery 和 TableTools:

"oTableTools": {
"sRowSelect": "single",
"fnRowSelected": function(node) {
    var row = $(node).find('td');
    //all cells
    $.each(row, function(index, td) {
        console.log($(td).text());
    });

    console.log("Specific cell content: " + $(row[2]).text());

}

}

Mutch better way instead of hooking a click event, using just jquery and TableTools:

"oTableTools": {
"sRowSelect": "single",
"fnRowSelected": function(node) {
    var row = $(node).find('td');
    //all cells
    $.each(row, function(index, td) {
        console.log($(td).text());
    });

    console.log("Specific cell content: " + $(row[2]).text());

}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文