Reputation: 1701
How can I retain the item and value of the selected from dropdown list after selecting? Let me show you first my code:
<select id="categories" class="select2-select-00" onchange="getData(<?php if(isset($_GET['student'])) echo $_GET['student']; ?>);">
<option selected="selected" value="<?php if(is_null($category_id)) echo 0; else echo $category_id;?>"> <?php if(is_null($category)) echo " "; else echo $category; ?></option>
<?php
foreach ($page_data['ategory_list'] as $category_list_key => $ategory_list_data){
?>
<!--<option id="hidden_option" class="hidden"></option>--
<option value="<?php echo $template_category_list_data['id']?>">
<?php echo $template_category_list_data['Category Name']; ?>
</option>
<?php } ?>
</select>
So when i select from the dropdown, it will call the JS function getData
. That JS function will then call the controller and the controller do it's thing then refreshes the list according to the selected background.
But the problem is my dropdown is included in rendering the page paritally.
How can i retain the selected item in the dropdown.
I'm also confuse as to how I'm going to convert this to Yii Chtml::Dropdown something
..
Any help is greatly appreciated. Thank you in advance.
Upvotes: 0
Views: 588
Reputation: 1305
Try with this:
<select id="categories" class="select2-select-00" onchange="getData(<?php if(isset($_GET['student'])) echo $_GET['student']; ?>);">
<?php
foreach ($page_data['ategory_list'] as $category_list_key => $category_list_data):
$selected = !is_null($category_id) && $categori_id == $category_list_data['id']? 'selected="selected"' : '';
echo '<option value="'.$category_list_data['id'].'" '.$selected.' >'.
$category_list_data['Category Name'].
'</option>';
endforeach;
?>
Upvotes: 1