Friday, 31 July 2015

Cascading number of drop downs with single Ajax request


Hello All,

Today we will learn about a new way of drop-down cascading. We all are aware of fetching a drop-down data on selection of another drop-down i.e On Selection of "Country" in the drop down we want to fetch all the states associated with it and fetch the "Sates" drop-down we have to make a separate ajax request for a drop-down so if there are three drop-downs then we have to make the ajax request two times. What if we fetch all the associated data in a single request and filter the data on client side. Isn't it amazing to do that.. Jquery gives us a function "$.grep" but it is not so popular because it is not commonly used.. we can use this amazing method to achieve our target.. Lets see how this works.. The code is in simple html and Jquery but some C# Asp.Net MVC code terms can be there.. so just convert those in your server side language according to you..




Here in the image you can see there are two drop-downs one is for "Cancellation Type" and another shows the associated "Cancellation Reason".. now we want to fetch the assoicated cancellation reasons for selected type.. we have two ways..

1. Either we can fire an ajax request for every change event of "Cancellation Type" on change of drop-down we will take the selected id to server side and fetch the associated reason and in response of ajax request we will return all the associated option and bind the drop-down of "Cancellation Reason" but in this approach we have to make an ajax request at every change event so to avoid this problem we will use second approach..

2. We can fetch all the options of "Cancellation Reason" on the page load and then on the change event of "Cancellation Type" we can filter the reason by using "$.grep ".. lets see how this works..

Html Code for Drop-downs :

 <select name="Cancellation" id="Cancellation"><option value="1"> Patient Cancellation</option>  
 <option value="2">Provider cancellation</option>  
 <option value="3">Cancellation (admin)</option>  
 <option value="4">DNA</option>  
 </select> 

<select name="Reason" id="Reason"></select>   

JavaScript used in Cascading :


 <script type="text/javascript">  
   $(document).ready(function () {  
     var issueList = new Array();  
     var selectedissueList = new Array();  
     $.post('@Url.Action("GetCancelDetails","Appointment")',null,function(data){  
       $.each(data, function (index,value) {  
         var object = {  
           Id: value.Id,  
           Reason: value.Reason,  
           TypeId:value.TypeId  
         };  
         issueList.push(object);  
       });  
       BindReasons();  
     });  
     $('#Cancellation').change(function () {  
       BindReasons();  
     });  
     function BindReasons()  
     {  
       var value = $('#Cancellation').val();  
       var reason = $('#Reason');  
       selectedissueList = $.grep(issueList, function (v, index) {  
         if (v.TypeId == value)  
           return v;  
       });  
       reason.html('');  
       $.each(selectedissueList, function (index, issue) {  
         reason.append('<option value="' + issue.Id + '">' + issue.Reason + '</option>');  
       });  
     }  
   });  
 </script>  


Explanation:

As you can see in the Html code.. the first Drop-Down "Cancellation Type" is already filled.. Now on page load i.e. document.ready, I have declared two arrays(issueList, selectedissueList) and firing an Ajax request using "$.post", you can also use "$.ajax". First argument in $.post function is the URL on which it will make a request.. this syntax is Asp.Net MVC specific, this can be different in other languages but the main point is we are making an Ajax request to fetch all the "Cancellation Reason with their CancellationTypeId" after getting response from server we are looping each object of response and pushing it in our pre declared array. After loop we are calling a function called "BindReason()".. now lets learn what is being done in this function :

BindReason() : In this function we are binding the "Cancellation Reason drop-down", this function look at the selected value of "CancellationType". In the next line it taken "Cancellation Reason" dropdown in a variable to populate it. Next line is the most important line.. it is using "$.grep" function on the array which was populated by us on page load and in that array it is matching the "CancellationType" value from each object and if an object is matching, it is pushing the matched object into another array.And finally it populate the CancellationReason drop down by the new populated array.

We use this function on document.ready as well as on CancellationType drop-down's change event.

So you can see that we are firing a single Ajax request on page load and after that  we just a filter the stored array by using jquery so we can perform the cascading on client side.

Hope you like this approach... Happy Coding!!!

If you have any questions please ask in the comments..

Wednesday, 15 July 2015

PayPal Integration(DoDirect Payment)


This is my first blog so start with a common assignment that every web developer has to go through. This is about paypal, there are many methods to integrate payapal on you site but in simple language it can be devided into two ways :

1. Redirect your user to paypal's site and rely on paypal to send you response(Very simple and common)

2. Sometimes client doesn't want their user to leave their site for payment so for that we have to integrate paypal api that allows user to use their credit cards on your own site, no need to redirect them to paypal's site, this method of payment through paypal is called Do Direct payment this blog discuss about Do Direct method, no code involved this is a simple step by step guide to integrate paypal to your site, programming language and technology can be any.



Do Direct Payment : Used for accepting credit cards etc at the website payment checkout process,  we do  not need to redirect user on PayPal page by using this method.
Steps Involved in Implementation and Testing :
Step 1.  Download an SDK from https://developer.paypal.com/  here you will get a sample implementation and all the required reference dlls.

Step 2. You need to create an account on  https://developer.paypal.com/ ,   after logging in your account go to Dashboard, there you will see a menu titled as 
“Sandbox” ,  in the submenu of this go to “Accounts” and click on the
Create Account button.

Step 3. Follow the process of http://sandbox.paypal.com/  and create an account there you can provide test details in every required field and enter an amount which you want to deposite in your test account.

Step 4. Now come back to developer site (https://developer.paypal.com/) and click on your fresh created linked Sandbox account, this will open a submenu, now click on “Profile” link and update your account to Sandbox pro.

Step 5. After successful updating go to sandbox account, click on Profile then navigate to API access and view your API signature.

That’s all !! From there you can get your Api username, password and signature , copy-paste these in the configuration settings of sdk and you can successfully test the transaction by using test credit card number.
For e.g.
Card Type : Visa
Card Number:  4111111111111124