【问题标题】:filtering table results using ajax使用ajax过滤表结果
【发布时间】:2015-07-30 10:32:34
【问题描述】:

我正在使用 codeigniter 框架开发一个 php 在线商店网站,我希望能够使用带有 ajax 的复选框来过滤我的表格结果

查看:

<input type="checkbox" name="brand" value="acer">  


<input type="checkbox" name="brand" value="lenovo">    

<input type="checkbox" name="pret" value="1000">   



 <table>
        <tbody>
        <?php foreach ($laptops_toate as $laptops_all) { ?>
            <tr>
                <td><img src="http://localhost:82/ci/images/emag/<?php echo $laptops_all->file ?>"></td>
                <td><p>Laptop <?php echo $laptops_all->brand ?> </p>
                </td>


            </tr>
        <?php } ?>
        </tbody>
    </table>  

控制器:

 public function laptops()
{

    $filter = array(
        'pret' => $this->input->get('pret'),
        'brand' =>$this->input->get('brand')
    );

    $data['laptops_toate'] = $this->emag_model->laptops_toate_grid($filter);

    $this->renders('emag/laptops', $data);
}  

型号:

 public function laptops_toate_grid($filter = null){
    $this->db->select('*')
             ->from('laptop_notebook');
   // $query = $this->db->get('laptop_notebook')->result();
   // return $query;
    if($filter['brand']){
        $this->db->where('brand', $filter['brand']);
    }
    if($filter['pret']){
        $this->db->where('pret', $filter['pret']);
    }
    $query = $this->db->get()->result();

    return $query;

}

现在问题出在ajax代码上,我不知道如何将数据过滤器发送到服务器以接收成功函数。

【问题讨论】:

    标签: ajax codeigniter checkbox filtering


    【解决方案1】:

    查看:

    <script>
    $("input[checkbox]").change(function(){
                   $.ajax({
                url: route,
                dataType: 'json',
                success: function(data){
                    $.each(data, function(index, element) {
                        $("tbody").empty();
                        $("tbody").append("<tr><td>"+
                        "Laptop "+element.brand+""+
                        "</td></tr>");
                    });
                }
            }); 
    

    控制器:

     public function laptops()
    {
    
    $filter = array(
        'pret' => $this->input->get('pret'),
        'brand' =>$this->input->get('brand')
    );
    
    echo json_encode($this->emag_model->laptops_toate_grid($filter));
    } 
    

    现在只需执行 console.log(data);首先在 $.each() 里面看看你的数组是什么样子的。

    【讨论】:

    • 我还建议更改视图中的代码以获得更好的概览,这只是个人喜好。而不是: 使用: 当然它没有那么快,但是性能损失非常低,并且在更大的视图中更容易看到循环和 if 结束的位置。再次只是我的个人意见
    • 这很奇怪,这是我通常如何在我的 HTML 中使用 PHP puu.sh/hTfix/d5994b14e3.png 的链接
    • 我同意你对 foreach 的看法,但主要问题仍然是 ajax :|,不知何故,使用 dataType: 'json' 在我的控制台中没有任何作用
    • @killstreet - 最佳解决方案。
    猜你喜欢
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 2023-03-30
    • 2014-10-23
    • 1970-01-01
    相关资源
    最近更新 更多