Javascript代码不适用于由ajax

时间:2018-06-07 09:11:00

标签: javascript jquery ajax yii

我在网页中使用滚动分页。它运作良好。起初我检索4个数据。然后,当我向下滚动页面时,滚动分页会添加以前数据的4个数据结束。但是,用于每个数据的另一个ajax查询不适用于通过滚动分页添加的下一个元素。这是我的代码:

控制器代码:

class StoreController extends Controller
{
    public $page_data = array();

    public function actionIndex()
    {
        $criteria = new CDbCriteria();
        $criteria->limit = 4;
        $criteria->order = "t.id ASC";
        $posts = StoreProduct::model()->findAll($criteria);

        $productTypes = StoreProductType::model()->findAll();
        $page_info = Banners::getBannerBy(2, 'shop-page');
        $this->page_data = array('page_info' => $page_info);

        $this->render('index', array(
            'allProducts' => $posts,
            'protypes' => $productTypes,
        ));

    }
    public function actionItems()
    {
        $a = Yii::app()->request->getQuery('id');
                $data = StoreProduct::model()->findByPk($a);
        $q = $this->renderPartial('product_item', array('data' => $data));
        echo $q;
    }

    public function actionSorting()
    {
        $q = Yii::app()->request->getQuery('id');
        if (isset($q))
        {
            $criteria = new CDbCriteria();
            $criteria->condition = 't.type_id = :id';
            $criteria->order = "t.id ASC";
            $criteria->limit = 4;
            $criteria->params = array(':id' => $q);
            $posts = StoreProduct::model()->findAll($criteria);

            $q = $this->renderPartial('_form', array('allProducts' => $posts));
            echo $q;
        }
    }

    public function actionLoader()
    {
        $a = Yii::app()->request->getQuery('id');
        $q = Yii::app()->request->getQuery('type_id');

        if (isset($a)){
            $criteria = new CDbCriteria();
            $criteria->condition = "t.id > :id";
            $criteria->order = "t.id ASC";
            $criteria->limit = 4;
            $criteria->params = array(':id' => $a);

            $posts = StoreProduct::model()->findAll($criteria);
            if ($q != null){
                $criteria = new CDbCriteria();
                $criteria->condition = "t.id > :id";
                $criteria->addCondition('t.type_id = :type_id');
                $criteria->order = "t.id ASC";
                $criteria->limit = 4;
                $criteria->params = array(':id' => $a, ':type_id' => $q);

                $posts = StoreProduct::model()->findAll($criteria);

            }
        }
        $class = 'click';
        echo $json = $this->renderPartial('_form', array('allProducts' => $posts, 'id' => $posts));
    }
}

索引视图代码:

    <ul class="products" id="post-data">
                        <?php
                        $this->renderPartial('_form', array('allProducts' => $allProducts));
                        ?>

                    </ul>
                    <div class="ajax-load" style="display: none;">
                        <p><img src="<?= Yii::app()->theme->baseUrl ?>/assets/images/icons/loader.gif" alt="" width="100" height="100" /></p>
                    </div>
                    <div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
                        <div class="modal-dialog modal-lg shop-modal">
                            <div id="product-box" class="modal-content">
                                <!--   a place of a response of an ajax request    -->
                            </div>
                        </div>
                    </div>
<script type="text/javascript">
//    modaldan ma'lumot olib controllerga yuborish
        $('.click').on('click', function(){ // this is not working for next elements
            var k = $(this).data('id');
            $.ajax ({
                url:'/store/store/items',
                type: 'get',
                data:
                    {
                        id: k,
                    },
                success: function (data) {
                    if(data)

                        $('div#product-box').html(data);
                    else
                        alert('Error!');
                },
            });
        });
    // saralash
    $('.click-to-sort').on('click', function(){
        var k = $(this).data('id');
        $.ajax ({
            url:'/store/store/sorting',
            type: 'get',
            data:
                {
                    id: k,
                },
            success: function (data) {
                if(data){
                    $('ul#post-data').html('');
                    $('ul#post-data').html(data);
                }
                else
                    alert('Error!');
            },
        });
    });
    var pro_type = null;
    $(window).scroll(function() {

        if($(window).scrollTop() + $(window).height() >= $(document).height()) {
            var last_id = $(".post-id:last").attr("id");
            $(".click-to-sort").on('click', function(){
                pro_type = $(this).data('id');
            });
            loadMoreData(last_id, pro_type);


        }
    });

    function loadMoreData(last_id, pro_type){
        $.ajax({
            url: '/store/store/loader/',
            type: "get",
            beforeSend: function()
            {
                $('.ajax-load').show();
            },
            data: {
                id: last_id,
                type_id: pro_type,

            },
            success: function (data) {
                if (data)
                {
                    $('.ajax-load').hide();
                    $("#post-data").append(data);
                }
                else if (data == null)
                {
                    // this._clear();
                    alert('Bu oxirgi ma`lumotlar');
                }
            }
        });

    }
</script>

_form查看代码:

<?php

$col = 4;
$all = sizeof($allProducts);
$row = floor($all / $col);
if ($all % $col != 0)
    $row++;
$k = 0;
for ($i = 0; $i < $row; $i++)
{
    for ($j = 0; $j < $col; $j++)
    {
        if ($k < $all){ ?>
            <li class="post-id product product_cat-glasses use-star-ratings <?php if ($j==0) echo 'first'; elseif ($j==3) echo 'last';?> instock" id="<?php echo $allProducts[$k]->id; ?>">
                <a href="<?= $allProducts[$k]->url; ?>" class="click" data-toggle="modal" data-target=".bd-example-modal-lg" id="p-click-<?= $allProducts[$k]->id ?>" data-id="<?= ($allProducts[$k]->id) ? $allProducts[$k]->id : $posts[$k]->id; ?>" product-type="<?= $allProducts[$k]->type_id; ?>">
                    <img class="img-fluid" src="/uploads/product/<?= $allProducts[$k]->mainImage->name; ?>" alt=""/>
                    <h2 class="woocommerce-loop-product__title"><?= $allProducts[$k]->translate->name; ?></h2>
                </a>

            </li>
    <?php
        }
        else
            break;
        $k++;
    }
}
?>

提前致谢!

0 个答案:

没有答案