javascript - Jquery UI 数据表 : Add row to a table with textbox as column

标签 javascript c# jquery jquery-ui datatables

我正在尝试向数据表中添加一行,但没有将 texbox 和 checkbox 作为列。我可以轻松添加其他列,但是带有文本框和复选框的列不知道如何添加。文本框和复选框 ID 的评估是将它们的类型附加到第一列值。我想知道实现这一目标的好方法是什么?我不想克隆一行,因为在某些情况下我的表可能没有任何数据。有什么想法吗?

这是客户端代码:

$(function () {
    var tableOpts = {
        "sPaginationType": "full_numbers",
        "sScrollY": "150px",
        "bFilter": false,
        "fnCreatedRow": function (nRow, aData, iDataIndex) {
            $(nRow).attr('id', aData[0]);          

            var txtBox = $(nRow).find("input[type=text]");   
            txtBox.attr('id', 'text-' + aData[0]);

            var checkBox = $(nRow).find("input[type=checkbox]");            
            checkBox.attr('id', 'check-' + aData[0]);
        }
    }   
    var table1 = $('#table1').dataTable(tableOpts);        
    $('#divButton').find('.toggle').on('click', function () {                 
         table1.fnAddData([4, 'Windows 7',7.1]);                          
          });
});

这是 JSFiddle

最佳答案

你可以试试这个:

$(function () {
    var tableOpts = {
        "sPaginationType": "full_numbers",
        "sScrollY": "150px",
        "bFilter": false,
        "fnCreatedRow": function (nRow, aData, iDataIndex) {
            $(nRow).attr('id', aData[0]);          

            var txtBox = $(nRow).find("input[type=text]");   
            txtBox.attr('id', 'text-' + aData[0]);

            var checkBox = $(nRow).find("input[type=checkbox]");            
            checkBox.attr('id', 'check-' + aData[0]);
        }
    }   
    var table1 = $('#table1').dataTable(tableOpts); 
    var textbox = '<input type="text" class="txtBox">';
    var checkbox = '<input type="checkbox">';
    $('#divButton').find('.toggle').on('click', function () {                 
       table1.fnAddData([table1.fnSettings().fnRecordsTotal() + 1, 'Windows 7',7.1, textbox, checkbox]);                          
    });
});

这是 FIDDLE .

关于javascript - Jquery UI 数据表 : Add row to a table with textbox as column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32810100/

相关文章:

javascript - 如何使用高级自定义字段插件在 WP 菜单中添加 onClick?

JavaScript 切换显示

javascript - 删除 HTML 元素上的事件

jquery - 为什么 jQuery.parseJSON 不是一个函数?

javascript - 使用 ajax 解析 CSV 并在某些列上执行异步任务

jquery - Safari 在使用 jQuery Masonry 时忽略边距

javascript - 响应式图像 map 卷展重新定位

c# - 用 List<T> 解构

c# - 为什么两个新对象没有相同的哈希码?

c# - 数据表左加入 c#