Skip to content

Commit

Permalink
Form Element Form Chooser: new option 'result_keep_order'
Browse files Browse the repository at this point in the history
  • Loading branch information
plepe committed Nov 26, 2017
1 parent a1ecced commit 819380c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ Definition:
* exclude_null_values: if true, remove null values from the array. Default: false.
* order: whether the elements shall be orderable. true (default) / false.
* removeable: whether the elements shall be removeable. true (default) / false.
* result_keep_order: if true, resulting elements will be ordered by appearance in def.

Example:
```json
Expand Down
1 change: 1 addition & 0 deletions demo/demo_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'name' =>"Filters",
'type' =>'form_chooser',
'order' =>false,
'result_keep_order' => true,
'def' =>array(
'a' => array(
'type' => 'text',
Expand Down
10 changes: 10 additions & 0 deletions inc/form_element_form_chooser.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ form_element_form_chooser.prototype.get_data=function() {
}
}

if (this.def.result_keep_order) {
var d = {}
for (var k in this.def.def) {
if (k in ret) {
d[k] = ret[k]
}
}
ret = d
}

if(count==0)
return this.def.empty_value || null;

Expand Down
10 changes: 10 additions & 0 deletions inc/form_element_form_chooser.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ function get_data() {
$data[$k] = $d;
}

if (array_key_exists('result_keep_order', $this->def) && $this->def['result_keep_order']) {
$d = array();
foreach ($this->def['def'] as $k => $v) {
if (array_key_exists($k, $data)) {
$d[$k] = $data[$k];
}
}
$data = $d;
}

if(!sizeof($data))
return array_key_exists('empty_value', $this->def) ?
$this->def['empty_value'] : null;
Expand Down

0 comments on commit 819380c

Please sign in to comment.