Getting toolbar list options in same order as they are displayed

177 views Asked by At

I am building options for a drop down menu on a toolbar from a database table, giving the id's for the options the same value as the index in the table, however ordered alphabetically by the description of the option. The visual drop down comes out sorted nicely, however when I ask for a list of all options, they are sorted by id, i.e. getAllListOptions() and forEachListOption(). Are there ways to lookup the option list in the "right" order to pick say the first option in the list, other than sorting the list again myself?

To emphasize, the problem is ID's are not in ascending order when options are sorted by description, however dhtmlx order them internally in ascending order by value instead of by name as they should. I.E, say option with ID of 8 comes out top of list, the first option in the internal list should be "8" not what ever is the smallest ID (in my case 2).

Website is MVC and back-end is MS SQL Server, data structures, like option lists, are passed back through razor scripts as XML.

I.E. the ajax call to get the option list is passed through the bussiness layer to the DAL, which request a list of objects sorted on description. The razor script then creates the XML like this

<item id="tbbOptions" type="buttonSelect" title="@(Messages.Lookup("Config"))" selected="-1" text="@Messages.Lookup("Config_Select")">
    @foreach (Analysis oAnalysis in Model.Analysis)
    {
        if (!string.IsNullOrEmpty(oAnalysis.SecondaryField))
        {
            <item type="button" id="@(oAnalysis.ID)" img="blueprint.png" imgdis="blueprint.png" text="@string.Format("{0}", oAnalysis.Description)" />
        }
        else
        {
            <item type="button" id="@(oAnalysis.ID)" img="blueprint.png" imgdis="blueprint.png" text="@string.Format("{0}", oAnalysis.Description)" />
        }
    }
</item>

SQL query:

    public List<Analysis> GetConfig()
    {
        return UnitOfWork.Database.Fetch<Analysis>("SELECT * FROM [Analysis] ORDER BY [Description]");
    }
3

There are 3 answers

0
aggaton On BEST ANSWER

After scouring through the javascript source code for the dhtmlx toolbar, I constructed a solution that seem to do what I want. Apparently there are no functions that expose the visual options, so only way to access is through attributes on the object.

In case others run into the same issue, here is what I did:

//Get all visible options
var asList = this.objPull[this.idPrefix + "tbb" + sChart].p_tbody.childNodes;
//If options exist for Chart set drop-down to first option in list
if (asList.length > 0) {
    this.setListOptionSelected("tbb" + sChart, asList[0].idd);
    this.setItemText("tbb" + sChart,"<strong>{0}</strong>".format(this.GetSelectedItemText("tbb" + sChart)));
}
1
Konstantin A. Magg On

Let the database sort the list, e.g. find().sort() for mongoDB:

https://docs.mongodb.com/getting-started/shell/query/#sort-query-results

Which DB are you using?

7
blckt On

LINQ OrderBy or SortBy will solve your problem;)

Use this on your view model in action method or in your View (not recommended)

 Model.Analysis.OrderBy(x=>x.ID)