Skip to content

Commit

Permalink
Fix combo box convert types
Browse files Browse the repository at this point in the history
  • Loading branch information
UmSenhorQualquer committed Apr 15, 2019
1 parent 9329e8a commit 2fc3e1a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
17 changes: 12 additions & 5 deletions pyforms_web/controls/control_combo.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ def value(self):
@value.setter
def value(self, value):
for i, (key, val) in enumerate(self._items.items()):
if str(value)==str(val):
if str(self._value)!=str(value):
self._value = self._types[i](val)
value = self._types[i](value) if value is not None else None
if value==val:
if self._value!=value:
self._value = value
self.mark_to_update_client()
if self._init_form_called:

Expand All @@ -91,17 +92,24 @@ def text(self, value):
break

def __convert(self, value):
if value == fields.NOT_PROVIDED:
return None

if isinstance(value, ValueNotSet):
return None
"""
if isinstance(value, bool):
if value==True: value = 'true'
if value==False: value = 'false'
if value==None: value = 'null'
elif isinstance(value, ValueNotSet):
value = 'null'
elif value==fields.NOT_PROVIDED:
value = 'null'
else:
value = str(value)

"""
return value

def serialize(self):
Expand All @@ -111,7 +119,6 @@ def serialize(self):
items.append({'text': key, 'value': self.__convert(value), 'name': key })

value = self._value


data.update({ 'items': items, 'value': self.__convert(value) })
return data
Expand Down
4 changes: 2 additions & 2 deletions pyforms_web/web/static/pyformsjs/ControlCombo.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ class ControlCombo extends ControlBase{

set_value(value){
this.flag_exec_on_change_event = false;
this.jquery().dropdown('set exactly', [value]);
this.jquery().dropdown('set exactly', [String(value)]);
this.flag_exec_on_change_event = true;
};

////////////////////////////////////////////////////////////////////////////////

get_value(){
var value = this.jquery().dropdown('get value');
if( value.length==0 ) return null;
if(value=='None') return null;
if(value=='true') return true;
if(value=='false') return false;
if(value=='null') return null;
Expand Down
4 changes: 3 additions & 1 deletion pyforms_web/widgets/django/modelform.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,9 +980,11 @@ def create_model_formfields(self):


elif isinstance(field, models.Field) and field.choices:

print(field.get_choices(include_blank=field.blank,))
pyforms_field = ControlCombo(
label,
items=[ (c[1],c[0]) for c in field.choices],
items=[ (c[1],c[0]) for c in field.get_choices(include_blank=field.blank)],
default=field.default
)
elif isinstance(field, models.BigIntegerField): pyforms_field = ControlInteger( label, default=field.default )
Expand Down

0 comments on commit 2fc3e1a

Please sign in to comment.