Thursday, April 3, 2014

Yii Load data into select option list box dynamically + sorting

I would like to share how to sort list and load data into list box in yii.

Let say we have member table like Member(id, mid, name),

Controller File

class SignupController extends Controller
public function actionGetMember(){
    $json = array();
    $records = Member::model()->findAll(array('order'=>'id'));

    $count_records = count($records);
    if($count_records > 0){
      $json = array();
      foreach ($records as $record) {
        array_push($json, $record->attributes);

    echo json_encode($json);


Js File

jQuery(function($) {

// To call profession and specialty listing
    url: 'signup/GetMember',
    type: 'GET',
    dataType: 'json',
    success: function(data) {
      $.each(data, function(index, element) {
        $("<option></option>", {value: element['mid'], text: element['name']}).appendTo('#User');


View File

<select id="User">
<option>Please select:</option>

No comments:

Post a Comment