【问题标题】:Create search field and update view dynamically创建搜索字段并动态更新视图
【发布时间】:2016-07-08 08:29:18
【问题描述】:

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

$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>

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

我考虑过使用 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>';

            });
        }
    });
}

【问题讨论】:

    标签: javascript php sql codeigniter


    【解决方案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。

    祝你有美好的一天,
    我.M.

    【讨论】:

    • 非常感谢,我整个上午都在努力想出一个效果不如你的解决方案。我更新了我的帖子,向您展示我现在的位置。
    猜你喜欢
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多