【问题标题】:How to Get cell values from data table ? Cell values如何从数据表中获取单元格值?单元格值
【发布时间】:2020-08-28 07:17:57
【问题描述】:

我在数据表单元格中使用复选框,并在选中一个或多个复选框后,显示添加按钮以进行更改。但我的问题是在选中复选框后我无法从单元格中获取值。如果我可以获取选中复选框的值,那么我可以将它们发送到数据表进行必要的更改

<td class="denetleme">
    <div class="vs-checkbox-con vs-checkbox-success">
        <input class="selectAll_1 selectinput" type="checkbox" value="true" >
        <span class="vs-checkbox">
            <span class="vs-checkbox--check">
                <i class="vs-icon feather icon-check"></i>
            </span>
        </span>
    </div>
</td>

如果在一行中选中了一个或多个复选框,则将这些复选框和列 id 也带上

enter image description here

【问题讨论】:

标签: javascript html jquery datatable datatables


【解决方案1】:

.selectinput可以获取单元格的全部数据

$('#mytable .selectinput').each(function() {
  alert($(this).html());
});

【讨论】:

  • 这将正常工作 - 直到 DataTables 开始使用分页(例如,显示 1,000 条记录中的 10 条)。然后,它只会看到显示行的复选框。
  • $('#saveBtn').click(function () { ``当我们点击保存按钮时,我需要选中复选框值及其行ID ``});
【解决方案2】:

    $('#saveBtn').click(function () {
 
        var dataArr = [];
        $.each($("#genelTablo tr.selected td.checkElement input.selectinput"), function () { //get each tr which has selected class

            
            var getColumnOfTd = $(this).closest("td")
            
            console.log("column", table.cell(getColumnOfTd).index().column, table.cell(getColumnOfTd).index().row)
            dataArr.push({
                columnName: $(this).closest("tr").find("td:first-child").text(),
                columnId: table.cell(getColumnOfTd).index().column,
                rowId: table.cell(getColumnOfTd).index().row,
                inputValue: $(this).val()
            })
 
            //dataArr.push($(this).find('td.checkElement input').val()); //find its first td and push the value
            //dataArr.push($(this).find('td:first').text()); You can use this too
        });
        console.log("here",dataArr);

 
 
    });

【讨论】: