dependent show/hide/disable/enable

dependent show/hide/disable/enable

resqonlineresqonline Posts: 60Questions: 15Answers: 0

I have a field with a dependent function to show a field if a certain value is chosen in another field - so far so good. However this dependant field gets a value from the data and if that value is > 0 it enters the value but then the field should be disabled for editing. I feel that show() and disable() are kind of cancelling each other out? What's the best approach to achieve this? Is there some kind of 'chain of command' to be considered?

editor.dependent('status', function (val, data, callback, e) {
                editor.field('status').processing(false);
                if (val == 'storno') {
                    editor.field('status').processing(true);
                    
                    // First check if booking already has a stornocost value in booking_details
                    var currentStornocost = 0;
                    if (data.values.booking_details && data.row.booking_details.stornocost) {
                        currentStornocost = parseFloat(data.values.booking_details.stornocost) || 0;
                    }
                        
                    // If stornocost already exists, use it directly
                    if (currentStornocost > 0) {
                        editor.field('stornocost').show();
                        editor.field('stornocost').val(currentStornocost);
                        editor.field('stornocost').disable(); // Make read-only since it's from stored data
                        editor.field('status').processing(false);
                    } else {
                        // Otherwise, calculate it from server
                        $.ajax({
                            url: datatablesajax.url,
                            type: 'POST',
                            data: {
                                action: 'getstornocost',
                                data: data.values,
                            },
                            dataType: 'json',
                            success: function (responseData) {
                                editor.field('stornocost').show();
                                editor.field('stornocost').val(responseData.values.stornocost);
                                editor.field('stornocost').enable(); // Allow editing of calculated values
                            }
                        });
                    }
                }
            });

Answers

  • resqonlineresqonline Posts: 60Questions: 15Answers: 0

    ah... one has to correctly call the data :D :D

    this is now working:

    editor.dependent('status', function (val, data, callback, e) {
        editor.field('status').processing(false);
        if (val == 'storno') {
            editor.field('status').processing(true);
            
            // First check if booking already has a stornocost value in booking_details
            var currentStornocost = 0;
            if (data.row.booking_details && data.row.booking_details.stornocost) {
                currentStornocost = parseFloat(data.row.booking_details.stornocost) || 0;
            }
                
            // If stornocost already exists, use it directly
            if (currentStornocost > 0) {
                editor.field('stornocost').show();
                editor.field('stornocost').val(currentStornocost).disable(); // Make read-only since it's from stored data
                editor.field('status').processing(false);
            } else {
                // Otherwise, calculate it from server
                $.ajax({
                    url: datatablesajax.url,
                    type: 'POST',
                    data: {
                        action: 'getstornocost',
                        data: data.values,
                    },
                    dataType: 'json',
                    success: function (responseData) {
                        editor.field('stornocost').show();
                        editor.field('stornocost').val(responseData.values.stornocost);
                        editor.field('stornocost').enable(); // Allow editing of calculated values
                        editor.field('status').processing(false);
                    }
                });
            }
        }
    });
    
  • allanallan Posts: 65,298Questions: 1Answers: 10,827 Site admin

    Thanks for the update. Good to hear you have it working now :)

    Allan

Sign In or Register to comment.