Javascript使用索引和数据将数据插入单元格

时间:2018-03-23 23:23:10

标签: javascript jquery html html-table cells

这看起来很简单,但我无法理解它。我试图插入我得到的数据并替换指定单元格索引处的单元格内容。我从函数调用的输入参数中获取数据(Kevin)和索引(0)。但我只是无法将此数据插入所需的单元格。我已经尝试了insertCell函数,但这不会替换内容,它只会继续添加新单元格。

我的console.logs报告了正确的索引和数据,我尝试插入/替换单元格的内容,如下所示:td[index].innerHTML = data但这会产生错误,显然td[index]会返回object HTMLTableCellElement 1}},所以我不能这样做。

HTML:

      <table>
            ...
            <tbody>
                <tr id="data-row-body">
                    <td></td> //need to insert here
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
            </tbody>
        </table>

Javscript:

function exeAPI(api, key, index, callback) {
$.ajax({
      url:"../lib/getData.php",
      type: "POST",
      data: {
          name: name,
          api: api
      },
      dataType: "text", 
      success:function(result){
          console.log(result);
        if(result){
            try {
                var jsonText = result.substr(result.indexOf('{'));
                console.log("this is the jsontext: " + jsonText);  
                var jsonObj = JSON.parse(jsonText);

                callback(jsonObj.response.result[key], index);
            } catch(e) {
                console.log(e);
                alert("Error getting rack data!");
            }
        }
      }
});

//getting back "Kevin" and "0" as parameters to insert here

function exeAPIcallback(data, index){
    var td = $('#data-table tbody td');
    var tr = $('#data-table tbody tr');
    var row = document.getElementById("data-row-body");
    console.log("This is the cell index: " + th[index].innerHTML + " ,index: " + th[index]);
    console.log("This is the cell index: " + td[index]);

    td[index].innerHTML = data; // this doesn't work

}



function popupParamChecker(){

exeAPI("getData.php", "Kevin", 0, exeAPIcallback);

$("#dialog-params").dialog({
    resizable: false,
    width: 1000,
    modal: true,
    draggable: false,
    buttons: {
        Confirm: function() {
            $( this ).dialog( "close" );
        }
    }
});
}

有更简单的方法吗?

4 个答案:

答案 0 :(得分:1)

行数可能很多,所以我为你的APICallback添加了一个参数。

以下是代码:

1。首先按$('table tbody tr')

选择表格下的所有行

2。使用.eq(index)获取特定行

3. .find()所有tds然后获取特定单元格来更改文本。

function exeAPIcallback1(data, row, col){
  let td = $('table tbody tr').eq(row).find('td').eq(col);
  td.text(td.text()+data)
}

function exeAPIcallback2(data, row, col){
  $('table tbody tr').eq(row).find('td').eq(col).text(data);
}
table td{
  width:10px;
  height:10px;
  border:1px;
  background-color:yellow;
}

table td:nth-child(even){
  background-color:red
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="exeAPIcallback1('test',0,0)"> Test Insert</button>
<button onclick="exeAPIcallback2('test',0,0)"> Test Replace</button>
<table>
    <tbody>
        <tr id="data-row-body">
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </tbody>
</table>

答案 1 :(得分:1)

使用以下功能将内容添加到表格单元格。

function add_content_to_cell(table_selector, row_number, cell_number, content) {
    $(table_selector).find('tr')
    .eq(row_number-1).find('td').eq(cell_number-1)
    .append(content); 
    }

只需选择表格ID,行号,单元格编号和您要添加的内容即可。

<强>用法

add_content_to_cell('#my_table', 2, 5, "I am some content")

<强>结果

enter image description here

答案 2 :(得分:0)

使用jQuery:

$('#data-row-body').find('td').eq(index).text(data); /* strips down html, if any */

OR

$('#data-row-body').find('td').eq(index).html(data); /* with html tags */

答案 3 :(得分:0)

使用您当前的代码段

查看是否有效

变化

var td = $('#data-table tbody td');

var td = $('#data-row-body').children('td');
$(td[index]).html(data);

这里假设索引总是在

的范围内