To clear List Collector values on change of a variable in SR.

zabeeulla2
Kilo Expert

Hi All

                  I have a requirement to clear all the options selected in the right slush busket from a list collector when a variable   say ' Customer' is changed instead of moving them to the left side because there is already an onchange script for the filter for every change of customer a set of new values get populated in the list collector. So when I change the customer the selected values in the list collector should disappear and show none .

                                                Immediate help is appreciated.

find_real_file.png

this is for one customer when I change the customer it should be as shown below

find_real_file.png

Thanks

Zabee.

1 ACCEPTED SOLUTION

Try this:


OnChange of customer:



var varName = 'Name of your list collector';


var rightBucket = gel(varName + '_select_1');


var selectedOptions = rightBucket.options;



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


             


    //Check for the item to add here based on sys_id


    var value = selectedOptions[i].value;


        rightBucket.remove(value);



    }


View solution in original post

24 REPLIES 24

Hi Robert,



I could do that but right list collector is not setting with none. Please check the below screenshot, the variable is mandatory but after removing the values also it is not showing as mandatory.


find_real_file.png



Thanks,


Sowmya


drbob
Tera Contributor

Sowmya,



I'm not sure if you have changed any code since your last post but the two snippets each have half the answer (at least given what works for me) - can you confirm that now when you have added 5 things all 5 are being removed ?



One uses an decreasing "for" counter (nice way to get around the changing length) but uses "rightBucket.remove(selectedOptions[i]);" to make the removal. The other uses the increasing counter (so suffers from early termination) and uses   "rightBucket.remove(selectedOptions[i].value);" (if you push the two lines together).



Suggest you use the .value version of the remove in a loop that works properly (either decreasing or increasing to a static variable, not the option list length as that changes on each iteration).



Rob.


ll.png

Hey Mujtaba,

          if I remove any of the type in the list collector show a popup message and include removed value name like wm_work is removed from the list collector

how can i configure this in client script.

 

drbob
Tera Contributor

I just had this exact situation and thank you for the code but I had to adjust a little more. If I had selected one item then it was successfully removed but in my next test I selected 3 and only 2 were removed. I put some alerts in and watched the selectedOptions.length report as 3 and then the loop only execute twice.



I believe the issue is that as the rightBucket.remove is executed the next loop test is against the new length not the length when you set off. So, edit #1 is to save the length in an new variable and use that for the loop condition. Having done that I got 3 passes but still an option left - it stands to reason that if the list is updating (the reason the loop was being cut short) then by the third time through selectedOptions[2] no longer exists. So I selected to remove selectedOptions[0] on every pass and this seems to do the trick...



var loop_end = selectedOptions.length;


  for(var i = 0; i < loop_end; i++){


  var value = selectedOptions[0].value;


  rightBucket.remove(value);


  }



Disclaimer: I'm a code recycler with a good sense of logic I have no in-depth knowledge of javascript - the explanation above is based on a good guess at what is going on having found code that works.


You are correct. The length is dynamically changing as we remove values.  I solve this by starting at the end of the list and working towards the start.

 

  • Get the length and put it into a variable. 
  • We have to set i to be the length minus 1 since we are working backwards; while there are 5 elements in the list, they are indexed 0 - 4, so we can't start at 5. 
  • We have to account for option indexed at 0, thus we do i > -1.

 

var rightBucket = gel(collectorName + '_select_1');
var selectedOptions = rightBucket.options;
rightLength = selectedOptions.length;

for(var i = rightLength-1; i > -1; i--){
     var value = selectedOptions[i].value;
     rightBucket.remove(value);
}