您可以使用來過濾數據表。有一件事你需要做的是你必須將數據表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