【问题标题】:Get the id of the HTML table column获取 HTML 表格列的 id
【发布时间】:2013-06-11 03:44:20
【问题描述】:

如何获取特定行单元格索引的列 ID?

HTML 表格设计:My Table

当表格的单元格内容被放入另一个单元格时,此代码处理和 Ajax 请求:

redipsInit = function () {
// reference to the REDIPS.drag lib
var rd = REDIPS.drag;
// initialization
rd.init();
// dragged elements can be placed to the empty cells only
rd.dropMode = 'single';
// define dropped handler
//the parameter targetCell is the reference of the cell where the content has been         
//dropped
rd.event.dropped = function (targetCell) {

    var tbl = rd.findParent('TABLE', targetCell); //reference to my table

    //getting the tabindex attribute that in my case represent the row index
    var cardPosition = targetCell.attributes[0].value;

    // the cell content (the div tag with class = "drag t1") has an ID that reprensents
    // the content ID in my DB.
    var cardIdBeingDragged = targetCell.firstElementChild.id;

    var columnID = "";
    var parametros = {
        "Card_Position": cardPosition,
        "Card_ID": cardIdBeingDragged,
        "Column_ID" : ??? // still dont know how to get it.
    };
    $.ajax({
        type: 'POST',
        url: "../../Contenido/Board.aspx/SaveCardPosition",
        data: JSON.stringify(parametros),
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });   
};
};

好的,我会做一个场景来理解我想要什么。

当单元格内容从一个单元格拖放到另一个单元格时,我能够处理上面的 javascript 代码。
下一步是获取 3 个值参数以在代码后面 (ASP.Net) 中发送到我的 webMethod(SaveCardPosition)

我能够获得:
CardId(Cell ContentId)
targetCell 所属的行索引(Card_Position)

但是我不知道如何获取这个targetCell所属的列id。

我怎样才能做到这一点?

【问题讨论】:

  • 我认为targetCell.id应该给出目标单元格ID或者试试$(targetCell).parent().closest('td').attr('id')
  • 是的 targetCell.id 给了我单元格 ID,但我想要该单元格所属的列 ID。

标签: jquery html asp.net redips.drag


【解决方案1】:

当给定参数与您想要的单元格的值或任何其他属性匹配时,我将遍历该行并返回一个索引,然后您就会知道它是哪一列,因此它就是列 ID。

【讨论】:

    【解决方案2】:

    您可以将数据属性放在指定列的单元格中...

    【讨论】:

    • 我可以做到,这是一个好主意,但它不符合我的需要,因为如果列单元格没有卡片(内容),则表示该卡片不存在于数据库中,因此单元格内容为空白。这很难解释,所以我想知道另一种选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 2017-10-27
    相关资源
    最近更新 更多