简体   繁体   English

jQuery UI数据表:将行添加到以文本框为列的表中

[英]Jquery UI Datatable: Add row to a table with textbox as column

I am trying to add a row to a datatable with no luck with the texbox and checkbox as columns. 我正在尝试将没有一行的数据添加到数据表中,而texbox和checkbox作为列。 I am able to add the other columns easily but the one with textbox and checkbox don't have any idea on how to do that. 我能够轻松添加其他列,但带有文本框和复选框的列不知道如何执行此操作。 The textbox and checkbox ids are evaluated appending their type with first column value. 评估文本框和复选框ID,并将其类型附加第一列值。 I am wondering what will be good way to achieve this? 我想知道什么是实现这一目标的好方法? I'm not looking to clone a row since there may be scenarios where my table may not have any data. 我不希望克隆行,因为在某些情况下我的表可能没有任何数据。 Any ideas? 有任何想法吗?

Here is the client side code: 这是客户端代码:

$(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]);                          
          });
});

Here's the JSFiddle 这是JSFiddle

You can try this: 您可以尝试以下方法:

$(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]);                          
    });
});

Here is the FIDDLE . 这是FIDDLE

You should use column rendering for this, not fnCreatedRow / createdRow . 您应该为此使用列渲染 ,而不是fnCreatedRow / createdRow Here some examples of how to insert various <form> <input> elements : 下面是一些有关如何插入各种<form> <input>元素的示例:

...    
aoColumnDefs : [
   { aTargets : [1],
     mRender : function(data, type, full) {
        return '<input type="text" value="'+data+'"/>'
     }    
   },
   { aTargets : [2],
     mRender : function(data, type, full) {
        return '<input type="checkbox" checked="checked"/>'
     }    
   },
   { aTargets : [3],
     mRender : function(data, type, full) {
        return '<textarea>'+data+'</textarea>'
     }    
   }
]

have used the "oldschool" hungarian notation, it works both with 1.9.x and 1.10.x versions of datatables. 曾经使用过“匈牙利语”匈牙利表示法,它适用于1.9.x和1.10.x版本的数据表。 A small demo (just added aoColumnDefs to an existing demo, it does not give any much sense besides the demonstration) -> http://jsfiddle.net/f6fqa641/ 一个小型演示(只是将aoColumnDefs添加到现有演示中,除了演示之外没有其他意义)-> http://jsfiddle.net/f6fqa641/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM