How to set the options of "select" type field in editor from database?
How to set the options of "select" type field in editor from database?
When you click on "new" button, there is a "priority" field having 5 options and those options are defined in the editor.
All I need is, instead of this approach, the options should be retrieved from database that I have a query: select level from priority the its returnset should be the options of priority field when you want to add a new row. How can i do it?
I tried:
in editor configuration:
{
label: "Priority:",
name: "Priority",
type: "select",
options: <%= getPriorities() %>
},
and in back-end:
public string getPriorities()
{
JsonToReturn j = new JsonToReturn();
List<Priority> l = new List<Priority>();
...
for(var item in ReturnListFromSQL) {
Priority p = new Priority ();
p.Label = level;
p.Value = level;
l.Add(p);
}
j.returnObject = l;
return JsonConvert.SerializeObject(l);
}
However there is no option with this approach. What's the best way to manage this one?
Note: I use it on asp.net / c# project.
-Eray
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Answers
Sorry for the poor formatting of the question, can't see how to edit..
The
Options
class is probably what you want. However, can the list of options change between edit and create actions? Or between rows?Allan
Hi Allan,
No the list of options should not change between edit/create actions or rows.
How should i use Options class in my design tho, any brief example you have maybe?
All I need is to show the result list of "select level from priority" query as the options for instance. No need to use any join.
Thanks?
Eray
Are you using the
Editor
class to populate the DataTable? If so, you would add the Options class to theField
instance in question.If you aren't, then you can send back an
options
object as part of the JSON response from the server for the DataTable. See the documentation for that here.Allan
Hi Allan,
I have datatables-editor-server added as a reference to my project. I use editor in front-end only so far, inside <script> blocks. Originally, I add the option class like below for Priority field in my editor:
What should be the return type of getProrities() function here in c#, i think it's where I am stuck.
And in the backend lets say I have:
It doesn't work saying: "Uncaught SyntaxError: Unexpected end of input"
I was expecting to see Test1 and Test2 as the options.
I feel I'm close based on your replies so far but can't finalize it. Any further help would be appreciated.
Thanks.
Edit: I also tried:
this does not give any error but there is no options shown in the select list, all blank.
Tried this:
Returns empty string.
That worked!!
Thanks for your help Allan.
Eray
Awesome - nice one! Thanks for posting back with your solution.
Allan