動的

2016-07-08 47 views
1

私はビューにユーザーデータを渡す私のコントローラのメソッドを持っている検索フィールドと更新ビューを作成します。私がしたいのは、これらのデータをソートする検索フィールドを作成することです。これは現時点ではこのように見えます。私はテキストボックスに何かを書くことができ、テキストボックスに現在の値を含む現在のチェックボックスだけを表示することができます。動的

enter image description here

私は(とSQLクエリは、名前とFIRSTNAMEに含まれている)Javascriptを使用して、パラメータとコントローラのメソッドを呼び出すと、ページをリフレッシュすることについて考えたが、私は思い出したくありません表示されたデータをソートする必要があるため、データベース全体です。 これを行うための最良の方法は何か分かりません。

EDIT:私の現在の作業ではないので、よく解決策:

function myFunction(obj) { 
    div = document.getElementById("userDiv"); 
    div.innerHTML = ''; 
    var ajax_url = "<?php echo site_url('sending/form/searchUser/');?>"; 
    var search = obj.value; 
    $.ajax({ 
     type: "POST", 
     url: ajax_url, 
     data: { 'search': search }, 
     success: function(data){ 
      // Parse the returned json data 
      var opts = $.parseJSON(data); 
      //console.log(opts); 
      // Use jQuery's each to iterate over the opts value 
      $.each(opts, function(i, d) { 
       // You will need to alter the below to get the right values from your json object. 
       console.log(i); 
       console.log(d.id_user); 

       div.innerHTML += '<div class=\"md-checkbox\"><input id=\"usersbox[' + d.id_user + ']\" name=\"usersbox[' + d.id_user + ']\" class=\"md-check users\" type=\"checkbox\"><label for=\"usersbox[' + d.id_user + ']\"><span class=\"inc\"></span><span class=\"check\"></span><span class=\"box\"></span>' + d.name + ' ' + d.firstname + '</label></div>'; 

      }); 
     } 
    }); 
} 

答えて

1


あなたはjavascriptのコードを使用してPHPのコードを置き換えるとhtmlに結果を追加することができます。

<div class="col-md-4"> 
    <div class="portlet light"> 
     <div class="portlet-title"> 
      <div class="caption font-blue-sunglo"> <i class="fa fa-user-secret font-blue-sunglo"></i> <span class="caption-subject bold uppercase"> Utilisateurs</span> </div> 
      <input id="searchUser" type="text" placeholder="Chercher un utilisateur" class="form-control" onkeyup="searchUser(this)"> 
     </div> 
     <div id="users"> 
     </div> 
    </div> 
</div> 

とJS:

<script> 
//on load all the names will be displayed, if you want results just after you type an character in the input delete this 
window.onload = searchUser(''); 

//better to call this function "onkeyup" 
function searchUser(user) { 
    //this may vary if you have an object or array in php (i used array) 
    var users = <?php echo json_encode($users); ?>; 
    var usersHtmlDiv = $('#users'); 

    //this deletes the child nodes on a new search 
    usersHtmlDiv.empty(); 
    var htmlFormat = ''; 
    var input = (user.value) ? user.value : ''; 
    users.forEach(function(entry) { 
     console.log(entry); 
     console.log(entry.id); 

     //you can add firstname too here 
     if(entry.name.startsWith(input) || entry.firstname.startsWith(input)) { 

      htmlFormat += "<div class=\"md-checkbox\">"; 
      htmlFormat += "<input id=\"usersbox[" + entry.id_user + "]\" name=\"usersbox[" + entry.id_user + "]\" groups=\"" + entry.groups +"\" class=\"md-check users\" type=\"checkbox\">"; 
      htmlFormat += "<label for=\"usersbox[" + entry.id_user + "]\">"; 
      htmlFormat += "<span class=\"inc\"></span>"; 
      htmlFormat += "<span class=\"check\"></span>"; 
      htmlFormat += "<span class=\"box\"></span>"; 
      //here you have something about the session that does not make sense (check the if statement) 
      htmlFormat += entry.name + " " + entry.firstname; 
      htmlFormat += "</label>"; 
      htmlFormat += "</div>"; 
     } 
    }); 
    //append the resulted html to the users div 
    usersHtmlDiv.append(htmlFormat); 
} 

また、あなたは、コードを少しきれいにすることをお勧めします。あなたはnameとfirstname変数を使い、firstnameとlastnameを使ってみてください。

が素敵な一日を、
筋肉内

+0

おかげでたくさん、私はあなたと同様に動作しない解決策を考え出すために、すべての朝働いていました。私は現時点で私がどこにいるかを示すために自分の投稿を更新しました。 – Komarzer