FahadAkram
FahadAkram

Reputation: 465

yii reload grid while getting data using right join

Scenario is one company have many divisions. Company is foreign key in division table.

I have two Requirements:

1 - Change 2nd dropdown on change of first (working fine).
2 - Reload Grid view on changing of company dropdown or first dropdown , like if i change drop down of company field (primary key in company table and foreign key in division table , reloaded grid show data of company selected in first dropdown.

Here is my model

class Divisions extends CActiveRecord 
{

    public function relations() {
        return array(
            'company' => array(self::BELONGS_TO, 'Companies', 'CompanyID'),
            'fiscalperiods' => array(self::HAS_MANY, 'Fiscalperiods', 'DivisionID'),
            'regions' => array(self::HAS_MANY, 'Regions', 'DivisionID'),
        );
    }
}

Here is my external file in admin.php for dropdown

<div class="row">
<?php
$form = $this->beginWidget('CActiveForm', array(
    'action' => Yii::app()->createUrl($this->route),
    'method' => 'get',
));
?>
<div class="row">
</div>
<div class="row">
    <div class="span2">
        Choose Company
    </div>
    <div class="span2 offset1">
        Choose Division
    </div>
</div>
<div class="row">
    <div class="span2">
        <?php
        $modelCompany = new Companies();
        echo CHtml::activeDropDownList($modelCompany, 'Name', Chtml::listData(Companies::model()->findAll(), 'CompanyID', 'Name'), array(
            'ajax' => array(
                'type' => 'POST', //request type
                'url' => CController::createUrl('divisions/generatedivisions'), //url to call.
                'update' => '#Divisions_Name',
        )));
        ?>
    </div>
    <div class="span2 offset1">
        <?php
        $modelDivisions = new Divisions();
        echo CHtml::activeDropDownList($modelDivisions, 'Name', Chtml::listData(Divisions::model()->findAll(), 'DivisionID', 'Name'), array(
            'ajax' => array(
                'type' => 'GET', //request type
        )));
        ?>
    </div>
</div>
<?php $this->endWidget(); ?>

Upvotes: 1

Views: 56

Answers (1)

Danila Ganchar
Danila Ganchar

Reputation: 11223

You can solve it without ajax. I do not strong on frontend. But this is can help you.

$companies = array(
    // id company => name
    1 => 'one',
    2 => 'two',
    3 => 'three',
    4 => 'etc'
);
$secondList = array(
    // id company
    1 => array(
        1 => 'Requirements one 1',
        2 => 'Requirements one 2'
    ),
    2 => array(
        // sub id => name
        4 => 'Requirements two 1',
        5 => 'Requirements two 2'
    )
);

Your view:

<?php
echo CHtml::activeDropDownList(new Companies(), 'Name', $companies);

$show = true;
?>
<select id="second-select" name="second-select">
    <?php foreach ($secondList as $companyId => $arrayOptions) : ?>
        <?php foreach ($arrayOptions as $key => $option) : ?>
            <option
                data-company="<?= $companyId ?>"
                <?php if ($show == false) : ?>hidden="hidden" <?php endif ?>
                value="<?= $key ?>"><?= $option ?>
            </option>
        <?php endforeach ?>
        <?php if ($show == true) $show = false; ?>
    <?php endforeach ?>
</select>

In your js:

$(document).ready(function() {
    $('#Companies_Name').on('change', function() {
         var companyId = $(this).val();
         var options = $('#second-select option');
         options.attr('hidden', 'hidden');
         options = options.filter('*[data-company="' + companyId + '"]');
         options.removeAttr('hidden');
         options.first().attr('selected','selected');
    });
});

Upvotes: 1

Related Questions