Is there any example of combination multiple selection and custom label / value properties?

Is there any example of combination multiple selection and custom label / value properties?

meakceymeakcey Posts: 3Questions: 2Answers: 0
edited November 28 in Editor

Hi

I am trying to use datatable as an input of another table.
I have already joined 2 tables with mjoin and could make multiple selection but just label / value properties is not enough?

Some portion of my registrations table is like

Server Script

Field::inst( 'registrations.userid' ),
Field::inst( 'users.id' ),
Field::inst( 'users.name' )
    ->validator( Validate::notEmpty( ValidateOptions::inst()->message( 'Full-name is required' ))),
Field::inst( 'users.email' )
->leftJoin( 'users', 'users.id', '=', 'registrations.userid')
->join(
    Mjoin::inst( 'packages' )
    ->link( 'registrations.userid', 'package_slots.userid')
    ->link( 'packages.code', 'package_slots.code')
    ->fields(
        Field::inst( 'code' )
            ->validator( Validate::required() )
            ->options( Options::inst()
                ->table( 'packages' )
                ->value( 'code' )
                ->label( 'title' )
            ),
        Field::inst( 'title' ),
        Field::inst( 'package_slots.code' ),
        Field::inst( 'package_slots.price' ),
        Field::inst( 'package_slots.checkin' ),
        Field::inst( 'package_slots.checkout' ),
        Field::inst( 'package_slots.created_at' )
    )
)

Front end

 var registrationsEditor = new DataTable.Editor({
       ajax: '/assets/vendor/datatables/controllers/registrations.php',
       table: '#table_registrations',
       fields:[
           {label: 'ID', name: 'registrations.userid', type:'hidden'},
           {label: 'Name', name: 'users.name', required: true},
           {label: 'Email', name: 'users.email', required: true},
           {label: 'Packages', name: 'packages[].code', type: 'datatable', multiple: true, 
                    config: { 
                        paging: false, 
                        scrollY: 300,
                        columns: [
                            { title: 'Code', data: 'value' },
                            { title: 'Description', data: 'label' }
                        ]
                    } 
                },
]});

When editor opened i could select multiple packages and save them to package_slots
But packages and registrations are only connected with userid and package.code
I need to add other inputs like price checkin checkout while adding packages to registration.
Just pairing them with userid and code is removing other fields when updating.

I have tried to keep other fields as getting on initEdit and updating after postEdit
That works if existing package is selected. When another package is selected, other fields of package_slots are not being saved even they corresponds to package.

In summary
I need combination of these 2 examples in multiple field manner
https://editor.datatables.net/examples/datatables/mJoin.html
https://editor.datatables.net/examples/datatables/properties.html

Any help appreciated
Thanks.

Answers

  • meakceymeakcey Posts: 3Questions: 2Answers: 0
    edited November 28

    Some ideas
    If I could related fields from label and distribute through columns that maybe works

    What i mean is

    ->join(
        Mjoin::inst( 'packages' )
        ->link( 'registrations.userid', 'package_slots.userid')
        ->link( 'packages.code', 'package_slots.code')
        ->fields(
            Field::inst( 'code' )
                ->validator( Validate::required() )
                ->options( Options::inst()
                    ->table( 'packages' )
                    ->value( 'code' )
                    ->label( ['title', 'price'] )
                ),
            Field::inst( 'title' ),
            Field::inst( 'price' ),
            Field::inst( 'package_slots.code' ),
            Field::inst( 'package_slots.price' ),
            Field::inst( 'package_slots.checkin' ),
            Field::inst( 'package_slots.checkout' ),
            Field::inst( 'package_slots.created_at' )
        )
    )
    
    {label: 'Packages', name: 'packages[].code', type: 'datatable', multiple: true, 
    config: { 
       paging: false, 
       scrollY: 300,
       columns: [
             { title: 'Code', data: 'value' },
             { title: 'Description', data: 'label.title' },
             { title: 'Price', data: 'label.price'}
         ]
     } 
    },
    

    But I cannot split label

  • allanallan Posts: 63,700Questions: 1Answers: 10,501 Site admin

    Your link table (package_slots) has information other than just the two foreign keys. As such do not use Mjoin to perform the editing. It will wipe out the other information. Use it for loading data, that is fine, but definitely add -set(false) to the Mjoin instance.

    The reason for that is the way Mjoin works. It deletes all links and then reinserts the new ones. That is very certainly not what you want.

    What you do what to do, is have a look at this example which I think is more or less what you want. In my case, I've got the child Editor working on the users table - in your case you would have it working on package_slots.

    Allan

Sign In or Register to comment.