editor fields def not working

editor fields def not working

sid@nomisma.insid@nomisma.in Posts: 11Questions: 2Answers: 0
edited March 2017 in Free community support

in editor i'm not able to assign a default value using def option in editor during initialization. Looks straight forward to me but is there a gotcha? below is an example usage of mine
fields:[
{
label:"some label",
name:"some name",
def: "default value"
}
]
Everything else works perfectly fine. I was able to integrate well in to my node server.
Just this default value isnt getting populated.

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    From the docs:

    var fname = editor.field( 'first_name' );

    Having got a reference to the field instance, you can then set values:

    fname
    .def( 'No name entered' )

  • sid@nomisma.insid@nomisma.in Posts: 11Questions: 2Answers: 0

    not working.

  • kthorngrenkthorngren Posts: 21,247Questions: 26Answers: 4,929

    Is the field value undefined when editing?

    You code snippet should work. Might need to look at your code to determine the problem.

    Kevin

  • sid@nomisma.insid@nomisma.in Posts: 11Questions: 2Answers: 0
    edited March 2017

    nothing get populated. just blank.
    below is the code , removed all extra fields.(for privacy reasons)

  • sid@nomisma.insid@nomisma.in Posts: 11Questions: 2Answers: 0
    var editor = new $.fn.dataTable.Editor( {
      formOptions : {
        main:{
          //onReturn:false
          submit:'changed',
        }
      },
      ajax:  { 
        edit : {
          type: 'PUT',
          contentType : 'application/json',
          url: '/mysql/tPayments/_id_',
          data: function(data){
            return JSON.stringify(data);
          },
        },
        remove : {
          type: 'DELETE',
          data: function(data){
            // instead of sending entire data just send action
            return {action:"remove"}
          },
          url: '/mysql/tPayments/_id_',
        },
      },
      table: table,
      idSrc: 'id',
      fields: [
        { 
          label: "Haeal Comments",
          name: "haealComments",
          def: "dummy comment",
        },
      ],
    } );
    
  • sid@nomisma.insid@nomisma.in Posts: 11Questions: 2Answers: 0

    Everything works perfectly, even server side curd operations. Just this default value is being stubbon.

  • kthorngrenkthorngren Posts: 21,247Questions: 26Answers: 4,929

    When you create a new row is the field populated with the default value?

    According to fields.def the default will be set when the value is undefined. My understanding is If the value is null or "" then the default won't be set.

    If the value is blank not undefined then you can use something like this to populate the blank filed with a "default" value:

    editor.on('initEdit', function() {
      if (editor.field('haealComments').val() === '') {
        editor.field('haealComments').val('dummy comment');
      }
    });
    

    Kevin

  • sid@nomisma.insid@nomisma.in Posts: 11Questions: 2Answers: 0
    edited March 2017

    In Created Mode it works like a charm.
    But in edit mode, its a disaster.
    "According to fields.def the default will be set when the value is undefined. My understanding is If the value is null or "" then the default won't be set."
    In which world does this make sense?
    I need a default value when something doesn't have value, it could be undefined, null or "". I think you need to change this behaviour asap

  • allanallan Posts: 63,350Questions: 1Answers: 10,443 Site admin

    In edit mode the field should already have a value, and therefore the default isn't used.

    I need a default value when something doesn't have value, it could be undefined, null or ""

    An empty string is a perfectly valid value. The default will not override an empty string nor should. It should however take the place of undefined (I'll need to check what happens with null).

    If you link to a test case showing the issue that would greatly help in our chances of being able to successfully help you resolve this (and if I can improve Editor along the way, so much the better!).

    Allan

  • sid@nomisma.insid@nomisma.in Posts: 11Questions: 2Answers: 0

    @ Assuming "" to be a valid value is a bit tricky.
    Its not always a valid value. Many databases have columns with not null whose default is "" .
    I think you need to check for "" also along with null case. Im sure its super tricky to resolve this.May be you're better off considering it as valid value.

    Test cases i'd like to but i'm a bit occupied, ill do it over the weekend.

    There is one more solid bug in editor.
    So to get the default value i hacked my code a bit , used select and inserted what i want as default as first element of the array.that i think solved it but wait.. here is the catch

    formOptions : {
    main:{
    submit:'changed',
    }
    },

    Now when a default value is applied, it doesn't get sent to server cause form thinks its value has not been changed.
    But the whole point of having default value is to save it, where as its not happening .
    I had to remove submit :' changed' option.

  • allanallan Posts: 63,350Questions: 1Answers: 10,443 Site admin

    @ Assuming "" to be a valid value is a bit tricky.

    Consider for example a "Description" field or "Extra notes" - an empty string is absolutely valid. What I think you are suggesting is that something like a telephone number might be stored as null rather than an empty string if there is no input. If so, that's why Editor's server-side libraries provide IfEmpty formatters. Keep in mind also that you don't really want the end user to have to tick a "null" checkbox on the client-side - better to have an empty string stored either as null or an empty string as required by the schema.

    Could you give me a link showing the issue with the submit: 'changed' please?

    In general I would not expect the default to be used for a row being edited. It would only ever be useful if a new column / field has been added since the original data was added (and even then, it would need to have a default value in the database to have been added).

    Allan

This discussion has been closed.