简体   繁体   English

如何在Codeigniter中的Ajax之后仅加载指定的div

[英]How to load only a specified div after ajax in codeigniter

I'm having a certain categories list according to my click the query has been changed and also I have to change the div according to my click 根据点击我有一个特定的类别列表,查询已更改,并且我也必须根据点击更改div

I tried something it retrieve correct results but instead it display whole page inside the div.. 我尝试了一些方法来检索正确的结果,但它会在div内显示整个页面。

landing Controller : 登陆控制器

public function index()
    {   
        .
        .
        .

        if(isset($_POST['cat']))
        {
            $this->load->model('product_model'); 
            $condition=" WHERE p.status='Publish' AND u.group='Seller' AND u.status='Active' AND p.category_id IN (4) OR p.status='Publish' AND p.user_id=0 AND a.pricing IS NOT NULL AND p.category_id IN (4) GROUP BY p.id ORDER BY p.created DESC";
            $products_list_s = $this->product_model->view_product_details($condition);
            #echo "If";print_r($this->data['productDetails']);exit;
        }
        else
        {
            $condition = " WHERE p.status='Publish' AND u.group='Seller' AND u.status='Active' OR p.status='Publish' AND p.user_id=0 AND a.pricing IS NOT NULL GROUP BY p.id ORDER BY p.created DESC";
            $products_list_s = $this->product_model->view_product_details($condition);
        }
        $this->data['productDetails']         = $this->product_model->get_sorted_array($products_list_s,$products_list_u,'created','desc');
        .
        .
        .

        #echo $this->load->view('site/landing/landing',$data, TRUE);
        $this->load->view('site/landing/landing',$this->data);
    }

Jquery jQuery的

$('.all_category_li a').on('click', function(e) {
    var cat = 'cat';
    $.ajax({
        type:'post',
        url:'<?php echo base_url();?>site/landing',
        data:{cat:cat},
        dataType:'html',
        success:function(data)
        {
            $('.product_listing').html(data);
        }
    });
});

View 视图

<div>
<ul class="product_listing" id="product_listing">
.
.
.
</ul>
</div>

How should I do this.. 我应该怎么做

Can someone help me.. 有人能帮我吗..

Inside jQuery you could strip down the result to only show the results inside an element. 在jQuery内部,您可以精简结果以仅在元素内部显示结果。 For example: 例如:

success:function(data)
{
    // Search Element (replace the element or ID with your element)
    var startPos = data.indexOf("<div id='divId'>");

    // Div element is found?
    if(startPos != -1)
    {
        // Add tag length to position
        startPos += 16;

        // Search Div closing tag starting from start position
        var endPos = data.indexOf("</div>", startPos);

        // Add data inside the Div element to the product listing Div
        $('.product-listing').html(data.substr(startPos, endPos - startPos));
    }
}

maybe you need something like this 也许你需要这样的东西

$( "#result" ).load( "ajax/test.html #container" );

When this method executes, it retrieves the content of ajax/test.html, but then jQuery parses the returned document to find the element with an ID of container. 执行此方法时,它将检索ajax / test.html的内容,但是jQuery会解析返回的文档以查找具有容器ID的元素。 This element, along with its contents, is inserted into the element with an ID of result, and the rest of the retrieved document is discarded. 该元素及其内容将插入具有ID ID为result的元素中,而其余的检索文档将被丢弃。

http://api.jquery.com/load/ http://api.jquery.com/load/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM