Editable::source PHP Method

source() public static method

..}] See https://github.com/vitalets/x-editable-yii/issues/37
public static source ( mixed $models, mixed $valueField = '', mixed $textField = '', mixed $groupField = '', mixed $groupTextField = '' )
$models mixed
$valueField mixed
$textField mixed
$groupField mixed
$groupTextField mixed
    public static function source($models, $valueField = '', $textField = '', $groupField = '', $groupTextField = '')
    {
        $listData = array();
        $first = reset($models);
        //simple 1-dimensional array: 0 => 'text 0', 1 => 'text 1'
        if ($first && (is_string($first) || is_numeric($first))) {
            foreach ($models as $key => $text) {
                $listData[] = array('value' => $key, 'text' => $text);
            }
            return $listData;
        }
        // 2-dimensional array or dataset
        if ($groupField === '') {
            foreach ($models as $model) {
                $value = CHtml::value($model, $valueField);
                $text = CHtml::value($model, $textField);
                $listData[] = array('value' => $value, 'text' => $text);
            }
        } else {
            if (!$groupTextField) {
                $groupTextField = $groupField;
            }
            $groups = array();
            foreach ($models as $model) {
                $group = CHtml::value($model, $groupField);
                $groupText = CHtml::value($model, $groupTextField);
                $value = CHtml::value($model, $valueField);
                $text = CHtml::value($model, $textField);
                if ($group === null) {
                    $listData[] = array('value' => $value, 'text' => $text);
                } else {
                    if (!isset($groups[$group])) {
                        $groups[$group] = array('value' => $group, 'text' => $groupText, 'children' => array(), 'index' => count($listData));
                        $listData[] = 'group';
                        //placeholder, will be replaced in future
                    }
                    $groups[$group]['children'][] = array('value' => $value, 'text' => $text);
                }
            }
            //fill placeholders with group data
            foreach ($groups as $group) {
                $index = $group['index'];
                unset($group['index']);
                $listData[$index] = $group;
            }
        }
        return $listData;
    }

Usage Example

<?php

/* @var $this ApplicationProjectMentorController */
/* @var $model ApplicationProjectMentor */
?>

<?php 
$this->widget('bootstrap.widgets.TbDetailView', array('data' => $model, 'attributes' => array('id', 'user_id', 'status', 'date_created', 'max_amount', 'max_hours', 'system_pick_amount')));
?>

<hr>
<h4>Awaiting Approval</h4>
<?php 
$this->widget('bootstrap.widgets.TbGridView', array('summaryText' => '', 'dataProvider' => $model3, 'columns' => array('id', 'app_id', 'project_id', 'title', array('class' => 'editable.EditableColumn', 'name' => 'approval_status', 'header' => 'Approval Status', 'editable' => array('type' => 'select', 'url' => $this->createUrl('applicationProjectMentor/updatePick'), 'placement' => 'right', 'source' => Editable::source(array('Proposed by Mentor' => 'Proposed by Mentor', 'Approved' => 'Approved', 'Rejected' => 'Rejected')))))));
?>
<hr>
<h4>History</h4>
<?php 
$this->widget('bootstrap.widgets.TbGridView', array('summaryText' => '', 'dataProvider' => $model2, 'columns' => array('id', 'app_id', 'project_id', 'title', 'approval_status')));
All Usage Examples Of Editable::source