On Create only - set select2 data

On Create only - set select2 data

RichJCGRichJCG Posts: 12Questions: 3Answers: 0

Essentially, I have 2 tables that load projects and upon selecting a project row loads the set cost codes into the second table. All works properly on edit and remove.
We have a standard list of cost codes that I would like to load into a select2 and show only when I Create a new record. This would then use the code and description from the standard list and add to the projects code table. I feel comfortable getting the ajax data loaded but not sure where to start to setup the form to only show the standard list of codes on create only. Any help in pointing me in the right direction is greatly appreciated.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,208Questions: 1Answers: 10,415 Site admin
    Answer ✓

    There are a few options here:

    1. Use two different instances of Editor - one for the create action which has the Select2 field configured and all the others, and one for the edit action which doesn't have the Select2 field configured. This is probably the easiest, but also also most redundant in that you'll be repeating field definitions.
    2. If you don't care about it being submitted to the server on server (i.e. it will be) then just show and hide the field (field().show() and field().hide()) - use the initCreate and initEdit events to call those methods.
    3. You could also use initCreate to dynamically add the Select2 field (add()) and then remove it (clear()) on submitComplete.

    Regards,
    Allan

  • RichJCGRichJCG Posts: 12Questions: 3Answers: 0

    Great information - field().show() actually helps with something else I'm just starting to work on as well.
    Agree on Option 1 probably being the easiest.
    If I went with option 2 my question would be - How do I load an unrelated table into the php Editor instance?
    Something like below where projects is the main table and eq_rental would be the unrelated table? Not sure if that would work as I wouldn't be able to join those two tables.

    Editor::inst($this->editorDb, 'projects', 'pid')
    ->field(
      Field::inst('projects.pname'),
      Field::inst('eq_rental.cid')
        ->options(Options::inst()
            ->table('eq_rental')
            ->value('cid')
            ->label('name')
        )
    )
    
  • allanallan Posts: 63,208Questions: 1Answers: 10,415 Site admin
    Answer ✓

    Not quite sure what you mean by "unrelated" in this case I'm afraid. You can load "related" information from another database table using a left join - but you wouldn't have a single form controlled data for two different and unreferenced tables.

    Allan

  • RichJCGRichJCG Posts: 12Questions: 3Answers: 0

    By "unrelated" I mean there is no way to join them together, as you stated that's not going to work.
    I think I will go with initCreate and add the field dynamically. The edit action will only happen once the code has been added to the project so shouldn't need anything out of the ordinary there.
    Many thanks.

  • allanallan Posts: 63,208Questions: 1Answers: 10,415 Site admin

    Daft question perhaps, but if you can't join them together, how do you combine them to display in a single table row?

    Allan

  • RichJCGRichJCG Posts: 12Questions: 3Answers: 0

    Not actually showing two sets of data in a single row.
    The standard list of codes table only has two pieces of information - the code number (01-1000) and the description (Safety Labor). That code gets selected and then the budget, labor hours, etc is added and stored in the project_code table. So I'm copying the standard code information into the project_code table.
    I'm doing it this way as we don't allow non-standard codes to be added to the projects. Once it's added they can edit and change budget or total labor hours - they just can't change the code. That's why I only need it to happen when creating a new code.
    I could reference them to each other but there are custom descriptions that can be modified per project so it's not always matching.

  • allanallan Posts: 63,208Questions: 1Answers: 10,415 Site admin

    I'm with you now - thanks for the explanation. initCreate sounds like a good approach.

    Regards,
    Allan

  • RichJCGRichJCG Posts: 12Questions: 3Answers: 0

    So here's my next question(s). I've added

    codeEditor.on('initCreate', function(){
        $.ajax( {
            url: '".site_url()."manage/get_standard_codes',
            success: function(data) {
                codeEditor
                    .field('code.code')
                    .update(data.code);
            }
            });
        });
    

    below my codeEditor definition. I am able to see the code data come through

    {"code":[{"code":"00-1000","description":"SOLICITATION"},
    {"code":"00-1113","description":"ADVERTISEMENT FOR BIDS"},
    

    I get no errors but unable to get the codes to load into the select.

    fields: [
                {
                    type: 'select',
                    label: 'Code:',
                    name: 'code.code'
                },
    

    Maybe too many "codes" haha. Any pointers greatly appreciated.

  • allanallan Posts: 63,208Questions: 1Answers: 10,415 Site admin

    By default Editor looks for label and value properties in the object, while you have code and description. You could change them, map them or use the optionsPair object of select to define your own code and description properties as the ones to use.

    Allan

This discussion has been closed.