【问题标题】:jQuery datatable: Get class and id of edited celljQuery数据表:获取已编辑单元格的类和ID
【发布时间】:2017-01-04 04:36:02
【问题描述】:

我正在使用 jquery-datatables-editable (http://code.google.com/p/jquery-datatables-editable/) 来使用 jQuery 管理我的表,但是我需要做的是当我编辑一个单元格时,我想获取我想要的单元格的 id 和类刚刚编辑。我怎样才能做到这一点?这是我的代码的一部分:

//creating the table
var table = $('<table id="mytable" border="1" cellpadding="1" cellspacing="1"></table>');
var head = $('<thead><tr><th>Name</th><th>IP</th><th>Status</th><th>Last Seen</th><th>Pref. Smithers</th><th>Debug LvL</th></tr></thead>');
table.append(head);
var body = $('<tbody></tbody>');
for (var i = 0; i < x.length; i++) {
    //using xml to fill the cells
    var ip = x.item(i).getAttribute("id");
    var row = $('<tr id="' + ip + '"></tr>');
    var id = $('<td id="ip" class="' + ip + '"></td>');
    var ls = $('<td id="lastseen" class="' + ip + '"></td>');
    var nm = $('<td id="name" class="' + ip + '"></td>');
    var st = $('<td id="status" class="' + ip + '"></td>');
    var dblvl = $('<td id="defaultloglevel" class="' + ip + '"></td>');
    var prfsmth = $('<td id="preferedsmithers" class="' + ip + '"></td>');
    var lastseen = x.item(i).getElementsByTagName("lastseen")[0].childNodes[0].nodeValue;
    var name = x.item(i).getElementsByTagName("name")[0].childNodes[0].nodeValue;
    var status = x.item(i).getElementsByTagName("status")[0].childNodes[0].nodeValue;
    var defaultloglevel = x.item(i).getElementsByTagName("defaultloglevel")[0].childNodes[0].nodeValue;
    var preferedsmithers = x.item(i).getElementsByTagName("preferedsmithers")[0].childNodes[0].nodeValue;
    nm.html(name);
    id.html(ip);
    st.html(status);
    ls.html(lastseen);
    dblvl.html(defaultloglevel);
    prfsmth.html(preferedsmithers);
    row.append(nm);
    row.append(id);
    row.append(st);
    row.append(ls);
    row.append(prfsmth);
    row.append(dblvl);
    body.append(row);
}
table.append(body);

$('#service_table').append(table);


//initiating the editable datatable
var oTable = $('#mytable').dataTable().makeEditable({
    sUpdateURL: function(value, settings) {
        return (value); //Simulation of server-side response using a callback function
    },
    "aoColumns": [
        {
        indicator: 'Saving platforms...',
        tooltip: 'Click to edit platforms',
        type: 'textarea',
        submit: 'Save changes',
        fnOnCellUpdated: function(sStatus, sValue, settings) {
            alert("(Cell Callback): Cell is updated with value " + sName);
        }},
        null
        ,
    {
        indicator: 'Saving platforms...',
        tooltip: 'Click to edit platforms',
        type: 'textarea',
        submit: 'Save changes',
        fnOnCellUpdated: function(sStatus, sValue, settings) {
            //I WANT HERE TO BE ABLE TO ALERT() THE ID AND THE CLASS OF THE CELL THAT WAS JUST EDITED
            alert("(Cell Callback): Cell is updated with value " + sValue);
        }},
        null
        ,
    {
        indicator: 'Saving platforms...',
        tooltip: 'Click to edit platforms',
        type: 'textarea',
        submit: 'Save changes',
        fnOnCellUpdated: function(sStatus, sValue, settings) {
            alert("(Cell Callback): Cell is updated with value " + sValue);
        }},
    {
        indicator: 'Saving platforms...',
        tooltip: 'Click to edit platforms',
        type: 'textarea',
        submit: 'Save changes',
        fnOnCellUpdated: function(sStatus, sValue, settings) {
            alert("(Cell Callback): Cell is updated with value " + sValue);
        }}
    ]

});

【问题讨论】:

    标签: jquery datatable html-table


    【解决方案1】:

    我可以提供一些理论,没有确定的:

    这个

    this 可能在 fnOnCellUpdated 回调的范围内。指编辑后的td

    设置

    也许td 是作为settings 的属性传递给fnOnCellUpdated 回调的,但是数据表可编辑文档在定义settings 时未提供。您可以尝试探索它的属性。

    其他回调

    This page 表示sUpdateURL 选项将采用回调函数,但是您会遇到与fnOnCellUpdated 相同的问题 - 它的settings 参数未定义。

    This page 表示有一个fnOnEdited 回调,这可能很有用,但它的参数没有完全定义。

    【讨论】:

      【解决方案2】:

      默认的fnOnCellUpdated 函数是不可能的。但是,您可以在 jquery-datatables-editable 插件中进行简单的调整。

      我在 code.google 找到了一个issue,它提供了一个解决方案: 更改此行:

      settings.fnOnCellUpdated(status, sValue, settings);
      

      settings.fnOnCellUpdated(status, sValue, aPos[0], settings);
      

      这应该给你行的ID。

      因为我需要像这样从 html 中获取表格行的 ID:

      <tr id="46" class="odd">
          <td class="sorting_1">46</td>
          <td >0</td>
      </tr>
      

      我换行了

      settings.fnOnCellUpdated(status, sValue, settings);
      

      settings.fnOnCellUpdated( fnGetCellID(this), status, sValue, settings);
      

      当你调用函数时返回表格行的id:

      fnOnCellUpdated:function(id){}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-16
        • 1970-01-01
        相关资源
        最近更新 更多