Setting defaults at runtime

Setting defaults at runtime

IvanwIvanw Posts: 7Questions: 0Answers: 0
edited July 2012 in Editor
Hi Allan

I'm having trouble setting a default value for a field I'm creating at runtime in Editor.

If I create the field when the editor instance is originally created there is no problem as below:

"fields": [
{
"label": "Id",
"name": "id",
"type": "hidden"
},
{
"label": "Code",
"name": "code",
"type": "text",
"default":"hello!"
}
]

If I create the field using one of the "events" methods the field is created and perfectly usable it just will not register a default, as below code example.

$(document).ready(function() {
var CodeEditor = new $.fn.dataTable.Editor( {
"ajaxUrl": "include/table.Codes.php",
"domTable": "#Codes",
"events": {

"onPreSubmit":function(){
CodeEditor.add(
{
"label": "eType",
"name": "eType",
"type": "text",
"default": "hello!"
}
)

}
},
Am I missing something obvious here or is what I'm trying to do not feasible?

Thanks

Ivan

Replies

  • allanallan Posts: 61,439Questions: 1Answers: 10,053 Site admin
    Hi Ivan,

    In theory yes, you can add new fields to an Editor instance at almost any time using the add API method as you have done - however, as you have seen it actually won't work fully in onPreSubmit (and a good number of the other callbacks) because the default values for the fields are set when the Editor edit display is shwon (i.e. whent he lightbox is brought up in the browser window). This has occured before you add the field, thus you get the problem you are seeing.

    Are you simply looking to add an extra field to submit to the server dynamically? If so, you can just use something like:

    [code]
    onPreSubmit: function (submitData) {
    submitData.data.myParameter = myValue;
    }
    [/code]

    Regards,
    Allan
  • IvanwIvanw Posts: 7Questions: 0Answers: 0
    Allan. Thanks for that advice it achieved what I was trying to do which was set a value for one field depending on the value of another.

    Cheers
  • allanallan Posts: 61,439Questions: 1Answers: 10,053 Site admin
    Great to hear! If you have any other feedback regarding Editor, do just let me know :-)

    Allan
This discussion has been closed.