Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Priority Scenario

0.00/5 (No votes)
9 Mar 2012CPOL 5.7K  
Priority Scenario

Introduction

This article is about Priority Scenario implementation in JavaScript.

Background

ASP.NET knowledge.

Using the code

You have to simply add this script block into the header section of the page.

Do not make any modifications to the code. It works as expected.

You can still try to put your knowledge to test.. But believe me it will work in the fashion I have designed.

JavaScript
<script language="javascript" type="text/javascript">
    <!--
    var selects = null, assignments = null, currentDDL = null, currentValue = null, priorityDDls = null;

    function PriorityDDList_onchange() {
        ddl = window.event.srcElement;
        removeEvents();
        newValue = parseToNumber(ddl.value);
        if (newValue > 0 && currentValue > 0) {
            synchAssignments();
            assignments[1][currentValue - 1] = newValue;
            if (newValue > currentValue) {
                for (var i = newValue; i > currentValue; i--) {
                    assignments[1][i - 1] = i - 1;
                }
            }
            else if (newValue < currentValue) {
                for (var i = newValue; i < currentValue; i++) {
                    assignments[1][i - 1] = i + 1;
                }
            }
            applyAssignments();
        }
        else if (currentValue == 0) {
            var newValueDDL = null;
            for (var i = 0; !newValueDDL && i < priorityDDls.length; i++)
                newValueDDL = priorityDDls[i] != currentDDL && 
                   parseToNumber(priorityDDls[i].value) == newValue ? priorityDDls[i] : null;
            if (newValueDDL)
                newValueDDL.value = currentValue;
        }
        attacEvents();
    }

    function PriorityDDList_onclick() {
        currentValue = parseToNumber((currentDDL = window.event.srcElement).value);
    }

    function parseToNumber(str) {
        return isNaN(str) ? 0 : parseInt(str);
    }

    function init() {
        attacEvents();
        initAssignments();
    }

    function initAssignments() {
        assignments = new Array();
        assignments.push(new Array());
        assignments.push(new Array());
        for (var i = 1; i <= priorityDDls.length; i++) {
            assignments[0][i - 1] = i;
            assignments[1][i - 1] = i;
        }
    }

    function synchAssignments() {
        for (var i = 1; i <= priorityDDls.length; i++) {
            assignments[1][i - 1] = (assignments[0][i - 1] = i);
        }
    }

    function applyAssignments() {
        for (var i = 0; i < priorityDDls.length; i++) {
            priorityDDls[i].value = priorityDDls[i] != currentDDL && 
               (index = parseToNumber(priorityDDls[i].value)) ? 
                assignments[1][index - 1].toString() : priorityDDls[i].value;
        }
    }

    function attacEvents() {
        selects = document.getElementsByTagName("select");
        priorityDDls = new Array();
        for (var i = 0; i < selects.length; i++) {
            if (selects[i].id.indexOf("ddlAP") > -1) {
                selects[i].onchange = PriorityDDList_onchange;
                selects[i].onclick = PriorityDDList_onclick;
                priorityDDls.push(selects[i]);
            }
        }
    }

    function removeEvents() {
        for (var i = 0; i < selects.length; i++) {
            if (selects[i].id.indexOf("ddlAP") > -1) {
                selects[i].onchange = null;
                selects[i].onclick = null;
            }
        }
        selects = null;
    }
-->
</script>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)