无法使用jquery数据表在DataTable中添加行

时间:2017-06-14 08:29:16

标签: javascript jquery datatable

我在View中有两个表。第一个'menuTable'和另一个'confirmTable'。当我从'menuTable'中单击一行中的按钮时,'confirmTable'中的一行将添加测试数据。我有一个参考:

<script src="~/Content/js/dataTables.bootstrap.min.js"></script>
<script src="~/Content/js/jquery.dataTables.min.js"></script>

我对'confirmTable'的看法:

<table id="confirmTable" class="table table-bordered table-striped dataTable">
    <thead>
        <tr role="row">
            <th class="sorting_desc" width="180px" rowspan="1" colspan="1" tabindex="0">Menu Item Name</th>
            <th class="sorting" rowspan="1" colspan="1" tabindex="1" width="220px;" aria-label="Description: activate to sort column ascending">Description</th>
            <th class="sorting" rowspan="1" colspan="1" tabindex="2" width="220px;">Rate </th>
            <th class="sorting" rowspan="1" colspan="1" tabindex="3" width="220px;">Quantity</th>
            <th class="sorting" rowspan="1" colspan="1" width="220px;">Total</th>
            <th class="sorting" rowspan="1" colspan="1" tabindex="4" width="220px;">Action</th>
        </tr>
    </thead>
    <tbody>
        <!--add row after clicking on menu items-->
    </tbody>
</table>

点击“添加”按钮后,调用“addToCurrentOrders()”函数,函数为:

function addToCurrentOrders() {

    //add to confirm table
    var currentRowNumber = $('#confirmTable').DataTable().data().length;
    var newRow = new Array(6);
    newRow[0] = "<input type='text' value='1' />";

    newRow[1] = "<input type='text' value='2' />";
    newRow[2] = "<input type='text' value='3' />";
    newRow[3] = "<input type='text' value='4' />";
    newRow[4] = "<input type='text' value='5' />";

    // Embed hidden fields in actionBtnTd
    var actionBtnsString = "<input type='text' id='inputMenuItemID' name='data' value='10'/>";
    newRow[5] = actionBtnsString;
    $('#confirmTable').DataTable().row(newRow).add.draw();

}

当我检查时,我收到以下错误消息:

Uncaught Error: Syntax error, unrecognized expression: <input type='text'  value='1'/> 

1 个答案:

答案 0 :(得分:0)

尝试将$('#confirmTable').DataTable().row(newRow).add.draw();更改为以下。

$('#confirmTable').DataTable().row.add(newRow).draw();