Conditional Options on Editor Field

Conditional Options on Editor Field

wolphitdepartmentwolphitdepartment Posts: 6Questions: 2Answers: 0

Hello. How would I the options for select dependent on a field's value?

`
(function($){

$(document).ready(function() {

var myEditor = new $.fn.dataTable.Editor( {
    serverSide: true,
    ajax: 'myphpfile.php',
    table: '#my_table',=
    fields: [
        {
            "label": "Status",
            "name": "mytable.status",
            "type": "select",
            "options": [
                if (mytable.status=="OKAY") {
                    { label: "A", value: "A" },
                    { label: "B", value: "B" },
                }
                else if (mytable.status="NOT OKAY)") {
                    { label: "C", value: "C" },
                    { label: "D", value: "D" },
                }
            ]
        }
    ]
} );

`

Answers

  • allanallan Posts: 56,851Questions: 1Answers: 9,035 Site admin

    Use dependent(). It provides a way to give a function that can be used to update the Editor form whenever required (e.g. on start edit, create, etc). There is an example available here.

    Allan

  • wolphitdepartmentwolphitdepartment Posts: 6Questions: 2Answers: 0
    edited July 29

    The example on the link you sent is only for hiding/showing editor fields. How do I make this code work to where dependent() API can be used to populate options for a SELECT field?

    editor.dependent( 'mytable.status', function ( val ) {
            if (val === 'APPROVED') {
                // SET SELECT OPTIONS for mytable.status
                mytable.statusoptions [
                    { label: "A", value: "A" },
                    { label: "B", value: "B" },
                ]
            }
        } );
    
  • allanallan Posts: 56,851Questions: 1Answers: 9,035 Site admin

    dependant()'s return value can include an options parameter, or you could call field().update() directly on the field in question:

    editor.dependent( 'mytable.status', function ( val ) {
            if (val === 'APPROVED') {
                editor.field('mytable.status').update([
                    { label: "A", value: "A" },
                    { label: "B", value: "B" },
                ]);
            }
    } );
    

    Allan

  • wolphitdepartmentwolphitdepartment Posts: 6Questions: 2Answers: 0
    edited July 30

    Thank you for your response. It's working. However, I'm using AJAX and upon update, I'm getting this error:

    Field is still processing For more information, please refer to https://datatables.net/tn/16

    I tried to fool around using this code but I don't know where to put the code you provide inside this code here:

    editor.dependent('mytable.status', function(val, data, cb) {
            $.ajax({
                url: '/mydependentapi.php',
                data: data,
                success: function (json) {
                    if (val === 'APPROVED') {
                        editor.field('mytable.status').update([
                            { label: "A", value: "A" },
                            { label: "B", value: "B" },
                        ]);
                    }
                    else if (val === 'PENDING') {
                        editor.field('mytable.status').update([
                            { label: "C", value: "C" },
                            { label: "D", value: "D" },
                        ]);
                    }
                    cb(json);
                }
                
            })
        
        });
    
  • colincolin Posts: 14,458Questions: 1Answers: 2,464

    I can see that error be displayed if the ajax call doesn't succeed - the callback is only being called in the success() function. Are you seeing errors in the browser's console, or any errors in the server-side logs?

    Colin

  • wolphitdepartmentwolphitdepartment Posts: 6Questions: 2Answers: 0

    The condition is working but there's a DTE_Processing_Indicator in the modal and when I checked the browser console after clicking the update button, it shows that field is still processing error.

  • allanallan Posts: 56,851Questions: 1Answers: 9,035 Site admin

    We'd need a link to your page showing the issue to be able to debug the problem. The code above looks correct.

    Allan

Sign In or Register to comment.