preaction / Yancy

The Best Web Framework Deserves the Best Content Management System
http://preaction.me/yancy/
Other
54 stars 21 forks source link

'x-foreign-key' values are not displayed when referred table column described using 'x-list-columns' with custom title #96

Closed OpossumPetya closed 4 years ago

OpossumPetya commented 4 years ago

Standalone Yancy. Simple DB -- two tables: post_types (id, post_type) and posts (id, post_type_id, title, text). In yancy.conf I describe posts's post_type_id using x-foreign-key, so I can easily find and select needed value in UI:

post_type_id => {
    'x-order' => 2,
    type => 'integer',
    'x-foreign-key' => 'post_types',
},

If I describe post_types as

'x-list-columns' => [ 'post_type' ]

it works great. But when I use following

'x-list-columns' => [ { title  => "Type of Post", template => '{post_type}' } ]

the drop down with values is displayed with empty rows -- the number of rows is correct, and selecting a row get the correct value (id from post_type), but those dropdown rows do not show actual corresponding values.

image

{
    backend => 'sqlite:./test.db',
    editor => { require_user => undef, },
    schema => {
        post_types => {
            title => 'Post Types',
            required => [qw( post_type )],
            # 'x-list-columns' => [ 'post_type' ],
            'x-list-columns' => [ { title  => "Type of Post", template => '{post_type}' }, ],
            properties => {
                id => {
                    'x-order' => 1,
                    type => 'integer',
                    readOnly => 1,
                },
                post_type => {
                    'x-order' => 2,
                    type => 'string',
                },
            },
        },
        posts => {
            title => 'Blog Posts',
            required => [ 'post_type_id', 'title', 'body_text' ],
            'x-list-columns' => [ 'id', 'post_type_id', 'title', 'body_text' ],
            properties => {
                id => {
                    'x-order' => 1,
                    type => 'integer',
                    readOnly => 1,
                },
                post_type_id => {
                    'x-order' => 2,
                    type => 'integer',
                    'x-foreign-key' => 'post_types',
                },
                title => {
                    'x-order' => 3,
                    type => 'string',
                },
                body_text => {
                    'x-order' => 4,
                    type => 'string',
                    format => 'textarea',
                },
            },
        },
    },
}

test.zip

preaction commented 4 years ago

Ah, hah! I knew I forgot something: The foreign key field assumes x-list-columns is only strings... That's a bug that'll have to get fixed.

Until that's fixed, there are a couple things that you can do to work around this:

  1. Set x-display-field (described here under the x-foreign-key section of the Extended Field Configuration) to the field you want to use to both search and display.
  2. Instead of setting the field title in x-list-columns, set a field title inside the post_type_id field definition (unless that's just an example for the sake of this bug report).
OpossumPetya commented 4 years ago

oh, thanks so much! -- both of these work for me. I got derailed by this anomaly from looking for more obvious ways, I guess :)