使用JavaScript创建一个简单的表

时间:2017-10-25 09:02:55

标签: javascript

我主要使用Jquery和DataTables

创建一个表

我在拨打$('#dataTable').dataTable();时收到错误但我不知道为什么会发生这种情况,因为I see my table correctly displayed 但是DataTable脚本不起作用,所以表保持正常,而不是分页等......

这是错误:

  

jquery.dataTables.js:1197未捕获的TypeError:无法读取属性   ' MDATA'未定义的       在HTMLTableCellElement。 (jquery.dataTables.js:1197)       在Function.each(jquery-1.9.1.js:648)       在init.each(jquery-1.9.1.js:270)       在HTMLTableElement。 (jquery.dataTables.js:1194)       在Function.each(jquery-1.9.1.js:648)       在init.each(jquery-1.9.1.js:270)       在init.DataTable [as dataTable](jquery.dataTables.js:869)       在DisplayTable(accounts.js:19)       在TypesChanged(accounts.js:168)       在HTMLDocument。 (accounts.js:171)

HTML:

<div class="table-responsive">
    <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
        <thead class="dataTableHead">
        <tr>
            <%--JSON ARRAY CREATES THE TABLE--%>
        </tr>
        </thead>
        <tfoot class="dataTableFooter">
        <tr>
            <%--JSON ARRAY CREATES THE TABLE--%>
        </tr>
        </tfoot>
        <tbody class="dataTableBody">
        <%--JSON ARRAY CREATES THE TABLE--%>
        </tbody>
    </table>
</div>

的JavaScript

var types = [];
var amountOfDayEnds = parseInt($('#amountOfDayEnds').val());

function TypesChanged() {
    selectedGroup = $('#group').val();
    switch (selectedGroup) {
        case "DDA":
            types = ["400", "4044", "4045"];
            break;
        case "SAV":
            types = ["300", "310"];
            break;
        case "MTG":
            types = ["700", "710"];
            break;
    }
    console.log("Selected group: " + selectedGroup + ", Types: " + types);
    $('.dataTableHead').empty();
    $('.dataTableFooter').empty();
    $('.dataTableHead').append('<th class="text-center">Day</th>');
    $('.dataTableFooter').append('<th class="text-center">Day</th>');
    var columnNames = [];
    $.each(types, function (index, value) {
        columnNames += '<th class="text-center">Type ' + value + '</th>';
    });
    $('.dataTableHead').append(columnNames);
    $('.dataTableFooter').append(columnNames);
    DisplayTable();
}

TypesChanged();

function DisplayTable() {
    var data;
    //Table
    $('.dataTableBody').empty();
    for (var i = 1; i <= amountOfDayEnds; i++) {
        data += '<tr align="center">';
        data += '<td>' + i + '</td>';
        $.each(types, function (index, value) {
            data += '<td>' + '<input class="text-center amountOfAccounts" type="number" value="0" name="amountOfAccounts-' + value + '"  data-error="Please, insert a value"  required></td>'
        });
        data += '</tr>';
    }
    $('.dataTableBody').append(data);

    $('#dataTable').dataTable();
}

0 个答案:

没有答案