Skip to content

Commit

Permalink
Adjusting enum type to accept key=>value array
Browse files Browse the repository at this point in the history
  • Loading branch information
janhartigan committed Nov 29, 2012
1 parent 3799d2a commit fca882e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
11 changes: 10 additions & 1 deletion Libraries/Fields/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ public function __construct($field, $info, $model)
{
parent::__construct($field, $info, $model);

$this->options = array_get($info, 'options', $this->options);
$this->value = $this->value === '' ? null : $this->value;
$options = array_get($info, 'options', $this->options);

//iterate over the options to create the options assoc array
foreach ($options as $val => $text)
{
$this->options[] = array(
'text' => $text,
'value' => is_numeric($val) ? $text : $val,
);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ The available options are:
- **search_fields**: default is array(name_field). Must be an array. You can supply an on-table column name or a raw SQL function like CONCAT(first_name, ' ', last_name)

##### Enum
- **options**: default is an empty array. Must be an array of values that can be converted into strings
- **options**: default is an empty array. This can either be an array of strings (array('Spring', 'Winter')) or an array of strings indexed on the enum value (array('Spring' => 'Beautiful Spring!', 'Winter' => 'Cold Winter! :(')). In the latter case, the key value will be used to save to / query the database.

##### Text/Textarea
- **limit**: default is 0 (i.e. no character limit).
Expand Down
5 changes: 4 additions & 1 deletion views/templates/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@
{{/if}}
{{if type === 'enum'}}
<select id="edit_field_${ field }" data-bind="attr: {disabled: freezeForm}, value: $root[key], chosen: true,
options: options, optionsCaption: 'None'"></select>
options: options,
optionsValue: function(item) {return item.value},
optionsText: function(item) {return item.text},
optionsCaption: 'None'"></select>
{{/if}}
{{if type === 'date'}}
<input type="text" id="edit_field_${ key }" data-bind="attr: {disabled: freezeForm}, value: $root[key],
Expand Down
4 changes: 3 additions & 1 deletion views/templates/filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
optionsCaption: 'All'"></select>
{{/if}}
{{if type === 'enum'}}
<select id="filter_field_${ field }" data-bind="value: value, chosen: true, options: options, optionsCaption: 'All'"></select>
<select id="filter_field_${ field }" data-bind="value: value, chosen: true, options: options, optionsCaption: 'All',
optionsValue: function(item) {return item.value},
optionsText: function(item) {return item.text}"></select>
{{/if}}
{{if type === 'date'}}
<input type="text" id="filter_field_min_${ field }" data-bind="value: minValue, datepicker: {dateFormat: date_format}" />
Expand Down

0 comments on commit fca882e

Please sign in to comment.