How to sort fields after editor.clear() / editor.add()?

How to sort fields after editor.clear() / editor.add()?

GargiucnGargiucn Posts: 109Questions: 30Answers: 0

I am writing a kind of survey that you can construct by defining the question and the type of answer.
It is possible to define an "answer" field of type text, textarea, radio and checkbox and to change the type of field in editor first I define the "answer" field as hidden and then in "open" I delete it and then redefine it as the type of field I am interested in.

var domEditor = new $.fn.dataTable.Editor( {
...
  table: '#isorapptestdom', 
  fields: [
    {
      label: "type",
      name: "type",
      type: "text"
    },
    {
      label: "question",
      name: "question",
      type: "textarea"
    },
    {
      label: "answer",
      name: "answer",
      type: "hidden"
    },
    {
      label: "other field",
      name: "other field",
      type: "select"
    }
...
} );

domEditor.on( 'open', function ( e, mode, action ) {
    let type = domEditor.val("type");
    domEditor.clear("answer");
    if (type == "input"){   
        domEditor.add( {
          label: "answer",
          name: "answer"
        } );
    }   
    if (type == "textarea"){
        domEditor.add( {
          label: "answer",
          name: "answer",
          type: "textarea"
        } );
    }   
...
} );

The problem arises with the "other field" field that I would like to appear below the "answer" field in the editor.
If I delete it and recreate it in "open" under the "answer" field, it no longer performs the select.

Any advice?
Thank you,

Giuseppe

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    Hi Giuseppe,

    The add() method has an optional second parameter which can be used to tell Editor which field the newly added field should be inserted after. I think that can be used to resolve the issue here.

    Allan

  • GargiucnGargiucn Posts: 109Questions: 30Answers: 0

    Thank you for your response.
    The problem is that using field.add() the field "answer" is placed at the bottom while I would like to add the field "other field" after "answer".
    The second parameter of add() allows you to place "answer" as the first question (and I don't want that) or after a certain field (but I would like it before "other field").
    As I wrote before the only way to have "other field" after "answer" is to use field.clear()/field.add() also for "other field" and to create it after "answer".
    Unfortunately, by doing this the "select" no longer works....

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Answer ✓

    Could you not do:

    domEditor.add( {
              label: "answer",
              name: "answer",
              type: "textarea"
            }, 'answer' );
    

    answer, although hidden, still has a position in the form.

    Allan

Sign In or Register to comment.