您可以使用来过滤数据表。有一件事你需要做的是你必须将数据表searching
属性设置为true
。当你点击相应的div时,我创建了一个示例来过滤数据表。仅供参考检查这个Fiddle
HTML
<table id="dataTable" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Category</th>
</tr>
</thead>
<tbody>
</tbody>
CSS
.search {
border-radius: 2px;
background: #73AD21;
margin-bottom : 10px;
width: 100px;
height: 20px;
}
JS
$(document).ready(function() {
var table = null;
function createDatatable() {
table = $('#dataTable').dataTable({
bFilter: false,
bLengthChange: false,
searching : true,
"sDom": 'lfrtip',
pagingType: 'full',
"oLanguage": {
"oPaginate": {
"sFirst": "<b><<</b>",
"sLast": "<b>>></b>",
"sNext": "<b>></b>",
"sPrevious": "<b><</b>"
}
}
});
}
createDatatable();
//creating a temp json. you will get this from server side after ajax call
var jsonString = '[{"Id": 1,"Name": "Sony Vaio","Category": "Laptops"},{"Id": 2,"Name": "Samsung Galaxy","Category": "Mobile Phones"},{"Id": 3,"Name": "Dell","Category": "Laptops"}]';
var data = JSON.parse(jsonString);
for(i=0; i<data.length;i++) {
$('#dataTable').dataTable().fnAddData([
data[i].Id,
data[i].Name,
data[i].Category
]);
}
$(document).on('click','.search',function(e)
{
var search = $(this).text();
table.fnFilter(search.trim());
});
});
请描述你正在尝试做什么。目前还不清楚你想要的行为是什么 – Praveen
@Praveen我试图搜索div id搜索内的文本。 – Steve