Javascript 未捕获的类型错误:table.search(...).draw 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29825455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: table.search(...).draw is not a function
提问by Tone
Added filtering to a jQuery DataTables plugin, and it is not working very well.
向 jQuery DataTables 插件添加了过滤功能,但效果不佳。
I want to have two links that will search for records on specific search words. To figure out how to do that I first tried to use this example. It uses an input field to search for values in the table. It generates this error:
我想要两个链接来搜索特定搜索词的记录。为了弄清楚如何做到这一点,我首先尝试使用这个例子。它使用输入字段来搜索表中的值。它生成此错误:
Uncaught TypeError: table.search(...).draw is not a function
未捕获的类型错误:table.search(...).draw 不是函数
My code:
我的代码:
$(document).ready(function() {
$('#store-list').dataTable({
"sPaginationType": "full_numbers"
});
var table = $('#store-list').DataTable();
$('#myFilter').on( 'keyup', function () {
table
.search( this.value )
.draw();
} );
});
I have tried different things to make this work:
我尝试了不同的方法来完成这项工作:
Swapped
.DataTable()with.dataTable().api()and.dataTable()Tried
( this.val() )and( $('#myFilter').val() )(link)Tried
table.search( this.value ).draw;(without())In desperation I tried without
searchand then withoutdraw
.DataTable()与.dataTable().api()和交换.dataTable()尝试
( this.val() )和( $('#myFilter').val() )(链接)尝试过
table.search( this.value ).draw;(没有())在绝望中,我尝试没有
search然后没有draw
Can someone please help me find the error?
有人可以帮我找出错误吗?
回答by Gyrocode.com
CAUSE
原因
You're using DataTables plug-in 1.9.4 but API methods and example for newer 1.10.x release.
您使用的是 DataTables 插件 1.9.4,但 API 方法和示例适用于较新的 1.10.x 版本。
API methods have changed when DataTables plug-in was updated to 1.10 version, see Converting parameter names for 1.10for details.
当 DataTables 插件更新到 1.10 版本时,API 方法发生了变化,有关详细信息,请参阅转换 1.10 的参数名称。
SOLUTION #1
解决方案#1
Upgrade your DataTables library to version 1.10 to use search()API method.
将您的 DataTables 库升级到 1.10 版以使用search()API 方法。
SOLUTION #2
解决方案#2
If you cannot upgrade to version 1.10 for some reason, use the code below. There is similar example for version 1.9 , see DataTables individual column filtering example.
如果由于某种原因无法升级到 1.10 版,请使用下面的代码。版本 1.9 有类似的示例,请参阅DataTables 单个列过滤示例。
For DataTables 1.9
对于数据表1.9
$(document).ready(function(){
$('#store-list').dataTable({
"sPaginationType": "full_numbers"
});
$("#myFilter").on('keyup', function (){
$('#store-list').dataTable().fnFilter(this.value);
});
});
See fnFilterAPI reference for additional optional parameters.
有关fnFilter其他可选参数,请参阅API 参考。
回答by daniel
This work for me:
这对我有用:
var table = $('#campaniasVinculadas').DataTable();
$('#myFilters input').on( 'keyup', function () {
table
.search( this.value )
.draw();
});
I use the selector '#myFilters input'because the id "#myFilters"for Tfoot doesn't have a "value" attribute, but "input" has the value attribute.
我使用选择器'#myFilters input'是因为"#myFilters"Tfoot的 id没有“value”属性,但“input”有 value 属性。
回答by Lalit Mohan
Just Make sure with naming conventions
只需确保命名约定
If you are using the remote datable Initialize the data-table with the following syntax
如果您使用的是远程数据表,请使用以下语法初始化数据表
var table = $('#store-list').DataTable();
instead of
代替
var table = $('#store-list').dataTable();
console the variable table
控制台变量 table
console.log(table)
It will show you all the remote accessible properties
它将向您显示所有可远程访问的属性
$: ? () ajax: {dt_wrapper: true, json: ?, params: ?, reload: ?, url: ?} cell: ? () cells: ? () clear: ? () column: ? () columns: ? () context: [{…}] data: ? () destroy: ? () draw: ? () i18n: ? () init: ? () off: ? () on: ? () one: ? () order: ? () page: ? () row: ? () rows: ? () search: ? () selector: {rows: null, cols: null, opts: null} settings: ? () state: ? () table: ? () tables: ? () __proto: Object(0)
$:?() ajax: { dt_wrapper: true, json: ?, params: ?, reload: ?, url: ?} cell: ? () 细胞: ?() 清除: ?() 柱子: ?() 列: ?() 上下文:[{…}] 数据:?() 破坏: ?() 画: ?() i18n: ? () 在里面: ?() 离开: ?() 在: ?() 一: ?() 命令: ?() 页: ?() 排: ?() 行: ? () 搜索: ?() 选择器: {rows: null, cols: null, opts: null} 设置: ? () 状态: ?() 桌子: ?() 表: ? () __proto: 对象(0)
dataTablewill work without any issue if you are using a client data-table
dataTable如果您使用的是客户端数据表,则可以正常工作

