09 April 2009

0 JavaScript code to get the final count of the selected items across pages in pagination and validate in JavaScript before form submission

When you are dealing with the pagination, here you go for the JavaScript code to get the final count of the selected items across pages in pagination and validate in JavaScript before form submission


var finalCount=session.length; //count of items already in session

                             

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

var currentPageItem = pageList[i];

//checked length is 0 and session length is 0

if (checked.length==0 && session.length > 0){

      // No items checked; previously checked items are removed

            for (var l=0; l <session.length; l++){

                  if(session[l] == currentPageItem){

                        finalCount --;

                  }

            } // end for

      }

//checked length greater than 0 and session length is 0

else if(checked.length > 0 && session.length == 0){

            // No items in session

            for(var k = 0; k < checked.length; k++){    

                  if(currentPageItem == checked[k]) {

                        // checked items in current page   

                        finalCount ++;

                  }    

            } // end for     

      }

     //checked length greater than 0 and session length greater than 0

else {          

            if(isPresent(currentPageItem, checked)){ 

                  // Item is checked     

                  if(!isPresent(currentPageItem, session)){

                        // Item is not in session; add item

                        finalCount ++;                           

                  }    

            }

else {     

                  //Item is not checked  

                  if(isPresent(currentPageItem, session)){ 

                        // Item in session; remove item    

                        finalCount --;   

                  }

            }

      } // end main if 

} // end for loop

 

alert (finalCount);


Before using the script get the three arrays used in the script.

Arrays used in the JavaScript

session - array of selected items already in session (items checked in the previous pages). This can be passed as a string separated by a comma to the script and then put into the array.

checked - array of checked items in the current page.

pageList - array of all items in the current page both checked and unchecked.

The finalCount gives the total count of selected items across pages.

The function isPresent is called to check if an item is present in the array. The function will return true if the item is present in the array else it will return false.

//function isPresent(key, Array)

//returns true if key is present in the Array

 

function isPresent(item, itemList){

      var flag = new Boolean();

      flag = false;

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

            if(itemList[i]== item){

                  flag = true;

            }

      }

      return flag;

}





0 comments:

Feeds Comments

Please give your valuable comments.