jQuery-datatables,如何获取列id

jQuery-datatables,如何获取列id,jquery,datatables,Jquery,Datatables,如何在jquery的datatable插件中获取列id我需要数据库中更新的列id。fGetPosition 获取特定对象的数组索引 来自其DOM元素的单元格。最佳使用 与fnGetData()结合使用 输入参数: nNode:要查找的节点 位置。这可能是一个“TR” 表中的行或“TD”单元格。这个 返回参数取决于此 输入 返回参数: int或数组[int,int,int]:如果 节点是一个表行(TR),然后是 返回值将是一个带 aoData中行的索引 对象如果节点是表单元格 (TD)则返回值将为

如何在jquery的datatable插件中获取列id我需要数据库中更新的列id。

fGetPosition

获取特定对象的数组索引 来自其DOM元素的单元格。最佳使用 与fnGetData()结合使用

输入参数:

nNode:要查找的节点 位置。这可能是一个“TR” 表中的行或“TD”单元格。这个 返回参数取决于此 输入

返回参数:

int或数组[int,int,int]:如果 节点是一个表行(TR),然后是 返回值将是一个带 aoData中行的索引 对象如果节点是表单元格 (TD)则返回值将为 具有[aoData索引行、列]的数组 索引(隐藏行折扣),列 索引(包括隐藏行)]

代码示例:

$(document).ready(function() {
    $('#example tbody td').click( function () {
        /* Get the position of the current data from the node */
        var aPos = oTable.fnGetPosition( this );

        /* Get the data array for this row */
        var aData = oTable.fnGetData( aPos[0] );

        /* Update the data array and return the value */
        aData[ aPos[1] ] = 'clicked';
        this.innerHTML = 'clicked';
    } );

    /* Init DataTables */
    oTable = $('#example').dataTable();
} );

中,上面的代码片段实际上帮助我解决了我的特殊情况下的这个问题。这是我的密码:

// My DataTable
var oTable;

$(document).ready(function() {
    /*  You might need to set the sSwfPath! Something like:
    *   TableToolsInit.sSwfPath = "/media/swf/ZeroClipboard.swf";
    */
    TableToolsInit.sSwfPath = "../../Application/JqueryPlugIns/TableTools/swf/ZeroClipboard.swf";

    oTable = $('#tblFeatures').dataTable({
        // "sDom": '<"H"lfr>t<"F"ip>', // this is the standard setting for use with jQueryUi, no TableTool
        // "sDom": '<"H"lfrT>t<"F"ip>', // this adds TableTool in the center of the header
        "sDom": '<"H"lfr>t<"F"ip>T', // this adds TableTool after the footer
        // "sDom": '<"H"lfrT>t<"F"ip>T', // this adds TableTool in the center of the header and after the footer
        "oLanguage": { "sSearch": "Filter this data:" },
        "iDisplayLength": 25,
        "bJQueryUI": true,
        // "sPaginationType": "full_numbers",
        "aaSorting": [[0, "asc"]],
        "bProcessing": true,
        "bStateSave": true, // remembers table state via cookies
        "aoColumns": [
        /* CustomerId */{"bVisible": false },
        /* OrderId */{"bVisible": false },
        /* OrderDetailId */{"bVisible": false },
        /* StateId */{"bVisible": false },
        /* Product */null,
        /* Description */null,
        /* Rating */null,
        /* Price */null
        ]
    });

    // uncomment this if you want a fixed header
    // don't forget to reference the "FixedHeader.js" file.
    // new FixedHeader(oTable);
});

// Add a click handler to the rows - this could be used as a callback
// Most of this section of code is from the DataTables.net site
$('#tblFeatures tr').click(function() {
    if ($(this).hasClass('row_selected')) {
        $(this).removeClass('row_selected');
    }
    else {
        $(this).addClass('row_selected');

        // Call fnGetSelected function to get a list of selected rows
        // and pass that array into fnGetIdsOfSelectedRows function.
        fnGetIdsOfSelectedRows(fnGetSelected(oTable));
    }
});

function fnGetSelected(oTableLocal) {
    var aReturn = new Array();

    // fnGetNodes is a built in DataTable function
    // aTrs == array of table rows
    var aTrs = oTableLocal.fnGetNodes();

    // put all rows that have a class of 'row_selected' into
    // the returned array
    for (var i = 0; i < aTrs.length; i++) {
        if ($(aTrs[i]).hasClass('row_selected')) {
            aReturn.push(aTrs[i]);
        }
    }

    return aReturn;
}

// This code is purposefully verbose.
// This is the section of code that will get the values of
// hidden columns in a datatable
function fnGetIdsOfSelectedRows(oSelectedRows) {
    var aRowIndexes = new Array();
    var aRowData = new Array();
    var aReturn = new Array();
    var AllValues;

    aRowIndexes = oSelectedRows;    

    // The first 4 columns in my DataTable are id's and are hidden.
    // Column0 = CustomerId
    // Column1 = OrderId
    // Column2 = OrderDetailId
    // Column3 = StateId

    // Here I loop through the selected rows and create a
    // comma delimited array of id's that I will be sending
    // back to the server for processing.
    for(var i = 0; i < aRowIndexes.length; i++){
        // fnGetData is a built in function of the DataTable
        // I'm grabbing the data from rowindex "i"
        aRowData = oTable.fnGetData(aRowIndexes[i]);

        // I'm just concatenating the values and storing them
        // in an array for each selected row.
        AllValues = aRowData[0] + ',' + aRowData[1] + ',' + aRowData[2] + ',' + aRowData[3];
        alert(AllValues);
        aReturn.push(AllValues);
    }

    return aReturn;
}
//我的数据表
可变的;
$(文档).ready(函数(){
/*您可能需要设置sSwfPath!类似于:
*TableToolsInit.sSwfPath=“/media/swf/ZeroClipboard.swf”;
*/
TableToolsInit.sSwfPath=“../../Application/JqueryPlugIns/TableTools/swf/ZeroClipboard.swf”;
oTable=$('#tblFeatures')。数据表({
//“sDom”:“t',//这是与jQueryUi一起使用的标准设置,没有TableTool
//“sDom”:“t”,//这将在标题的中心添加TableTool
“sDom”:“tT”,//这会在页脚后添加TableTool
//“sDom”:“tT”,//这将在页眉的中心和页脚之后添加TableTool
“oLanguage”:{“sSearch”:“筛选此数据:”},
“iDisplayLength”:25,
“bJQueryUI”:没错,
//“sPaginationType”:“完整编号”,
“aaSorting”:[[0,“asc”]],
“bProcessing”:正确,
“bStateSave”:true,//通过cookies记住表状态
“aoColumns”:[
/*CustomerId*/{“bVisible”:false},
/*OrderId*/{“bVisible”:false},
/*OrderDetailId*/{“bVisible”:false},
/*StateId*/{“bVisible”:false},
/*Product*/null,
/*Description*/null,
/*额定值*/null,
/*价格*/null
]
});
//如果需要固定标题,请取消对此的注释
//不要忘记引用“FixedHeader.js”文件。
//新固定收割台(可旋转);
});
//将单击处理程序添加到行中-这可以用作回调
//这部分代码的大部分来自DataTables.net站点
$('#tblFeatures tr')。单击(函数(){
if($(this).hasClass('row_selected')){
$(this.removeClass('row_selected');
}
否则{
$(this.addClass('row_selected');
//调用fnGetSelected函数以获取所选行的列表
//并将该数组传递到fnGetIdsOfSelectedRows函数中。
fnGetIdsOfSelectedRows(fnGetSelected(oTable));
}
});
功能fnGetSelected(oTableLocal){
var aReturn=新数组();
//fnGetNodes是一个内置的DataTable函数
//aTrs==表行数组
var aTrs=oTableLocal.fnGetNodes();
//将具有“row_selected”类的所有行放入
//返回的数组
对于(变量i=0;i
我认为上面datatables.net网站上的股票答案没有帮助,也没有回答这个问题

我相信neko_ime希望获得与所选项目的列相对应的列标题值(因为这可能与表中的列名相同,或者用户在表标题和数据库表之间有一个映射)

以下是如何获取给定单元格的缝合线(列名值)

(注意,我将主键放在每行的第一列,并确保即使使用ColReorder的movable columns,iFixedColumns为1,也将该键保留在第一列。我的datatable由oTable引用。我假设我有cell DOM引用,我在下面称之为“target”):


这是我必须自己解决的问题,因为我使用JEditable允许用户编辑表中的单元格。

像这样一个简单的问题需要一个好的简单jQuery解决方案

假设您的id位于第0行 例如,在对第5行执行操作时,您希望访问它

$('td:eq(5)').click(function (){
    var id  = $(this).parent().find('td:eq(0)').html();
    alert('The id is ' + id);
});
注意,这也适用于过滤器和分页结果


我同意@fbas的观点,股票答案并没有真正的帮助。

这里有一个例子,说明如何在单击行后获取id

$('#example tbody tr').live('click', function() {
         var row = example .fnGetData(this);
         id=row['id'];//or row[0] depend of situation
         function(id);
});
如果需要表中的所有id,则必须使用如下代码:

$(exampleTable.fnGetNodes()).each(function() { 

    var aPos = exampleTable.fnGetPosition(this);
    var aData = exampleTable.fnGetData(aPos[0]);
    aData[aPos[0]] = this.cells[0].innerHTML; 

    IdSet.push(aData[aPos[0]]);
});
<!-- datatables initialisation -->
"rowCallback": function( row, data ) {
    setCrudLinks(row, data);                  
}

function setCrudLinks(row, data) {
    d = $(row).find('a.delete');
    d.attr('href', d.attr('href')+'/'+data[0]);
    e = $(row).find('a.edit');
    e.attr('href', e.attr('href')+'/'+data[0]);
}
希望有帮助

瓦罗
/* Get the rows which are currently selected */
function fnGetSelected(oTableLocal) {
    var aReturn = new Array();
    var aTrs = oTableLocal.fnGetNodes();

    for (var i = 0; i < aTrs.length; i++) {
        if ($(aTrs[i]).hasClass('row_selected')) {
            aReturn.push(aTrs[i]);
        }
    }
    // console.log( aReturn);
    return aReturn;
}

$(function() {

    /////////////////
    //btn_EditCustomer
    $('#btn_EditCustomer').on('click', function(e) {
        var anSelected = fnGetSelected(oTable);
        var rowData = oTable.fnGetData(anSelected[0]);
        console.log(rowData[0]);
    });
});  </script>
<!-- datatables initialisation -->
"rowCallback": function( row, data ) {
    setCrudLinks(row, data);                  
}

function setCrudLinks(row, data) {
    d = $(row).find('a.delete');
    d.attr('href', d.attr('href')+'/'+data[0]);
    e = $(row).find('a.edit');
    e.attr('href', e.attr('href')+'/'+data[0]);
}
table.MakeCellsEditable({
    "onUpdate": myCallbackFunction
});

myCallbackFunction = function (updatedCell, updatedRow) {
    var columnIndex = cell.index().column;
    var columnHeader = $(table.column(columnIndex).header()).html();
    console.log("The header  is: " + columnHeader);
    console.log("The new value for the cell is: " + updatedCell.data());
}