E
E
Eugene2018-08-27 09:48:35
Yii
Eugene, 2018-08-27 09:48:35

How to correctly form layout for AJAX response in yii2?

In general, I do a little lazy filtering of laptops.
There are various checkboxes and sliders with prices in the aside. By clicking the Select button, Ajax is sent to the server and receives the necessary laptops.

But it's easier for me to form the layout on the server side.

It is necessary to form a layout where each laptop has something similar

<div class="col-md-4 col-xs-6">
                        <div class="product">
                            <div class="product-img">
                                <img src="./img/product01.png" alt="">
                                <div class="product-label">
                                    <span class="sale">-30%</span>
                                    <span class="new">NEW</span>
                                </div>
                            </div>
                            <div class="product-body">
                                <p class="product-category">Ноутбук</p>
                                <h3 class="product-name"><a href="product.html">product name goes here</a></h3>
                                <h4 class="product-price">$980.00 <del class="product-old-price">$990.00</del></h4>
                                <div class="product-rating">
                                    <i class="fa fa-star"></i>
                                    <i class="fa fa-star"></i>
                                    <i class="fa fa-star"></i>
                                    <i class="fa fa-star"></i>
                                    <i class="fa fa-star"></i>
                                </div>
                            </div>
                            <div class="add-to-cart">
                                <a href="product.html"><button class="add-to-cart-btn"><i class="fa fa-shopping-cart"></i> Посмотреть</button></a>
                            </div>
                        </div>
                    </div>


Is it correct to form the layout for filtering on the server side and how to correctly send the layout in JSON as a response?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-08-27
@evgen9586

Good morning.
In yii2 there is such method renderAjax() .
It returns a view in response to an ajax request. The first parameter accepts a file containing the layout you need, and the second can take a model with the necessary data.

return $this->renderAjax('path/to/file', ['model' => $model])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question