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

Questions about Reference_fields #492

Closed
F3L1X79 opened this issue Jun 4, 2015 · 5 comments
Closed

Questions about Reference_fields #492

F3L1X79 opened this issue Jun 4, 2015 · 5 comments
Labels

Comments

@F3L1X79
Copy link

F3L1X79 commented Jun 4, 2015

Hi guys,
I got a question about Reference_fields, not really an issue I guess.
In my app, a Post is related to an User so I need to use Reference like this:

post.listView()
            .sortField('created_at')
            .sortDir('DESC')
            .infinitePagination(true)
            .fields([
                nga.field('id').label('ID'),
                nga.field('created_at', 'date')
                nga.field('content').map(truncate),
                nga.field('wuserId', 'reference')
                    .label('User')
                    .targetEntity(user)
                    .targetField(nga.field('email'))
            ])
            .listActions(['show', 'edit', 'delete']);

In the example above, in my targetField, i'd like to concatenate two values (user.firstname and user.lastname) instead of the only field email. I was wondering if this was possible.

Also, while the code above is working great on a listView, it's not displaying the email field on the dashboard View - ie:

post.dashboardView() 
            .title('10 last Posts')
            .order(1)
            .perPage(10)
            .sortField('created_at')
            .fields([
                nga.field('content').isDetailLink(true).map(truncate),
                nga.field('created_at', 'date'),
                nga.field('wuserId', 'reference')
                    .label('User')
                    .targetEntity(user)
                    .targetField(nga.field('email'))
            ]);

Is this normal? And if it is - it would we be great to add this feature in a next release.

Best regards.

@fzaninotto
Copy link
Member

You can use map() to modify a field, the following way:

                nga.field('wuserId', 'reference')
                    .label('User')
                    .targetEntity(user)
                    .targetField(nga.field('email').map(function (value, entry) {
                         return entry.firstname + ' (' + entry.lastname + ')';
                    }))

As for references in the dashboardView, I think it's been fixed recently (#488). Could you test with the latest master? If not, please open a dedicated issue.

@F3L1X79
Copy link
Author

F3L1X79 commented Jun 4, 2015

It's all working. Thanks a lot!

@jamespsterling
Copy link

@fzaninotto is this possible with a reference field on edit/create view?

@F3L1X79
Copy link
Author

F3L1X79 commented Nov 8, 2016

@jamespsterling yes it is actually working on the creationView() et editionView().

@jamespsterling
Copy link

@F3L1X79 how are you using it? When I do

.map(function (value, entry) {
                         return entry.firstname + ' (' + entry.lastname + ')';
 })

It only shows me one entry for the preselected item in the reference list (dropdown).

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

No branches or pull requests

3 participants