Add "options" from DB Table and more custom options

Add "options" from DB Table and more custom options

karasukarasu Posts: 27Questions: 2Answers: 0

Hi,

to add a select box with options from DB table is easy with following code:

JS

var editor = new $.fn.dataTable.Editor( {
    ajax: 'php/table.example.php',
    table: '#example',
    formOptions: {
        main: {
            focus: 1,
        }
    },
    fields: [
        {
            "label": "Label:",
            "name": "example",
            "type": "select",
            "options": {
                "example": [{
                    "label": "4201",
                    "value": "4201"
                }]
            }
        }
    ]
} );

PHP

Editor::inst( $db, 'example', 'id' )
    ->fields(
        Field::inst( 'example' )
            ->options( 'example', 'name', 'example' )
    )

But how can I add more options ("names")

JS

"options": [
    "xy1",
    "xy2",
    "xy3"
]

PHP

Editor::inst( $db, 'example', 'id' )
    ->fields(
        Field::inst( 'beschreibung' )
            ->validator( 'Validate::notEmpty' )
    )

Is this possible or must I use only the last solution?

Thank you

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    ->options( 'example', 'name', 'example' )
    

    This will show all options available in the example table. To add more names, you'd add more entries to that table.

    Allan

  • karasukarasu Posts: 27Questions: 2Answers: 0

    Thank you Allan, this is clear, if I add to more entries the table "example" that also these are displayed.
    But, the other users will also see this.
    Therefore, I wanted to show the entries from the table example and manually add entries in the code.

    Anyway, thanks for the answer.

    It is not optimal but I have now added all the entries from the table example manually in the code.

    Thank you

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Answer ✓

    To do that, what you would need to do is use the ->options() method with a function. That way you can read the information needed from the database, and then modify the array of entries, adding in whatever extra values are needed.

    Allan

  • mihalispmihalisp Posts: 127Questions: 22Answers: 0

    I also need that please.

    I want to add an extra value in options list returned by db.

        {
                label: "Kind:",
                name: "id",
                type: "select" ,
                def: null //i want this extra value (null or 0) which does not exist in db (options)
                                 }, 
    
  • mihalispmihalisp Posts: 127Questions: 22Answers: 0

    Don't bother ,i found this https://datatables.net/forums/discussion/34404/editor-inline-select-field-options and it worked like a charm!

This discussion has been closed.