New Batch#100 (10th Nov 2021) - Salesforce Admin + Dev Training (WhatsApp: +91 - 8087988044) :https://t.co/p4F3oeQagK

Thursday 11 February 2016

Moving pick-list values up and down (reordering) in javaScript (Works only chrome, not working for Firefox and IE)

<!-- To switch the picklist values -->
    function arraymove(direction) {
        var optionslist = document.getElementById('select-2').options;
        var selLst = [];
        var unselLst = [];
        var combLstVals = [];
        var combLstLabels = [];
        var combLstSel = [];
        var selStartIndex;
        var count = 0;
        for(i=0;i<optionslist.length;i++) {
            if(optionslist[i].selected) {
                if(count == 0)
                    selStartIndex = i;
                count++;
                selLst.push(optionslist[i]);
            }
            else {
                unselLst.push(optionslist[i]);
            }
        }
        var ovelAllLength = selStartIndex + selLst.length;
            var isApply = false;
            //Down Arrow Logic
            if(direction == 'down') {
                if(selStartIndex != null && (ovelAllLength < optionslist.length || selStartIndex == 0)) {
                    isApply = true;
                    if(selStartIndex != 0) {
                        for(i=0;i<unselLst.length;i++) {
                            if(i == selStartIndex) {
                                combLstVals.push(unselLst[i].value);
                                combLstLabels.push(unselLst[i].label);
                                combLstSel.push(false);
                                for(j=0;j<selLst.length;j++) {
                                    combLstVals.push(selLst[j].value);
                                    combLstLabels.push(selLst[j].label);
                                    combLstSel.push(true);        
                                }
                            }
                            else {
                                combLstVals.push(unselLst[i].value);
                                combLstLabels.push(unselLst[i].label);
                                combLstSel.push(false);
                            }
                        }
                    }
                    else {
                        combLstVals.push(unselLst[0].value);
                        combLstLabels.push(unselLst[0].label);
                        combLstSel.push(false);
                        for(j=0;j<selLst.length;j++) {
                            combLstVals.push(selLst[j].value);
                            combLstLabels.push(selLst[j].label);
                            combLstSel.push(true);        
                        }
                        for(i=1;i<unselLst.length;i++) {
                            combLstVals.push(unselLst[i].value);
                            combLstLabels.push(unselLst[i].label);
                            combLstSel.push(false);
                        }
                    }
                }                          
            }
            //Up Arrow Logic
            else if(direction == 'up') {
                if(selStartIndex != 0 && selStartIndex != null) {
                    isApply = true;
                    if(selStartIndex == 1) {
                        for(j=0;j<selLst.length;j++) {
                            combLstVals.push(selLst[j].value);
                            combLstLabels.push(selLst[j].label);
                            combLstSel.push(true);        
                        }
                        for(i=0;i<unselLst.length;i++) {
                            combLstVals.push(unselLst[i].value);
                            combLstLabels.push(unselLst[i].label);
                            combLstSel.push(false);
                        }
                    }
                    else {
                         for(i=0;i<unselLst.length;i++) {
                            if(i == selStartIndex - 1) {
                                for(j=0;j<selLst.length;j++) {
                                    combLstVals.push(selLst[j].value);
                                    combLstLabels.push(selLst[j].label);
                                    combLstSel.push(true);        
                                }
                                combLstVals.push(unselLst[i].value);
                                combLstLabels.push(unselLst[i].label);
                                combLstSel.push(false);                            
                            }
                            else {
                                combLstVals.push(unselLst[i].value);
                                combLstLabels.push(unselLst[i].label);
                                combLstSel.push(false);
                            }
                        }
                    }
                }
            }      
            if(isApply) {
                for(i=0;i<optionslist.length;i++) {
                    optionslist[i].value = combLstVals[i];
                    optionslist[i].label = combLstLabels[i];
                    optionslist[i].selected = combLstSel[i];
                }
            }
    }

1 comment:

  1. Thanks for sharing
    "Sanjary Academy provides excellent training for Piping design course. Best Piping Design Training Institute in Hyderabad,
    Telangana. We have offer professional Engineering Course like Piping Design Course,QA / QC Course,document Controller
    course,pressure Vessel Design Course, Welding Inspector Course, Quality Management Course, #Safety officer course."
    Piping Design Course in India­
    Piping Design Course in Hyderabad
    Piping Design Course in Hyderabad
    QA / QC Course
    QA / QC Course in india
    QA / QC Course in Hyderabad
    Document Controller course
    Pressure Vessel Design Course
    Welding Inspector Course
    Quality Management Course
    Quality Management Course in india
    Safety officer course

    ReplyDelete

Labels