微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何仅在Jquery dataTables中编辑选定行的特定列值

如何解决如何仅在Jquery dataTables中编辑选定行的特定列值

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

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.

先谢谢了。保持安全:)

解决方法

假设数量是第三列。

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

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

ps。我不确定GenerateDraftInvoice的按钮的位置,但是如果将其附加到行,则代码将更改为

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。