【发布时间】:2019-06-30 19:47:55
【问题描述】:
我有一个带有表格的页面:
<table id="myTable">
<thead>
<tr>
<th>Description</th>
<th>Qty</th>
<th>Cost</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
@Html.EditorFor(model => item);
}
</tbody>
</table>
然后我像这样初始化表格:
$("#myTable").tabulator({
layout: "fitColumns",
addRowPos: "bottom",
columns: [
{ title: "Description", field: "Description", editor: "input" },
{ title: "Qty", field: "Quantity", sorter: "number", editor: "number", editorParams: { min: 0, step: 1, } },
{ title: "Cost", field: "Cost", sorter: "number", editor: "number", editorParams: { min: 0, step: 0.1, }, align: "right", formatterParams: { decimal: '.', thousand: '.', symbol: "R" }, bottomCalc: "sum", bottomCalcFormatter: "money", bottomCalcFormatterParams: { decimal: '.', thousand: '.', symbol: "R" } },
{ formatter: "buttonCross", width: 30, align: "center", cellClick: function (e, cell) { cell.getRow().delete(); } },
],
});
这一切都很好,但我尝试添加:
$("#add-row").click(function () {
$("#myTable").tabulator("addRow", {}, true);
});
单击我的按钮添加新行会导致:错误:无法在初始化之前调用制表符上的方法;试图调用方法'addRow'
我尝试这样做:var table = $("#myTable").tabulator... 和 table.addRow({}) 但随后我收到一条错误消息,提示 addRow 不是函数。
它应该像这样工作吗?
【问题讨论】:
-
你能提供一些脚本细节来重现这个问题吗?我尝试在 this fiddle 中使用标准 Tabulator 和 jQuery 包装器,但控制台不断抛出
$(...).tabulator is not a function。 -
我按照here 的说明先尝试了CDN,但这也给了我
$(...).tabulator is not a function。我最终下载了库。在你的小提琴上使用 CDN 仍然会出现错误(不知道我是否正确加载它) PS。在本地,我引用 tabulator.js、jquery_wrapper.js 和 bootstrap css。 -
该表位于 PartialView 中。部分视图引用了我的脚本文件,其中包含初始化代码以及按钮单击代码。在我尝试引用制表符之前,这一切都很好。 DOM 似乎“忘记”了它的存在。将初始化代码移动到包含视图并使用制表符“内置”回调,我设法让它工作。我想将初始化代码保留在它自己的脚本文件中,链接到部分视图,但除非有办法绕过 DOM“忘记”,否则我将不得不接受我所得到的。
标签: asp.net-mvc tabulator