2017-03-17 11 views
0

asp.netの検索ボックスで簡単なjsonテーブルを作成しました。データバードからデータを取得しています。何が間違っているのかわからない、検索ボックスが機能していません。してください誰か私の検索コードをチェックしますか?検索ボックスがテーブル内で機能していない

$(document).ready(function() { 
    $('.update').hide(); 
    $('.addRole').click(function() { 
     addRole() 
    }); 

    empRoles() 
    $(".paginate").paginga({ 
     item: "> tr", 
     itemsPerPage: 5, 
     maxPageNumbers: 2 
    }); 
    $('#searchGo').click(function() { 
     var searchVal = $('#searchroleName').val(); 
     if (searchVal == "") { 
      $('#searchroleName').addClass("error"); 
      $('.requiredField').show(); 
     } 
     else { 
      $('#searchroleName').removeClass("error"); 
      $('.requiredField').hide(); 
     } 
    }); 
    $(".searchClear").click(function() { 
     $('#searchroleName').val(""); 
     $('#roleListTable tr').show(); 
     $('.requiredField').hide(); 
     $('#searchroleName').removeClass("error"); 
    }); 
     var $rows = $('#roleListTable tr'); 
     var val = $.trim($('#searchroleName').val()).replace(/ +/g, ' ').toLowerCase(); 
     $rows.show().filter(function() { 
     var text = $(this).text().replace(/\s+/g, ' ').toLowerCase(); 
     return !~text.indexOf(val); 
    }).hide(); 
}); 

https://jsfiddle.net/ojog9uq0/

+0

https://jsfiddle.net/9a6x07y2/ $('#searchGo').clickイベントにコードの下/書き込みを移動する必要があります1 /追加の閉じ括弧 '})がありました。 document.readyの終わりに。 – Ayush

+0

これは括弧で囲まれていますか?実際には私の間違いです。 – user7397787

+0

@ user7397787あなたのサンプルリンクでテーブルデータを見ることができません。 –

答えて

2

あなたのコードが正しく動作しています。あなたの検索ボタンのクリックコードの一部にelseの部分に検索コードを配置するだけです。

$('#searchGo').click(function() { 

    var searchVal = $('#searchroleName').val(); 
    console.log('click ' + searchVal); 
    if (searchVal == "") { 
     $('#searchroleName').addClass("error"); 
     $('.requiredField').show(); 
    } 
    else { 
     $('#searchroleName').removeClass("error"); 
     $('.requiredField').hide(); 
     var $rows = $('#roleListTable tr'); 
     var val = $.trim($('#searchroleName').val()).replace(/ +/g, ' ').toLowerCase(); 
     console.log("trim "+val); 
     $rows.show().filter(function() { 
      var text = $(this).text().replace(/\s+/g, ' ').toLowerCase(); 
      console.log("show text "+text); 
      return !~text.indexOf(val); 
     }).hide(); 
     } 
}); 

これを試してください。以下は

、それはそれのためのreffrenceを持っている、とJSを返していないため、このコードを削除するだけでfunction empRoles() {

});を削除を変更した後、あなたのコードはjsfiddleで働いている

Working fiddle link

1

エラー

$(".paginate").paginga({ 
     item: "> tr", 
     itemsPerPage: 5, 
     maxPageNumbers: 2 
); 
1

試してください:

Working fiddle demo

$("#searchroleName").on("keyup", function() { 
var g = $(this).val().toLowerCase(); 
$(".searchclass").find('td').each(function() { 
    var s = $(this).text().toLowerCase(); 
    $(this).closest('td')[ s.indexOf(g) !== -1 ? 'show' : 'hide' ](); 
    }); 
}); 
1

あなただけ

var $rows = $('#roleListTable tr'); 
    var val = $.trim($('#searchroleName').val()).replace(/ +/g, ' ').toLowerCase(); 
    $rows.show().filter(function() { 
    var text = $(this).text().replace(/\s+/g, ' ').toLowerCase(); 
    return !~text.indexOf(val); 
       }).hide(); 

var roleList=[{ 
 
"sNo"  :"1", 
 
"roleName":"Designer" 
 
}, 
 
{ 
 
"sNo"  :"2", 
 
"roleName":"Developer" 
 
}, 
 
{ 
 
"sNo"  :"3", 
 
"roleName":"HR Dept" 
 
}, 
 
{ 
 
"sNo"  :"4", 
 
"roleName":"Project Manager" 
 
} 
 
]; 
 

 
$(document).ready(function() { 
 
    $('.update').hide(); 
 
    $('.addRole').click(function() { 
 
     addRole() 
 
    }); 
 
    
 
    empRoles() 
 
    
 
    $('#searchGo').click(function() { 
 
     var searchVal = $('#searchroleName').val(); 
 
     if (searchVal == "") { 
 
      $('#searchroleName').addClass("error"); 
 
      $('.requiredField').show(); 
 
     } 
 
     else { 
 
      $('#searchroleName').removeClass("error"); 
 
      $('.requiredField').hide(); 
 
     } 
 
     
 
     var $rows = $('#roleListTable tr'); 
 
     var val = $.trim($('#searchroleName').val()).replace(/ +/g, ' ').toLowerCase(); 
 
     $rows.show().filter(function() { 
 
     var text = $(this).text().replace(/\s+/g, ' ').toLowerCase(); 
 
     return !~text.indexOf(val); 
 
    \t \t \t \t \t }).hide(); 
 
    }); 
 
    $(".searchClear").click(function() { 
 
     $('#searchroleName').val(""); 
 
     $('#roleListTable tr').show(); 
 
     $('.requiredField').hide(); 
 
     $('#searchroleName').removeClass("error"); 
 
    }); 
 
      
 
}); 
 

 
function empRoles(){ 
 
\t \t for(var i=0;i<roleList.length;i++) 
 
\t  { 
 
\t  var table='<tr id="row'+i+'"><td>'+roleList[i].sNo+'</td><td class="roleName" id="name'+i+'">'+roleList[i].roleName+'</td><td><button class="btn edit btn-info" id="edit'+i+'"><i class="fa fa-pencil"></i>Edit</button><button class="btn update btn-success" id="update'+i+'"><i class="fa fa-floppy-o"></i>Update</button><button class="btn dlt btn-danger" data-toggle="modal" data-target="#confirm"><i class="fa fa-trash-o"></i>Delete</button></td><tr>'; 
 
\t  \t $('#roleListTable').append(table) 
 
\t  } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
 
<div class="searchRole"> 
 
       <input class="col-lg-3 col-md-3 col-sm-3 col-xs-4" type="text" name="searchroleName" id="searchroleName"> 
 
       <button class="btn btn-primary searchGo" id="searchGo">Go</button> 
 
       <button type="reset" class="btn btn-primary searchClear">Clear</button> 
 
      </div> 
 
      <div class="col-lg-12 col-md-12 col-xs-12 padding paginate table-responsive"> 
 
       <table class="table table-hover table-bordered"> 
 
        <thead class="roleListTableHead"> 
 
         <tr> 
 
          <td>S.NO</td> 
 
          <td>ROLE NAME</td> 
 
          <td>ACTION</td> 
 
         </tr> 
 
        </thead> 
 
        <tbody class="items" id="roleListTable"></tbody> 
 
       </table> 
 
       </div>

+0

@ user7397787 - 上記の答えを参照してください - コードスニペットは完璧に動作しています。あなたのコードは、あなたが上記のようにするために必要なもの – prog1011

関連する問題