Sorting add options not working

sigmachiuta
Kilo Guru

I am trying to sort my addOptions but i dont have success doing this what i have is a a glide ajax call here is my script include sort is an integer.   This should be passing the sort as the last parameter

      while(gr.next()){

          var a = gr.u_operatingsystem.split(',');

          var result =   this.newItem('os1');

          for(var i = 0 ; i < a.length; i++){

              var na = new GlideRecord('u_operatingsystems');

              na.addQuery('sys_id',a[i]);

              na.addQuery('u_active', 'true');

              na.query();

              while(na.next()){

             

          var b = this.newItem("os");

          b.setAttribute("os", na.u_osdisplayname);

    var c = this.newItem("sort");

    c.setAttribute("sort", na.u_sort);

                 

              }

and here is a snip from my client script the options are showing correct the sort order is just not correct.

  function OsProcessor(response) {

      var sec = response.responseXML.getElementsByTagName("os");

  var srt = response.responseXML.getElementsByTagName("sort");

      for ( var i = 0 ; i < sec.length; i++){

          var n1 = sec[i].getAttribute("os");

          var sort= sec[i].getAttribute("sort");

          g_form.addOption('os', n1, n1, sort);

         

      }

5 REPLIES 5

Erik Stolberg
Tera Guru

To make sure I'm understanding this fully, your u_operatingsystems table has a column for u_sort already defined. You aren't trying to alphabetically sort these when you query or anything, correct?



The g_form.addOptions 4th parameter is an index, but it's on a zero-based array, and inserting will always push later values to a higher index.


If [X,Y,Z] already exist and you add W with index 2, it will result in [X,Y,W,Z] meaning Z might have been passed in with index 2 previous but now has been bumped to 3.



Have you tried logging your index values to make sure they are passing correctly? I would try to do a sort on your GlideRecord using that u_sort column to get all of your records in the order you want, then just loop through your GlideAjax to add the values, completely neglecting the index parameter.


correct i have a u_operatingsystems table with a sort number defined.   My idea was since the sort is an integer 1, 2, 3 etc to pass that value into the index the 4th parameter and it would populate in the sort order that I defined in the record u_operatingsystems table .


Looks like i had an error in my code it should have been....   The issue I am seeing now is that even though I am alerting the correct os and sort value the addOption is displaying the options in the order they are returned not in the order that should be set by the parameter.



  b.setAttribute("os", na.u_osdisplayname);


  b.setAttribute("sort", na.u_sort);


I looked into the array functionality, and it looks like if you pass in an index, it will put the value in that spot no matter what, leaving any gaps as an undefined value.



That being said, I still recommend just adding "na.orderBy(u_sort)" to your server query and ignoring the Index parameter. You did say that results were being returned, just not in the right order. That will put the options in the right order while insuring you don't have any gaps.