php - October CMS backend list's column get array key value -
hi i'm new october cms . have defined below shown method in model class. method used show select options in backend form. method returns array key value similar field value in db. have defined method static because recommended in front end function , process db record , iterate show value of array matches key. works fine. thing in columns.yaml file, how list method's array value matches db record did in front end.
public static function getsampleoptions() { return[ '1'=>'sample1', '2'=>'sample2' ]; }
hello friends found answer october cms help/support http://octobercms.com/index.php/forum/post/dropdown-shows-its-value-than-key-name-in-list-controller , referred few concepts of laravel.
model class method
public static function getsampleoptions() { return[ '1'=>'mobile app', '2'=>'web app' ]; }
columns.yaml file
sample: label: sample column type: dropdown
again in model, declare attributes object , include filed name key empty value
public $attributes = ['sample'=>''];
define getfield_nameattribute() function set associated value appropriate key in column
public function getsampleattribute() { $result = $this->attributes['sample']; $options = $this->getsampleoptions(); foreach($options $key=>$value) { if($key == $result) { return $value; } } }
updated solution rectify problem while editing record simple. create partial , modify fields. yaml
_sample_options.htm (partial) // file name should begin with_(underscore)
<?php $fieldoptions = $model->getsampleoptions(); $sample = $model->attributes['sample']; ?> <select id="<?= $field->getid() ?>" name="<?= $field->getname() ?>" class="form-control custom-select" <?= $field->getattributes() ?>> <?php foreach($fieldoptions $key=>$label) { ?> <option value="<?= $key ?>" <?php echo ($sample == $key)?"selected":''; ?>><?= $label ?></option> <?php } ?> </select>
here $model , $field partial variables used access intended model's methods , properties. documentation : https://octobercms.com/docs/backend/forms#field-partial
fields.yaml file
sample: label: sample field type: partial path: $/october/demo/controllers/sample/_sample_options.htm //path partial located in controller view
Comments
Post a Comment