Select Field Type - how to set default value?
Select Field Type - how to set default value?
How do I set the type -select
to "Monthly"? Currently, it defaults to the first item.
This is my options list:
CMP_FREQ = {"Continuous": 0, "Daily": 1, "Weekly": 2, "Biweekly": 3, "Twice Monthly": 4, "Every 4 Weeks": 5, "Monthly": 6, "Bimonthy": 7, "Quarterly": 8, "Every 4 Months": 9, "Semiannually": 10, "Annually": 11, "Exact": 12, "None": 13},
My field initialization is this:
var editor = new $.fn.dataTable.Editor({
ajax: function (method, url, data, success, error) {
if (data.action === 'edit') {
if (typeof rowDataArray[CMP_FREQ_SEL] !== 'number') {
rowDataArray[CMP_FREQ_SEL] = parseInt(rowDataArray[CMP_FREQ_SEL], 10);
}
success({row: rowDataArray});
}
},
legacyAjax: true, // if true, versions prior to v1.5.0
idSrc: IDX_COL,
formOptions: {
inline: {
onBlur: 'submit'
}
},
fields: [
{
name: CMP_FREQ_SEL,
type: "select",
options: CMP_FREQ,
// def: "Monthly",
def: 6,
id: "cmp_freq_sel"
},
init -placeholder
does not seem to be what I need in that the default is an item in the select list.
Thanks.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Should be all that you need there. Perhaps try
def: '6'
?If that doesn't work, can you give me a link to the page so I can check it out please?
Allan
Thanks, Allan. I believe I understand what my problem is. The table is initialized with "empty" rows, that is, each cell is set to null. Editor is used to add values. Thus
def: '6'
does not work because the cell's value is not undefined when the initial Editor open event fires.Can you give me a pointer how to hook the default select element and set its value when the cell contains a null? I assume I would do this in the Editor's
open
?Currently the page is only on localhost.
Yes, for that approach you would need to explicitly set the values. Perhaps something like:
Allan
Thanks, Allan.
Putting the code in the 'preOpen' event handler worked for me.
For anyone else that has this use case:
Hey, thanks for this question & answer; it helped me today.
(I added a call to
editor.val()
ininitEdit
to set the selection to the most-recently-selected value; to get that, I'm stuffing it into a var inpreSubmit
.)This site's search engine + the quality of the forums are a lot of what makes DataTables so great to use; every time I want to do something, there's an example I can copy from.