【问题标题】:How To Edit a Specfic Column Value of a Selected Row ONLY in Jquery dataTables如何仅在 Jquery dataTables 中编辑选定行的特定列值
【发布时间】:2020-10-15 05:03:50
【问题描述】:

我有一个包含以下列的数据表

var t = $('#myDataTable').DataTable({
    
    "scrollX": true,
    "scrollY": true,
 
    columnDefs: [
        {//ProductName
            targets: 0, className: 'text-left'
        },
        {//Code
            targets: 1, className: 'text-center'
        },
        {//Quantity
            "render": $.fn.dataTable.render.number(',', '.', 4), "targets": 2, className:'text-right'
        }
    ]
});

用户可以使用以下代码通过单击行上的任意位置来选择行。

$('#myDataTable tbody').on('click', 'tr', function () {
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
}
else {
t.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
});

我想要实现的是,当用户单击“生成发票草稿”(如附件所示)时,数量列中的值将从 1234 更新。仅在所选行中。不是所有的行。

这是我试图做的,但它不起作用:(

$('#GenerateDraftInvoice').click(function () {
    t.row('.selected').$(this).parents('tr').data()[4].text('1234');
});

注意:为了便于理解,我已经缩短了代码。When The User Clicks on The Generate Draft Invoice Button as show in the picture. The quantity value in the selected row (RowNo: 2) will get change to 1234. I have shortened my code for easy understanding of the reviewer.

提前致谢。保持安全:)

【问题讨论】:

    标签: jquery asp.net-mvc datatables


    【解决方案1】:

    假设数量是第 3 列。

    $('#GenerateDraftInvoice').click(function () {
        $('tr.selected').children().eq(2).text('1234');
    });
    

    .eq()0 开始,.data() 使用 data-modal 之类的属性

    ps.Im 不确定 GenerateDraftInvoice 按钮的位置,但如果它附加到行,代码将更改为

    $('#GenerateDraftInvoice').click(function () {
        $(this).parent().siblings().eq(2).text('1234');
    });
    

    【讨论】:

    • 非常感谢 Pakawat,你拯救了我的一天,我花了很多时间研究谷歌和其他来源,但找不到这个解决方案。无论如何;我对 DataTables 有更多疑问,我想知道您或您认识的人是否可以教我,请让我知道有人感兴趣。
    猜你喜欢
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    相关资源
    最近更新 更多