Datatables Editor

Datatables Editor

KarlogKarlog Posts: 9Questions: 4Answers: 0
edited October 2019 in DataTables

Hi, in general I'm unsure how to ask this question, but please do correct me if I'm totally mistaken!

I have a database structure which has let's say 4 tables
teams
teamLeaders
teamAssignments
users

I'm trying to assign multiple users to a team, and assign multiple users as team leaders for a given team and have the users assigned as leaders in one cell, and the members in another.
I've made an image of the database.

Is it possible to do this using datatables, I cannot seem to figure it out, without the rows being duplicated in datatable.
Let me explain a bit more. In the editor, when selecting two users in the dropdown ("type": 'select'), with "mulitple": true, the datatable collects the date correctly, except for the fact that i comes as two rows in the table, one with each name.

The code I'm using is:

Editor::inst( $db, 'Teams', 'id' )
    ->fields(
        Field::inst( 'Users.name' ),
        Field::inst( 'TeamLeaders.user_id' )
            ->options(Options::inst()
                ->table('Users')
                ->value('id')
                ->label('name')
            )
    )
    ->leftJoin('TeamLeaders', 'TeamLeaders.team_id', '=', 'Teams.id')
    ->leftJoin('Users', 'Users.id', '=', 'TeamLeaders.user_id')

Please note that this code might not exactly reflect the image i've added as the database structure is a lot more complex than that.

Thanks in advance!

Regards

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Answer ✓

    Hi,

    I think that what you'd need to do in this case is have a DataTable for teams which will also show the users assigned to that team (using an Mjoin) and then have a second DataTable showing the teams where you'd be able to assign the team leaders for a team. That's going to be the easiest way at least.

    It will be possible to do what you are looking for on a single form (two different Mjoins) but you'd need to use some extra Ajax to update the list of possible team leaders from the list of members of the team (as that value changes). Its not impossible, but its probably also not entirely trivial.

    Allan

  • KarlogKarlog Posts: 9Questions: 4Answers: 0

    Alright, thanks a bunch!

This discussion has been closed.