2016-03-23 21 views
1

我有一個擁有所有者名稱的表。用戶希望能夠從病房的第一個字母對錶格進行分類。使用數據表我想用首字母搜索

例子:

1)SAURABH Gujarani 2)Testsa

通過搜索 「SA」 應該只有列表 - SAURABH Gujarani

但在我目前的代碼也同時顯示。

<script type="text/javascript"> 
$(document).ready(function() { 
    $('#custom_2').dataTable({ 
     "aoColumnDefs": [ 
    { "bSearchable": false, "aTargets": [ 0,1,4,5,6,7,8 ] } 
] }); 
}); 
</script> 

回答

2

您可以按如下方式修改DataTable的默認搜索功能。

//Declare table 
var table = $('Table Selector').DataTable(); 

//on search box keyup 
$('input[type = search]').on('keyup', function() { 

    //start check from first character 
    table.search('^' + this.value, true, false).draw(); 
}); 

請參閱工作演示https://jsfiddle.net/Prakash_Thete/b577yxa3/1/

+0

小提琴更新。 –

+0

此代碼是如此helpul我已經對我的代碼進行了更改我已經使用您的代碼添加此代碼 var oTable = $('#custom_2')。dataTable(); ('keyup',function(){ var ap = $(this).val(); oTable.fnFilter(「^」+ ap,null,true ,true,false).draw();; }); } 鏈接:http://legacy.datatables.net/ref#fnFilter – saurabh

0

我已經使用它來使用戶能夠搜索元素週期表中的元素。

var searchString = document.getElementById("search").value; //The user input field 
 
    if (searchString !== "") { 
 
    var cols = document.querySelectorAll('#yourTableID td'), colslen = cols.length, i = -1; 
 
\t while(++i < colslen){ 
 
     if(cols[i].id.indexOf(searchString)>-1){ 
 
      //Do something to the tds that match the searchstring (acces with "cols[i]") 
 
     } else { 
 
      //Hide the tds that does not match the searchstring (eg. cols[i].style.display = "none";)

我用<td>的id搜索字符串相匹配,但我想你也許能夠替換.ID。在if語句中,如果要搜索td中的文本,則使用innerHTML或value。

希望這會有所幫助!