No no i get that, I know how to do this, i manged it . But i want to be displayed that it is selected. Everytime i open node the fetch function calls and dropdown refills . So basically everytime i do that the 1st one is selected. Now i want to make if i selected one option that it stores its data and then when i reopen the node it can be selected.
But your question suggests to us you aren't quite "getting it". Because to me, your question sounds identical to the one you've asked previous and that we've explained before how to use the node property to ensure the right option is selected in the list after it gets repopulated.
Now i want to make if i selected one option that it stores its data
Yes - the selected value is stored as a node property
then when i reopen the node it can be selected.
When the dialog reopens, your fetch runs to get the available options, then once the select box has been populated with the available options, it can use the value from the stored node property to ensure the right value has been preselected in the list.
I understand every part of what u said and i know how to implement most of it. Except for the code for the last thing you wrote. dont know how to make the right value selected in the list.
data.forEach((item) =>
let value = item.name;
let text = item.name;
options.push({ v: value, t: text, });
$("#node-input-options").append(
$("<option></option>").attr("value", value).text(text)
);
if(value == node.selectedValue) $('#node-input-options').attr('selected','selected'); //<<<<<
);
What it does: as you build your select, if the remembered selection (node.selectedValue) matches the value being added to the options, set the selected attribute of the option being added to value selected.
for (let i = 0; i < options.length; i++) {
let value = options[i].v;
let text = options[i].t;
var opt = $("#node-input-options").append(
$("<option></option>")
.attr({
value: value,
})
.text(text)
);
if (text == node.selectOption)
opt.attr("selected", "selected");
}
for (let i = 0; i < options.length; i++) {
let value = options[i].v;
let text = options[i].t;
$("#node-input-options").append(
$("<option></option>")
.attr({
value: value,
selected: (text == node.selectOption ? "selected" : "")
})
.text(text)
);
}
Your last post almost does it. Now it displays the selected but the problem is that no matter which i select now the text is always 3rd one and the value is selected. If u dont understand what i mean ill do a quick video about it.
Maybe the problem is that its in a loop?
Its like my oneditsave its not dynamic anymore.
Tried console loging the node.selectOption in oneditsave and oneditprepare and its good. Just the dropdown always has 3rd one selected
for (let i = 0; i < options.length; i++) {
let value = options[i].v;
let text = options[i].t;
let opt = $("<option></option>");
opt.attr({ value: value});
opt.text(text);
if (text == node.selectOption) opt.attr("selected", "selected");
$("#node-input-options").append(opt);
}
OR
for (let i = 0; i < options.length; i++) {
let value = options[i].v;
let text = options[i].t;
let opt = $("<option></option>");
opt.attr({ value: value});
opt.text(text);
$("#node-input-options").append(opt);
}
$("#node-input-options").val(node.selectOption);