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?

NwhreitDataTableNwhreitDataTable Posts: 8Questions: 2Answers: 0
edited March 2023 in Free community support

Please see this example.

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

  • NwhreitDataTableNwhreitDataTable Posts: 8Questions: 2Answers: 0

    Sorry for the poor formatting of the question, can't see how to edit..

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    The Options class is probably what you want. However, can the list of options change between edit and create actions? Or between rows?

    Allan

  • NwhreitDataTableNwhreitDataTable Posts: 8Questions: 2Answers: 0
    edited March 2023

    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

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    Are you using the Editor class to populate the DataTable? If so, you would add the Options class to the Field 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

  • NwhreitDataTableNwhreitDataTable Posts: 8Questions: 2Answers: 0
    edited March 2023

    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:

    ...
    fields: [
    ...
        {
            label: "Priority:",
            name: "Priority",
            type: "select",
            options: <%= getPriorities() %>
        },
    ...
    ],
    ...
    

    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:

    public List<DataTables.Options> getPriorities()
    {
            List<DataTables.Options> l = new List<DataTables.Options>();
    
            DataTables.Options d = new DataTables.Options();
            d.Label("Test1");
            d.Value("Test1");
            l.Add(d);
    
            DataTables.Options d2 = new DataTables.Options();
            d.Label("Test2");
            d.Value("Test2");
            l.Add(d2);
    
            return l;  
    }
    

    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:

    public String getPriorities()
        {
            List<DataTables.Options> l = new List<DataTables.Options>();
    
            DataTables.Options d = new DataTables.Options();
            d.Label("Test1");
            d.Value("Test1");
            l.Add(d);
    
            DataTables.Options d2 = new DataTables.Options();
            d.Label("Test2");
            d.Value("Test2");
            l.Add(d2);
    
            var x = JsonConvert.SerializeObject(l);
            return x;
        }
    

    this does not give any error but there is no options shown in the select list, all blank.

  • NwhreitDataTableNwhreitDataTable Posts: 8Questions: 2Answers: 0

    Tried this:

     public String getPriorities()
        {
            DataTables.Options d = new DataTables.Options();
            d.Add("Eray1", "Eray1");
    
            return JsonConvert.SerializeObject(d);
        }
    

    Returns empty string.

  • NwhreitDataTableNwhreitDataTable Posts: 8Questions: 2Answers: 0

    That worked!!

    public String getITResources()
        {
            Dictionary<string, object> d = new Dictionary<string, object> { { "Test1", "Test1" }, { "Test2", "Test2" } };
    
            return JsonConvert.SerializeObject(d);
    
        }
    

    Thanks for your help Allan.

    Eray

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    Awesome - nice one! Thanks for posting back with your solution.

    Allan

Sign In or Register to comment.