Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum enhancement #10

Closed
iicmaster opened this issue Nov 29, 2012 · 6 comments
Closed

Enum enhancement #10

iicmaster opened this issue Nov 29, 2012 · 6 comments

Comments

@iicmaster
Copy link

I think it will better if we can set display text and value of options by array key,value like this

'options' => array(
    'user' => 'Customer',
    'owner' => 'Merchant',
    'admin' => 'Content Moderator',
    'superadmin' => 'System Administrator'
),
@iicmaster
Copy link
Author

I try to looking in custom-bindings.js but not found anything about enum yet, is that mean we must write new rule for this enhancement?

@janhartigan
Copy link
Member

Good suggestion! I've implemented this for 2.2.0. If you're curious how it was done, check this out:

fca882e

Basically, I had to adjust the Enum constructor to check for both array possibilities. So consider this example:

array(
    'Winter' => 'Cold, Cold Winter!!', 
    'Spring', 
    'Summer' => 'Hot, Hot Summer', 
    'Fall'
)

In this case, the $options property in the Enum class would end up with this value:

array(
    'Winter' => 'Cold, Cold Winter!!', 
    'Spring' => 'Spring', 
    'Summer' => 'Hot, Hot Summer', 
    'Fall' => 'Fall'
)

Then I had to adjust the filter and edit fields to include the optionsValue and optionsText properties of the Knockout options binding.

Custom bindings are only needed for special fields that can't be handled by existing bindings. As you can see, the enum field uses the custom chosen binding which turns the field into a jQuery Chosen object, so we don't need to do anything there. We need to handle that in a binding because Knockout refreshes the DOM whenever a viewModel observable updates.

@iicmaster
Copy link
Author

Wow! Thanks

@iicmaster
Copy link
Author

I store 0,1, 2 for user_role in my database and I can't use enum for this field. this is a little bit hack for my ugly DB structure. I changed enum.php line 32

from 'value' => is_numeric($val) ? $text : $val,

to 'value' => is_int($val) ? $text : $val,

and now I can used

'options' => array(
    '00' => 'User', 
    '01' => 'Normal Curator', 
    '02' => 'Fetured Curator'
),

@janhartigan may you change is_numeric to is_int for more flexibility like this case
or any idea for better solution :)

@iicmaster
Copy link
Author

Update: my solution is work grate in filters but doesn't work in edit, any idea? (T T)

@janhartigan
Copy link
Member

OK so the issue here is that you shouldn't be using the enum field as you are. Enums are for text-only fields that don't have an ID. I assume you have a roles table, correct? So why not make a roles relationship and set up a relationship field?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants