Setting default value select when dynamically loading data

Setting default value select when dynamically loading data

aschippersaschippers Posts: 22Questions: 8Answers: 3

Hello,

For an inline form edit, I dynamically load data from a json query. I'll do the update with the update field.
Everything is working fine, but I want to set a selected field (default field).


$('#production_delivery_registrations').on('click', 'tbody td:not(:first-child)', function (e) { var tr = $(this).closest('tr'); var row = production_delivery_registrations.row( tr ); var index = production_delivery_registrations.column.index( 'fromVisible', $(this).index() ); var group_user = production_delivery_registrations.cell(this, production_delivery_registrations.column('group_user:name')).data(); var mx_order = production_delivery_registrations.cell(this, production_delivery_registrations.column('mx_order_description:name')).data(); switch(index) { case production_delivery_registrations.column('login:name').index(): case production_delivery_registrations.column('production_count:name').index(): case production_delivery_registrations.column('comment:name').index(): case production_delivery_registrations.column('production_date:name').index(): case production_delivery_registrations.column('production_run:name').index(): if ($('.production_delivery_registrations.show').data('rights_create') == 1) { production_delivery_registrations_modify.inline( this, { buttons: { label: '<em class="fa fa-edit"></em>', fn: function () { this.submit(); } } } ); } case production_delivery_registrations.column('production_run:name').index(): $.ajax({ url: Routes.order_references_order_event_list_path(mx_order, { format: 'json'}), type: 'GET', dataType: 'json', success: function(result) { production_delivery_registrations_modify.field('production_run').update(result); } }); }; });

The webform is

    production_delivery_registrations_modify = new $.fn.DataTable.Editor ({
        ajax: {
            edit: {
                url: $('#production_delivery_registrations').data('source'),
                type: 'PUT'
            }
        },
        table: '#production_delivery_registrations',
        idSrc: 'id',
        fields: [ 
            {
                name: 'production_date',
                type: 'datetime', 
                def:   function () { return new Date(); },
                label: $('#production_date').html(),
            },{
                name: 'user_id',
                label: $('#login').html(),
                type: 'select',
            },
            {
                name: 'mx_order_description',
                label: $('#mx_order_description').html(),
                type: 'readonly',
            },
            {
                name: 'brand_description',
                label: $('#brand_description').html(),
                type: 'readonly',
            },
            {
                name: 'production_count',
                label: $('#production_count').html(),
            },
            {
                name: 'production_run',
                data: 'production_run',
                label: I18n.production_delivery_registrations.table.production_run,
                type: 'select'
            },
            {
                name: 'comment',
                label: $('#comment').html(),
            }
        ]
    });

When adding "def" to the table, the default value is not set, because the data is loaded afterwards
When adding production_delivery_registrations_modify.field('production_run').def(..); in the update, it also doesn't work.

Nice would be, to have a default parameter in the update string.

Regards,

Sander

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi Sander,

    When you say "the update string", do you mean that you want the default value to be submitted when you edit a row? The issue there is that the default value isn't applied since it is an edit. The default value only applies on create.

    The production_run field should be submitting whatever its current value is on edit (assuming that the list of options you are loading has that value available).

    Allan

  • aschippersaschippers Posts: 22Questions: 8Answers: 3

    Production_run contains the correct value, but because the selection list is inserted afterwards, always de first item is chosen.

    If i define in the editor field the options on forehand, it works
    option [ {value: 0, label: "label1"}, {value :1, label: "label 2"} ... ]

    but the select list is dynamically loaded afterwards, always the first one is selected. It's completely logical behaviour, but the question is, how can i set in a select box, loaded async), the selected field.

    One option should be, to add a string of

    <option value="0">Label1</option>
    <option value="1" selected>Label 2</option>
    

    But I don't know how to do that within an editor field.

    Sander

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi Sander,

    Editor does actually have some code in its select field type to handle this sort of thing. If the list of options doesn't include the value selected and then you change the options, it should then select the value correctly. Can you give me a link to your page so I can take a look and see why that isn't working on your page please?

    Thanks,
    Allan

This discussion has been closed.