preselect an item in a drop down list of Editor popup
preselect an item in a drop down list of Editor popup
I read many threads about select in editor, so I found the update function for reading the options from database and update the select in the editor. I use editor version 1.5.2.
my editor fields:
// is called with loading the page
function getCompanynames () {
$.ajax({
url: 'contact/companies/dt_names',
success: function (response,statusText) {
var aDropdownList = new Array ();
var l = response.length;
for (var i=0;i < l;i++) {
aDropdownList[i] = {
"label": response[i][0],
"value": response[i][1]
};
}
editor.field('company').update(aDropdownList);
}
error: ...
});
}
editor = new $.fn.dataTable.Editor( {
...
fields: [
{ "label": "Id", "name": "id", "type": "readonly" },
{ "label": "Number", "name": "number"},
{ "label": "Company", "name": "company", "type": "select",
// options set with update
},
{ "label": "Companyname",
"name": "companyname" ,
"type": "hidden"
}
],
...
})
But now I need to know how can I preselect a label or value from the select list, to show the company name from the selected table row in the select list. The selected item has to be set dynamically for each row which should be edited. I want to try it when the edit popup opens (editor.on('open', function ...)).
I can't find that information in the duscussions.
This question has an accepted answers - jump to answer
Answers
Sounds like you want to set a default value -
fields.def
andfield().def()
can be used to set the default.Allan
maybe, but I don't get it working, with
I tried preOpen and open, and the label and the value from the select (4 is the label, 5 would be the value), in my test field #info, it shows the correct value. But in the edit popup in the select I only see the empty option and if I open the select I can see the whole list of options. So the list os there, but nothing is selected.
Mißunderstood I how to use it?
Can anybody tell me, why that isn't working, if def is the function to select an option in a dropdown?
The
field().def()
function will set the default value. So when the Editor form is shown, if there is no value (i.e. in the case of a "Create" action) then the default value will be selected.If you want to set a value, not a default, then use
field().val()
.If you do actually want a default, the default would have to be set before
preOpen
. It should be set as soon as you know what the default will be (i.e. before the user interacts with the form).Allan
Thanks Allan, val() does what I need.