Populating select element from database table

Populating select element from database table

TomBajzekTomBajzek Posts: 164Questions: 37Answers: 1

I need to populate a select element from another (unrelated) table. This other table is just a list of valid entries for the select field. I think that I need to return an array that contains the entries that should populate the options of the field in the Editor. It seems that a function could return that array. However, I don't have a sense of how to get the contents of this other, unrelated table in the context of an Editor for the table I'm working on.

Consider that I have a 'category' field in Table A. I also have a Category table that contains the list of valid entries for the 'category' field of Table A. In the Editor for Table A, I need to offer a select element containing the contents of the 'category' table.

I can easily do this outside of DataTables/Editor, but I need to do it within the Editor.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,457Questions: 1Answers: 10,465 Site admin
    Answer ✓

    Assuming you are using the PHP or .NET libraries for Editor, you can do this with the Options class.

    Allan

  • TomBajzekTomBajzek Posts: 164Questions: 37Answers: 1

    Allan,

    I think I understand how to handle the category instance (I only need the category name to go into the category field in the tickets table, which is not fully shown):

            Field::inst( 'category' )
                ->options(Options::inst()
                    ->table('categories')
                    ->value('name')
                    ->label('name')
                    ->order('name asc')
                    ->render( function ($row) {
                        return $row['name'];
                    } )
                ),
    

    What I'm not understanding is how to use this idea with a separate table that is not related to/joined to the original table.
    Thanks,
    Tom

  • TomBajzekTomBajzek Posts: 164Questions: 37Answers: 1

    Allan,

    OK. I see that it just works! I had thought I would need some way to call something that would return the category array as json and then populate the array with that. It seems that Editor is able to figure that out on its own, without the need of a join. That's wonderful, and I'm sorry I didn't expect that from the beginning so as not to bother you.

    I really like the DataTables/Editor software, and the more I do, the more I like it, even if I have to ask too many questions.

    Thanks,
    Tom

  • allanallan Posts: 63,457Questions: 1Answers: 10,465 Site admin

    Great to hear you've got it working. Delighted to hear that you are enjoying using it!

    Allan

  • Markus EvacosoMarkus Evacoso Posts: 11Questions: 3Answers: 0

    If I may add a question about the Render () method in C# .net / Options:

    https://editor.datatables.net/docs/1.7.0/net/html/548cc5ab-9cc6-c58d-28da-acfd0789669b.htm

                        .Field(new Field("fk_bereich")
                            .Options(new Options()
                                .Table("tbl_Bereiche")
                                .Value("PK_Bereich")
                                .Label("Beschreibung")
                                .Order("PK_Bereich asc")
                                .Render()
                            )
                        )
    

    I'm wondering how the Render function is to use. Could you give me a hint, Allan?

    Best,
    Markus

  • allanallan Posts: 63,457Questions: 1Answers: 10,465 Site admin

    Hi Markus,

    With a lambda function:

    new Field("users.site")
        .Options(new Options()
            .Table("sites")
            .Value("id")
            .Label(new []{"name", "country"})
            .Render(row => row["name"]+" ("+row["country"]+")")
        );
    

    Documentation is available here.

    Allan

This discussion has been closed.