Laravel Eloquent - Options for fields of select type

Laravel Eloquent - Options for fields of select type

cent89cent89 Posts: 19Questions: 6Answers: 0

Hi, I have a BollaAcquisto model that have a foreign key 'fornitore_id' in Fornitore table.
In laravel, I have defined that relationship:

public function fornitore(){ return $this->hasOne('Modules\Prodotto\Entities\Fornitore', 'id'); }

In query() function of BollaDataTable class, I have:

public function query(BollaAcquisto $model) { return BollaAcquisto::with('fornitore')->select('bolla_acquisto.*'); }

When I show the datatable, I have the fornitore.nome instead of fornitore_id, right.
The fornitore.nome field, in the editor, have type=select, so I would select the fornitore_id value from Fornitore table.

If I click on the select, no options are shown. So, how I can specify the options to Datatables editor?

Thanks a lot

Answers

  • kthorngrenkthorngren Posts: 20,247Questions: 26Answers: 4,760

    This example shows an example of the options for a select field.
    https://editor.datatables.net/examples/simple/fieldTypes.html

    The docs are here:
    https://editor.datatables.net/reference/field/select

    Its not clear to me if you are building the options list. Maybe you can post your Editor code and an example of the options you are building.

    Kevin

  • cent89cent89 Posts: 19Questions: 6Answers: 0

    Thanks for the answer.
    The question is where I can define the options for select fields.
    My app is build in Laravel; the js in index.blade.php:

    var editor = new $.fn.dataTable.Editor({
                        ajax: "{{route('bollaacquisto.index')}}",
                        table: "#bollaacquisto",
                        display: "bootstrap",
                        fields: [
                            {label: "id", name: "id"},
                            {label: "data", name: "data", type:"datetime"},
                            {label: "fornitore.nome", name: "fornitore.nome", type:"select"},
                            {label: "importo:", name: "importo"},
                            {label: "confermata:", name: "confermata", type:"checkbox"}
                        ]
                    });
    
               $('#bollaacquisto').on('click', 'tbody td:not(:first-child)', function (e) {
                        editor.inline(this);
                    });
    
                    {{$dataTable->generateScripts()}}
    

    This the code in BollaDataTable.php

    public function query(BollaAcquisto $model)
        {
            return BollaAcquisto::with('fornitore')->select('bolla_acquisto.*');
        }
    
    public function html()
        {
            return $this->builder()
                        ->columns($this->getColumns())
                        ->minifiedAjax()
                        ->parameters([
                            'dom' => 'Bfrtip',
                            'order' => [1, 'asc'],
                            'select' => [
                                'style' => 'os',
                                'selector' => 'td:first-child',
                            ],
                            'buttons' => [
                                ['extend' => 'create', 'editor' => 'editor'],
                                ['extend' => 'edit', 'editor' => 'editor'],
                                ['extend' => 'remove', 'editor' => 'editor'],
                            ]
                        ]);
        }
    
    protected function getColumns()
        {
            return [
                [
                    'data' => null,
                    'defaultContent' => '',
                    'className' => 'select-checkbox',
                    'title' => '',
                    'orderable' => false,
                    'searchable' => false
                ],
                'id',
                'data',
                'fornitore.nome',
                'importo',
                'confermata',
            ];
        }
    
    
    

    The BollaDataTablesEditor.php has empty functions.
    I'va used this tutorial: https://yajrabox.com/docs/laravel-datatables/master/editor-tutorial

    Thanks.

This discussion has been closed.