Editor. Select. Default Value.

Editor. Select. Default Value.

roadjanroadjan Posts: 9Questions: 3Answers: 0

Hi.

I am using serverside data with Editor, all working great with various options and select dropdowns pulling data from the database. When I edit a row of data that already has data in the dropdown, how do I get it to show the data in the database - the dropdown currently show a list of the options, but not the value that already exisits in the row of data.

Answers

  • colincolin Posts: 12,621Questions: 0Answers: 2,156

    That shouldn't be the case, it sounds like the format being returned for the edit isn't as expected. It would be worth looking at this example, as it is doing just that. If that doesn't help, could you post your code and the format of the data sent from the server, please.

    Colin

  • roadjanroadjan Posts: 9Questions: 3Answers: 0

    Everything is OK until I introduce a select into the fields
    type: "select"
    If I leave the select out it returns to correct values in editor.

  • roadjanroadjan Posts: 9Questions: 3Answers: 0
    edited August 19
    editor = new $.fn.dataTable.Editor( {
        "ajax": "../../../admin/editor/php/staff.php",
        "table": "#example",
        template: '#customForm',
        "fields": [
            { label: "Company", name: "customers.company_name" },            
            { label: "Address", name: "customers.billing_address_1" },            
            { label: "Address", name: "customers.billing_address_2" },                    
            { label: "Town",  name: "customers.billing_town"  },      
            { label: "County",  name: "customers.billing_county" },            
            { label: "Postcode", name: "customers.billing_postcode" },   
            { label: "Address", name: "customers.delivery_address_1" },            
            { label: "Address", name: "customers.delivery_address_2" },
            { label: "Town",  name: "customers.delivery_town" },     
            { label: "County",  name: "customers.delivery_county" },            
            { label: "Postcode", name: "customers.delivery_postcode" },
            { label: "Contact Email", name: "customers.contact_email_address" },
            { label: "Invoice Email", name: "customers.invoice_email" },
            { label: "Statement Email", name: "customers.statement_email" },
            { label: "Telephone", name: "customers.telephone" },
            // { label: "Price List ID", name: "customers.customer_price_list_id" },
            { label: "Price List", name: "price_lists.price_list_name", type: "select" },
            // { label: "Payment Terms", name: "customers.payment_terms" },
            { label: "Payment", name: "payment_terms.payment_terms_name", type: "select" }
        ]
    } );
    
  • roadjanroadjan Posts: 9Questions: 3Answers: 0
        Editor::inst( $db, 'customers', 'customers.customer_id' )    
    
        ->leftJoin( 'price_lists', 'price_lists.price_list_id', '=', 'customers.customer_price_list_id' )   
        ->leftJoin( 'payment_terms', 'payment_terms.payment_terms_id', '=', 'customers.customer_payment_terms_id' )
    
        ->fields(
    
        Field::inst( 'customers.customer_id' )->set( false ),
        Field::inst( 'customers.customer_price_list_id' ),
        Field::inst( 'customers.customer_payment_terms_id' ),
    
        Field::inst( 'price_lists.price_list_name' )
            ->options( Options::inst()
                ->table( 'price_lists' )
                ->value( 'price_list_id' )
                ->label( 'price_list_name' )
            ),        
    
        Field::inst( 'payment_terms.payment_terms_name' )
            ->options( Options::inst()
                ->table( 'payment_terms' )
                ->value( 'payment_terms_id' )
                ->label( 'payment_terms_name' )
            ),   
    

    ...

  • allanallan Posts: 54,703Questions: 1Answers: 8,557 Site admin

    This error is usually because of a mix up in the label and the value. Is payment_terms.payment_terms_name really the field you want to edit the value of here? I would have thought that you would want to edit customers.customer_payment_terms_id which would have its options based on the payment_terms table. For example:

    {
      label: "Payment",
      name: "customers.customer_payment_terms_id",
      type: "select"
    }
    

    And on the server-side:

    Field::inst( 'customers.customer_payment_terms_id' )
        ->options( Options::inst()
            ->table( 'payment_terms' )
            ->value( 'payment_terms_id' )
            ->label( 'payment_terms_name' )
        ),
    

    Allan

  • roadjanroadjan Posts: 9Questions: 3Answers: 0

    Hi Allan, thanks for coming back to me. Tis makes sense. I have made the changes but now I have lost both fields on the editor. Here is the JS

        "fields": [
            { label: "Company", name: "customers.company_name" },            
            { label: "Address", name: "customers.billing_address_1" },            
            { label: "Address", name: "customers.billing_address_2" },                    
            { label: "Town",  name: "customers.billing_town"  },      
            { label: "County",  name: "customers.billing_county" },            
            { label: "Postcode", name: "customers.billing_postcode" },   
            { label: "Address", name: "customers.delivery_address_1" },            
            { label: "Address", name: "customers.delivery_address_2" },
            { label: "Town",  name: "customers.delivery_town" },     
            { label: "County",  name: "customers.delivery_county" },            
            { label: "Postcode", name: "customers.delivery_postcode" },
            { label: "Contact Email", name: "customers.contact_email_address" },
            { label: "Invoice Email", name: "customers.invoice_email" },
            { label: "Statement Email", name: "customers.statement_email" },
            { label: "Telephone", name: "customers.telephone" },
    
            { label: "Price List", name: "customers.customer_price_list_id", type: "select" },
            { label: "Payment", name: "customers.customer_payment_terms_id", type: "select" }
    
        ]
    

    Here is the PHP

        Editor::inst( $db, 'customers', 'customers.customer_id' )    
    
        ->leftJoin( 'price_lists', 'price_lists.price_list_id', '=', 'customers.customer_price_list_id' )   
        ->leftJoin( 'payment_terms', 'payment_terms.payment_terms_id', '=', 'customers.customer_payment_terms_id' )
    
        ->fields(
    
        Field::inst( 'customers.customer_id' )->set( false ),
    
        Field::inst( 'customers.customer_payment_terms_id' )
            ->options( Options::inst()
                ->table( 'payment_terms' )
                ->value( 'payment_terms_id' )
                ->label( 'payment_terms_name' )
            ),        
    
        Field::inst( 'customers.customer_price_list_id' )
            ->options( Options::inst()
                ->table( 'price_lists' )
                ->value( 'price_list_id' )
                ->label( 'price_list_name' )
            ),  
    
  • allanallan Posts: 54,703Questions: 1Answers: 8,557 Site admin

    When you say you have lost them, do you mean they no longer appear in the form? I don't see anything in the above that would cause that.

    Can you give me a link to your page so I can take a look at debug it directly please?

    Thanks,
    Allan

  • roadjanroadjan Posts: 9Questions: 3Answers: 0

    Here you go Allan. I have removed the login for you to test, please notify me when you are done. Thanks.

    https://thebusinessmanual.co.uk/admin/index.php?customers

  • allanallan Posts: 54,703Questions: 1Answers: 8,557 Site admin

    Thanks!

    You've got a custom template and two fields are commented out:

                                <div id="customForm">
                                    <div class="row">
                                        <div class="name col-4">
                                            <h4>Name</h4>
                                            <editor-field name="customers.company_name"></editor-field>
                                            <!-- <editor-field name="customers.customer_price_list_id"></editor-field> -->
                                            
                                            <editor-field name="customers.contact_email_address"></editor-field>
                                            <editor-field name="customers.invoice_email"></editor-field>
                                            <editor-field name="customers.statement_email"></editor-field>
                                            <editor-field name="customers.telephone"></editor-field>
                                            <!-- <editor-field name="customers.payment_terms"></editor-field> -->
                                            <editor-field name="price_lists.price_list_name"></editor-field>
                                            <editor-field name="payment_terms.payment_terms_name"></editor-field>
                                        </div>                    
                                        <div class="office col-4">
                                            <h4>Billing Address</h4>
                                            <editor-field name="customers.billing_address_1"></editor-field>
                                            <editor-field name="customers.billing_address_2"></editor-field>
                                            <editor-field name="customers.billing_town"></editor-field>
                                            <editor-field name="customers.billing_county"></editor-field>
                                            <editor-field name="customers.billing_postcode"></editor-field>
                                        </div>
                                        <div class="hr col-4">
                                            <h4>Delivery Address</h4>
                                            <editor-field name="customers.delivery_address_1"></editor-field>
                                            <editor-field name="customers.delivery_address_2"></editor-field>
                                            <editor-field name="customers.delivery_town"></editor-field>
                                            <editor-field name="customers.delivery_county"></editor-field>
                                            <editor-field name="customers.delivery_postcode"></editor-field>
                                        </div>
                                    </div>
                                </div>       
    

    The second one (customers.payment_terms) needs to be updated to be customers.customer_payment_terms_id.

    Nice use of a template btw - it looks good.

    Allan

  • roadjanroadjan Posts: 9Questions: 3Answers: 0

    Thank you. All fixed. Such a fantastic tool - huge kudos to all involved that developed it.

Sign In or Register to comment.