创建搜索字段并动态更新视图

时间:2016-07-08 08:29:18

标签: javascript php sql codeigniter

我的Controller中有一个将用户数据传递给视图的方法:

$user['where'] = array('user.id_company' => $company_id);
$data['users'] = $this->user_model->getListDetailled_sending($user);

有了这个,我就会显示如下数据:

<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" onchange="searchUser(this)">
        </div>
        <div class="md-checkbox-list">

        foreach ($users as $value){

            echo "<div class=\"md-checkbox\">";
            echo "<input id=\"usersbox[".$value->id_user."]\" name=\"usersbox[".$value->id_user."]\" groups=\"".$value->groups."\" class=\"md-check users\" type=\"checkbox\">";
            echo "<label for=\"usersbox[".$value->id_user."]\">";
            echo "<span class=\"inc\"></span>";
            echo "<span class=\"check\"></span>";
            echo "<span class=\"box\"></span>";
            $label = ($this->session->userdata['logged_in']['acl_id'] == 10) ? $value->name. " ".$value->firstname : $value->name. " ".$value->firstname;
            echo $label;
            echo "</label>";
            echo "</div>";
        }
        ?>
        </div>
    </div>
</div>

我想要做的是创建一个搜索字段,对这些数据进行排序。目前看起来像这样。我希望能够在文本框中编写内容,并仅显示包含文本框中当前值的当前复选框的数据。

enter image description here

我考虑过使用Javascript,并使用参数调用Controller方法,并刷新页面(包含名称和名字的包含sql查询),但我不想回忆整个数据库,因为它需要对显示的数据进行排序。 我不知道最好的方法是什么。

编辑:我当前没有这么好的解决方案:

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 个答案:

答案 0 :(得分: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);
}

另外,您可能想要清理一点代码。您使用名称和名字变量,尝试使用名字和姓氏。

祝你有愉快的一天,
I.M。