DataTables

DataTables Examples

by beginnerCF

HTML

<link rel="stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/css/jquery.dataTables.css">
<script src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/css/jquery.dataTables_themeroller.css "></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/jquery.dataTables.min.js"></script>
 <div id="divButton">
     <button class="toggle">Add Row</button>
</div>   
</br>
<table id="table1">
    <thead>
        <tr>
            <th>Rendering engine</th>
            <th>Platform(s)</th>
            <th>Engine version</th>
            <th>CSS grade</th>
            <th>CheckBox</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            
            <td>Win 95+</td>
            <td>5.5</td>
            <td>
                <input type="text" class="txtBox"></input>
            </td>
            <td>
                <input type="checkbox"></input>
            </td>
        </tr>
        <tr>
            <td>2</td>
           
            <td>Win 98+</td>
            <td>6</td>
            <td>
                <input type="text" class=" txtBox"></input>
            </td>
             <td>
                <input type="checkbox"></input>
            </td>
        </tr>
        <tr>
            <td>3</td>           
            <td>Win XP SP2+</td>
            <td>7</td>
            <td>
                <input type="text" class=" txtBox"></input>
            </td>
             <td>
                <input type="checkbox"></input>
            </td>
        </tr>
    </tbody>
</table>

CSS

.txtBox
{
    background-color:red;
}

JavaScript

$(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">';
    console.log(table1.fnSettings().fnRecordsTotal());
    $('#divButton').find('.toggle').on('click', function () {                 
         table1.fnAddData([table1.fnSettings().fnRecordsTotal() + 1, 'Windows 7',7.1, textbox, checkbox]);                          
          });
});