【发布时间】:2016-03-22 07:20:35
【问题描述】:
在我的项目中,我正在从服务器端将数据加载到数据表中。第一次加载工作正常。但是当我更改 <select> <option> 值时,它给了我一个错误。
我有一个<select> <option> 面板。因此,一旦我更改了选项,我需要删除当前内容并将不同的内容加载到表格中。我正在从 ajax 调用中获取数据并加载到表中。第二次更改选项时,它给了我这个错误。
DataTables 警告:table id=example - 无法重新初始化 DataTable。有关此错误的更多信息,请参阅http://datatables.net/tn/3
我检查了给定的 URL,但对我来说没有很好的声音。
这是我的 HTML,
<table id="example" class="table table-striped table-bordered nowrap" cellspacing="0" width="100%" style="background:#f3f3f3">
<thead>
<tr>
<th><div class="heading">Title 01</div></th>
<th><div class="heading">Title 02</div></th>
<th><div class="heading">Title 03</div></th>
<th><div class="heading">Title 04</div></th>
<th><div class="heading">Title 06</div></th>
<th><div class="heading">Title 07</div></th>
</tr>
</thead>
<tbody></tbody>
</table>
这里是选择选项下拉菜单
<select name="exampleSelect" id="exampleSelect" class="exampleSelect">
<option id="exam1">value</option>
<option id="exam2">value</option>
<option id="exam3">value</option>
<option id="exam4">value</option>
<option id="exam5">value</option>
<option id="exam6">value</option>
<option id="exam7">value</option>
</select>
这是我的更改函数,
$('select#exampleSelect').on('change', function() {
var sId = $(this).children(":selected").attr('id');
loadedData(sId);
});
这是我的loadedData函数
function loadedData(sId) {
var table = $('#example').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "ajaxdata.json",
"type": "POST"
},
"sScrollY": "300px",
"scrollX":true,
"paging": false,
"bFilter": false,
"bInfo": false,
"searching": false,
"bSort" : false,
"fixedColumns": true
});
}
这里根据“sId”,加载数据是变化的
这是我的 JSON 数据,
{
"draw": 1,
"recordsTotal": 57,
"recordsFiltered": 57,
"data": [
["Airi", "Satou", "Accountant", "Tokyo", "28th Nov 08", "$162,700"],
["Angelica", "Ramos", "Chief Executive Officer (CEO)", "London", "9th Oct 09", "$1,200,000"],
["Ashton", "Cox", "Junior Technical Author", "San Francisco", "12th Jan 09", "$86,000"],
["Bradley", "Greer", "Software Engineer", "London", "13th Oct 12", "$132,000"],
["Brenden", "Wagner", "Software Engineer", "San Francisco", "7th Jun 11", "$206,850"],
["Brielle", "Williamson", "Integration Specialist", "New York", "2nd Dec 12", "$372,000"]
]
}
我需要根据选择选项的变化值将相关数据集附加到tbody。
有没有人有这方面的经验来解决这个问题..?
【问题讨论】:
-
对于所有
sId,您都在查询相同的服务,对吗? -
@Cerlin Boss 是的,但数据不同。
-
附带说明,请尝试仅在选择时使用 id,例如
$('#exampleSelect').on
标签: jquery datatables html-table