Set editor field instance type on preOpen ?

Set editor field instance type on preOpen ?

crush123crush123 Posts: 417Questions: 126Answers: 18
edited October 2016 in Editor

As a follow up to my last question https://datatables.net/forums/discussion/38229/custom-validation-on-the-fly#latest I have the validation working great.

My icing on this particular cake would be to show a field type pertinent to the data in the editor instance.

At the moment, the field is always a text field, which works, but if possible, i would like to show a checkbox if the data type has been set to boolean, or a select list, or whatever.

I have been looking at preOpen and modifier, and by this method, I can set the field instance label, value etc, as per https://editor.datatables.net/reference/api/ but I can't see a way to set the field instance type

eg

editor.on( 'preOpen', function ( e )  { 
var mode = editor.mode(); // Gets editor mode, (create, edit, remove)
var modifier = editor.modifier();  // Gets the selected row of the table
if ( modifier ) {
    // Get data for this row
    var data = table.row( modifier ).data();
    var opttype = data.refglobaloptions.OptionType;
    console.log( opttype );
    var optval = editor.field('refglobaloptions.OptionValue');
    if (opttype == 1) {
    optval
        .label('hello');
    }
....
}

} );

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Answer ✓

    I can't see a way to set the field instance type

    You can't dynamically change the field's type after initialisation. You'd need to remove the field (clear()) and then add a new one of the new type (add()).

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited October 2016

    Great,

    here is a simplified version of what i will do, no errors, but the field value does not appear in the editor field instance

    editor.on( 'preOpen', function ( e )  { 
    var mode = editor.mode(); // Gets editor mode, (create, edit, remove)
    var modifier = editor.modifier();  // Gets the selected row of the table
    if ( modifier ) {
        // Get data for this row
        var data = table.row( modifier ).data();
        var opttype = data.refglobaloptions.OptionType;
        console.log( opttype );
        editor.clear( 'refglobaloptions.OptionValue' );
        if (opttype == 1) {
            editor.add( {
                label: "Value (Integer):",
                name: "refglobaloptions.OptionValue",
                data: "refglobaloptions.OptionValue"
            });
        }
        if (opttype == 2) {
            editor.add( {
                label: "Value:",
                name: "refglobaloptions.OptionValue",
                data: "refglobaloptions.OptionValue",
                type: "select",
                options: [
                    { label: "0 (False)", value: "0" },
                    { label: "1 (True)", value: "1" }
                    ]
            });
        }
        if (opttype == 3) {
            editor.add( {
                label: "Value (Text):",
                name: "refglobaloptions.OptionValue",
                data: "refglobaloptions.OptionValue"
            });
        }
    
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Yes, you'd need to read the value and then set it when you add the new field. It won't (currently) automatically pick up the value. I might change that behaviour!

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    cool, easy enough.

    Thanks again.

This discussion has been closed.