km65
km65

Reputation: 21

ng-admin won't display dropdown stored values

I really hope somebody can help me with an issue using ng-admin. I created a dashboard with ng-admin and all works fine. There is only one issue, on models where I used boolean field with chooices. Of course there is displayed a dropdown field, but on edit an entry the correct value from database isnt't selected. I also tried to use filterChoices, but won't make any changes... it is still nothing selected after open edit view.

Here is my field declaration:

nga.field('copiable','boolean')
                .label('Copy')
            .choices([
                      { value: null, label: 'Please choose' },
                    { value: true, label: 'Yes' },
                      { value: false, label: 'No' }
                  ])
            .filterChoices([
                    { value: null, label: 'Please choose' },
                    { value: true, label: 'Yes' },
                      { value: false, label: 'No' }
                  ]),

And here you can see my JSON response object:

{xxx, xxx, "copiable":"1"}

Here is the MySQL structure: enter image description here

Maybe I did a bad mistake here and somebody can help me.

Upvotes: 0

Views: 126

Answers (2)

pashamesh
pashamesh

Reputation: 11

You can use map/transform approach:

nga.field('copiable', 'boolean')
        .label('Copy')
        .choices([
            { value: null, label: 'Please choose' },
            { value: true, label: 'Yes' },
            { value: false, label: 'No' }
        ])
        .map(value => !!value)
        .transform(value => value ? 1 : 0)

map will convert incoming from API 1 or 0 to true or false accordingly.

transform wise versa convert true or false to 1 or 0 before send data to API

Upvotes: 0

km65
km65

Reputation: 21

If there is someone out there... who has the same issue. I got the solution now!

MySQL stored boolean as integer 0,1 and my backend with PHP SLIM API takes these values and put it in the JSON response. But ng-admin expects the values "true" or "false" only and can't work with 0,1 ... So I only had to customize my API response and now it's working fine!

Maybe this helps someone.... Greetings

Upvotes: 0

Related Questions