描述(Description)
DataTables缓存数据是用来做搜索和排序,是为了让搜索和排序更快的的反应。有时候用这个方法获取DataTables已经为这些操作缓存好的数据是非常有用的,比如当创建一个select
列表来提供基于列的过滤器。
缓存数据并不能保证在任何特定的时候都可用。如果DataTables没有请求数据,那么就不会被缓存。特别是当你使用
orderString
选项,列的排序还没有执行,数据就不会被缓存。数据失效也会导致缓存被移移除。
应当注意的是,这个方法是必须的,因为Datatables针对不同操作(搜索、排序、显示等)使用不同的数据。具体参考
columns.dataOption
和
columns.renderOption
可以获取更多信息。
cells().data()API
方法可以获取单元格里的原始数据。如果你不为DataTables的不同操作使用数据和显示分离,这个方法则没什么意义。
注意,这个方法主要为开发DataTables插件的作者提供操作内部数据的能力。
类型(Type)
这个选项能够接受以下类型的参数:
function cells().cache( [type] )
描述(Description):
获得指定缓存类型的缓存数据
参数(Parameters):
名称(Name) | 类型(Type) | 是否可选(Optional) | |
---|---|---|---|
1 | type |
stringType
|
No |
指定数据应该从哪个缓存器中读取。可以使用两个值
orderString
和
searchString
。如果没有指定值,默认指定order 。
|
返回(Returns):
DataTables API 实例,结果集包含被选择单元格缓存数据
例子(Example)
给第一列构建一个过滤列表
var table = $('#example').DataTable({
columns:[
{"data":"time",render:function(data,type,row,meta){
if(type == "display"){
return data;
}
if(type == "filter"){
return timeFilterFormat(data);
}
if(type == "sort"){
return timeOrderFormat(data);
}
return data;
}},
{"data":"date",render:function(data,type,row,meta){
if(type == "display"){
return data;
}
if(type == "filter"){
return dateFilterFormat(data);
}
if(type == "sort"){
return dateOrderFormat(data);
}
return data;
}}
]
});
//模拟时间转换
function timeOrderFormat(time){
return 1234567890;
}
function timeFilterFormat(time){
return "2020年7月4日"+time;
}
function dateOrderFormat(date){
return "12345670000";
}
function dateFilterFormat(date){
return "2020年7月4日";
}
// Create the select list and search operation
var select = $('<select />')
.appendTo( 'body' )
.on( 'change', function () {
table
.column( 0 )
.search( $(this).val() )
.draw();
} );
// Get the search data for the first column and add to the select list
var data = table
.cells( '', 0 )
.cache( 'search' )
.sort()
.unique()
.each( function ( d ) {
select.append( $('<option value="'+d+'">'+d+'</option>') );
} );
<table id="example" class="display">
<thead>
<tr>
<th>时间</th>
<th>日期</th>
</tr>
</thead>
<tbody>
<tr>
<td>00:04:06</td>
<td>2020年7月4日</td>
</tr>
<tr>
<td>00:04:34</td>
<td>2020年7月6日</td>
</tr>
</tbody>
</table>
相关属性(Related)
下面的选项是直接相关的,也可能是您的应用程序的开发非常有用。
API