Editor - Insert and Edit with multiple selects

Editor - Insert and Edit with multiple selects

peterbrownepeterbrowne Posts: 45Questions: 12Answers: 0

I have two selects for the editor insert and edit function on the template.

Each select corresponds to their own column in the table as shown in the last two columns below:

+-----------------------+------------------------------+-------------------+-----------------------------------+
| learning_event_pk | learning_event_name | rdb_group_fk | rotation_discipline_block_fk |
+-----------------------+------------------------------+-------------------+-----------------------------------+

I want the user to select only one select (either/or case). Perhaps through a radio button group next to the select to enable the select.

For the select that is not enabled, the default value to be inserted should be zero for the select's column. Any ideas how I can achieve this?

var editor = new $.fn.dataTable.Editor( {
                ajax: "php/learning_event_data.php",
                table: "#learning_event_table",
                template: '#learning_event_form',
                fields: [ {
                    label: "Learning Event:",
                    name: "learning_event.learning_event_name"
                }, {
                    label: "Rotation/Discipline/Block Sub-group:",
                    name: "learning_event.rdb_group_fk",
                    type: "select"
                }, {
                    label: "Rotation/Discipline/Block:",
                    name: "learning_event.rotation_discipline_block_fk",
                    type: "select"
                } ]
            } );


Editor::inst( $db2, 'learning_event', 'learning_event_pk' )    
    ->field(
        Field::inst( 'learning_event.learning_event_name' ),
     Field::inst( 'learning_event.rdb_group_fk' )
            ->options( Options::inst()
                ->table( 'rdb_group' )
                ->value( 'rdb_group_pk' )
                ->label( 'rdb_group_name' )
            )
            ->validator( 'Validate::dbValues' ),
        Field::inst( 'rdb_group.rdb_group_name' ),
        Field::inst( 'learning_event.rotation_discipline_block_fk' )
            ->options( Options::inst()
                ->table( 'rotation_discipline_block' )
                ->value( 'rotation_discipline_block_pk' )
                ->label( 'rotation_discipline_block_name' )
            )
            ->validator( 'Validate::dbValues' ),
        Field::inst( 'rotation_discipline_block.rotation_discipline_block_name' )

    )
    ->leftJoin( 'rdb_group', 'rdb_group.rdb_group_pk', '=', 'learning_event.rdb_group_fk' )
    ->leftJoin( 'rotation_discipline_block', 'rotation_discipline_block.rotation_discipline_block_pk', '=', 'learning_event.rotation_discipline_block_fk' )
    ->process($_POST)
    ->json();

This question has an accepted answers - jump to answer

Answers

  • peterbrownepeterbrowne Posts: 45Questions: 12Answers: 0
    edited October 28

    To be clearer, I want the selects to be disabled, then to allow the user to enable one select to use. The disabled select should just default to a value of zero for input.

  • allanallan Posts: 50,737Questions: 1Answers: 7,581 Site admin
    Answer ✓

    Yes - there are two parts to my answer:

    1) You need to have the ability to select (and have it selected by default) a placeholder value of 0. You can do that with the placeholder, placeholderDisabled and placeholderValue options of the select option - for example:

    {
      label: "Rotation/Discipline/Block Sub-group:",
      name: "learning_event.rdb_group_fk",
      type: "select",
      placeholder: 'Select value',
      placeholderDisabled: false,
      placeholderValue: 0
    }
    

    2) You could have a checkbox that will let the user decide which to select, disabling the other via a radio box. See this example for how that can be done.

    Note that with the second part, you might want to set the value to 0 for the field that is disabled in case the user were to select a value from one input and then change the radio box state and select another value. Or you could have some validation to make sure that doesn't happen (probably a good idea on the server-side anyway).

    Allan

  • peterbrownepeterbrowne Posts: 45Questions: 12Answers: 0

    Thanks Alan.

    When I update a record that has no set value for learning_event.rdb_group_fk (the placeholder shows as Select value), the editor returns 'This value is not valid' in red beneath the select for learning_event.rdb_group_fk. I need to have that select record a value of 0 if left at 'Select value'.

  • peterbrownepeterbrowne Posts: 45Questions: 12Answers: 0

    Probably ->validator( 'Validate::dbValues' ), should I disable that or is there a method to allow zero as a value?

    Editor::inst( $db2, 'learning_event', 'learning_event_pk' )    
        ->field(
         Field::inst( 'learning_event.learning_event_name' ),
         Field::inst( 'learning_event.learning_event_outcome' ),
         Field::inst( 'learning_event.rdb_group_fk' )
                ->options( Options::inst()
                    ->table( 'rdb_group' )
                    ->value( 'rdb_group_pk' )
                    ->label( 'rdb_group_name' )
                )
                ->validator( 'Validate::dbValues' ),
            Field::inst( 'rdb_group.rdb_group_name' ),
            Field::inst( 'learning_event.rotation_discipline_block_fk' )
                ->options( Options::inst()
                    ->table( 'rotation_discipline_block' )
                    ->value( 'rotation_discipline_block_pk' )
                    ->label( 'rotation_discipline_block_name' )
                )
                ->validator( 'Validate::dbValues' ),
            Field::inst( 'rotation_discipline_block.rotation_discipline_block_name' )
    
        )
        ->leftJoin( 'rdb_group', 'rdb_group.rdb_group_pk', '=', 'learning_event.rdb_group_fk' )
        ->leftJoin( 'rotation_discipline_block', 'rotation_discipline_block.rotation_discipline_block_pk', '=', 'learning_event.rotation_discipline_block_fk' )
        ->process($_POST)
        ->json();
    
  • peterbrownepeterbrowne Posts: 45Questions: 12Answers: 0

    OK, I changed the validator for that field to ->validator( 'Validate::notEmpty' ), and it works OK.

Sign In or Register to comment.