How can I update view if I trigger Ajax from js?

How can I update view if I trigger Ajax from a JS ?

I want to update _partialCategory.php when user clicks on parent category.

  1. User click on a custom design parent category div.
  2. On click I want to load its child category by replacing the parent category.
    I m able to populate parent category.
    I m also able to make a ajax call to the controller but
    I m not able to update view based on new data from ajax call.


function getChildCategory(parentId){
    var url = '/site/get-child-category';
    var data = { id : parentId};
        url: url,
        type: 'get',
        dataType: 'json',
        data: data
        .done(function(response) {
            if ( == true) {
        .fail(function() {


public function actionGetChildCategory($id){
            $category = Category::find()
                ->where(['parent_id' => $id])
            if (Yii::$app->request->isAjax) {
                Yii::$app->response->format = Response::FORMAT_JSON;
                if ($category != null) {
                    $message = 'Please select child category';
                    return [
                        'data' => [
                            'success' => true,
                            'data' => $category,
                            'message' => $message,
                        'code' => 0,
                } else {
                    return [
                        'data' => [
                            'success' => false,
                            'data' => null,
                            'message' => 'An error occured.',
                        'code' => 1, // Some semantic codes that you know them for yourself
                return "Outside isAjax";


<?= $this->render('_partialCategory', array('category'=>$category)); ?>


/* @var $category \backend\models\Category */
foreach ($category as $item) { ?>
    <div class="cat-wrapper category">
        <div class="card-block radio">
            <div class="row justify-content-center d-flex">
                <div class="cat-icon">
                    <i class="<?= $item['imagepath'] ?>"></i>
        <h5 class="cat-name"><?= $item['name'] ?></h5>
        <p class="cat-id" value="<?= $item['id'] ?>"></p>
<?php } ?>

is your _partialCategory.php partial included into the current view? if it is then all you need is to modify your ajax a little and instead of alerting the content you insert somewhere in the page:

if ( == true) {
        $('.some-element').html(; // assuming the data received is string or html
1 Like

Thanks for your reply. I figured it out. I was unaware that i could user .html to add views.