Showing posts with label Dialog Widget. Show all posts
Showing posts with label Dialog Widget. Show all posts

Sunday, June 8, 2014

Step By Step How to open a JQuery UI Dialog from server side

        By Carmel Shvartzman
In this tutorial we'll learn how to open a JQuery UI Dialog from server side, showing at client side a message from the server side. For this tutorial, we'll not use the classic alert() javascript function, but a more sophisticated dialog widget from JQueryUI. 
The jQuery UI is a set of user interface widgets, interactions, and themes built on top of the jQuery JavaScript Library.
We'll customize the JQuery UI Dialog so that it's located in the MIDDLE of the web page, will open in 1000 milliseconds on the screen, and when closed it will disappear during another 1000 milliseconds using the "explode" jquery effect.
We'll want to add to our MVC site the jQuery Dialog , showing as follows:

First, go to the corresponding Controller to add the code to render a message to the browser:
 As you can see, the message will be contained in ViewBag, and will open only when a condition will met : in this case, when a session parameter exists.
Then go to the View, and add the Dialog widget as follows :


Now, we code the following to
1) HIDE the dialog while not necessary
2) DISPLAY the dialog IF the server requires it :


This is the code to copy-paste :

(SERVER SIDE) 
if ((string)Session["status"] == "OPEN_ALERT_WINDOW")
            {
                ViewBag.Alert = true;
                ViewBag.Message = "This is a message sent from the Web Server to client side";
                Session["status"] = null;
            }

(CLIENT SIDE)
        <div >
  <div id="effect" class="ui-widget-content ui-corner-all">  
    <p>
       @ViewBag.Message
    </p>
  </div>
</div>

@{
    <script>
        $("#effect").hide();
    </script>
 
    if (ViewBag.Alert == true)
    {
        <script>
            $("#effect").dialog({
                autoOpen: true,
                title:"Message",
                show: {
                    effect: "blind",
                    duration: 1000
                },
                hide: {
                    effect: "explode",
                    duration: 1000
                }
            });    
     
        </script>
    }  
}


Check whether you load the jquery.js & jquery-ui.js files, containing the necessary functions to call.
And the dialog will finally appears as this :




That's all!! 
In this tutorial we've learn how to open a JQuery UI Dialog from server side, showing at client side a message from the server side. 

Happy programming.....


כתב: כרמל שוורצמן

Friday, June 6, 2014

Step By Step How to create a JQuery UI Dialog widget in 10 minutes

        By Carmel Shvartzman

In this tutorial we'll learn how to use the JQuery UI Dialog widget  in 10 minutes. The jQuery UI is a set of user interface widgets, interactions, and themes built on top of the jQuery JavaScript Library.
We'll customize the JQuery UI Dialog so that it's located in the MIDDLE of the web page, will remain 2 seconds on the screen, and then will disappear. The custom Dialog will also contain a "Close"  button to hide itself.
We'll want to add to our MVC site the jQuery Dialog , showing as follows:




For the sake of using JQuery UI Widgets, like Accordion, Tabs, Portlets, Dialogs, and so on, we'll need to import a JQuery UI Theme, the jquery-ui.js and jquery-ui.css files. (and of course you'll need also a reference to the basic jquery.js file).

The present section is a sub-tutorial about importing the JQuery UI to our app: you can delay it if you already have done it, or you can see the complete step by step  in a former tutorial.

This is the BEGIN of the  sub-tutorial section: -----------------------------------------------------
---------------------------------------------------------------------------------------------------

First of all, we need to download the Theme from the JQuery UI site, so browse to the site and go to the "Themes" tab:


Select from the "Gallery" the theme you like the most, and download it. I choose the "Le-Frog":



After you download it, extract all from the .ZIP file :


We'll use some of these files. But first , we need to update the javascript jQuery files in our project, because the files which comes with Visual Studio will be obsolete. Open the NuGet utility:


Make a search for the JQuery package and install it:


Do the same for the JQuery UI package:


Now open the Theme's folder we downloaded and copy the entire theme which is inside the "development-bundle" folder:



Paste the folder inside the "themes" folder inside your project:


It will show as follows:


We'll make use of the .CSS file jquery-ui.css. Open the BundleConfig file:



Take a look at these ScriptBundles, because we'll use them inside our app:



Replace the "base" folder by the "le-frog" folder, and add the jquery-ui.css file to the script:


Next, we'll call the bundles from the _Layout file:


Locate the Render "jquery" section and cut it:


Locate the render at the <head> section:


Paste there the "jquery" section , and add the "jqueryui" bundle:



We're done with the javascript and the style we'll need.
Now we'll add the Dialog widget to the Index View. Create a new View:


This is the END of the  sub-tutorial section --------------------------------------------------------
---------------------------------------------------------------------------------------------------



To get the code of the Dialog, browse to the "Development" tab at the JQUERY UI site, and select  the "Effect" demo:




Then go to the "View source", because we'll customize the STYLE, SCRIPT and DIV sections of the Widget to make our own Dialog:






Put  an <script> tag inside your Index file, and code the following functions:


Here we have the following functions:
1) a hide() : to make invisible the Dialog;
2) a #btnOpenDialog : when the button is pressed, SHOW the Dialog;
3) a #btnCloseDialog : when the "Close" button is clicked, hide() the Dialog;
4) the fnShow() : show the Dialog using the "puff" effect during 2 secs, and THEN call a CALLBACK function named:
5) callback() : WAIT 2 secs, and then hide the Dialog using the "puff" effect during 2 secs.

<script>
    $(function () {
        $("#effect").hide();

        $("#btnOpenDialog").click(function () {
            fnShow();
        });

        $("#btnCloseDialog").click(function () {
            $("#effect").hide(500);
        });

        function fnShow() {
            $("#effect").show("puff", 2000, callback);
        }

        function callback() {
            setTimeout(function () {
                $("#effect").effect("puff", {}, 2000);
            }, 2000);
        };
     
    })</script>

Copy the Dialog <div> markup:


And paste it somewhere inside our Index file, with some customization and the adding of a "Close" button:



<div class="toggler" style="">
    <div id="effect" class="ui-widget-content ui-corner-all">
        <div>
            <div style="float: right;">
                <a href="#" id="btnCloseDialog" class="ui-state-default ui-corner-all">Close</a>
            </div>
            <h3 class="ui-widget-header ui-corner-all">Effect</h3>
        </div>
        <p>
            Etiam libero neque, luctus a, eleifend nec, semper at, lorem. Sed pede.
            Nulla lorem metus, adipiscing ut, luctus sed, hendrerit vitae, mi.
        </p>
    </div>
</div>

Also, add a button to open the dialog from the Client side:



<div style="width: 100%;">
    <a href="#" id="btnOpenDialog" class="ui-state-default ui-corner-all">Press to open a Dialog
    </a>
</div>

Last but not least, we need to customize the style of the Dialog, so copy the <style> CSS from the JQuery UI site:


 Again, we'll use the JQuery UI source as a bootstrap, but we'll customize it in order to make the Dialog appear IN THE MIDDLE OF THE WEB PAGE:



<style>
    .toggler {
        width: 500px;
        height: 200px;
        position: absolute;
        left: 600px;
        top: 350px;
    }
    #btnOpenDialog {
        position: fixed;
        left: 50%;
        margin-left: -100px;
        padding: 5px 5px;
        text-decoration: none;
        font: 900 12px georgia;
        color: #FFF;
    }
    #effect {
        width: 240px;
        height: 135px;
        position: fixed;
        top: 50%;
        left: 50%;
        margin-top: -50px;
        margin-left: -100px;
    }

    #effect h3 {
        margin: 0;
        padding: 0.4em;
        text-align: center;
    }
    .ui-effects-transfer {
        border: 2px dotted gray;
    }
</style>

Here, #effect is the name of the Dialog, and #btnOpenDialog is the button which opens it. As you see, i added a CSS code to position the JQuery UI Dialog IN THE MIDDLE of the web page:

      position: fixed;
        top: 50%;
        left: 50%;
        margin-top: -50px;
        margin-left: -100px;
What i'm telling here is something like this: "position yourself in the MIDDLE of the web page, and then move a bit up and left, to remain in the middle".

Finally, let's run the application to see the Dialog in action. The Button when clicked will open the Dialog:





The Dialog remains opened for only 2 seconds:







...and then it vanishes automatically:







Or can be closed by the user:




That's all!! 
In this tutorial we've learn how to use the JQuery UI Dialog widget in 10 minutes.  

Happy programming.....


כתב: כרמל שוורצמן



Wednesday, May 28, 2014

Step By Step How to create a Modal JQuery UI Dialog widget in 10 minutes

        By Carmel Shvartzman


In this tutorial we'll learn how to create a Modal JQuery UI Dialog widget in 10 minutes. The jQuery UI is a set of user interface widgets, interactions, and themes built on top of the jQuery JavaScript Library.

We'll want to add to our MVC site a Modal jQuery Dialog , showing as follows:




For the sake of using JQuery UI Widgets, like Accordion, Tabs, Portlets, Dialogs, and so on, we'll need to import a JQuery UI Theme, the jquery-ui.js and jquery-ui.css files. (and of course you'll need also a reference to the basic jquery.js file).

The present section is a sub-tutorial about importing the JQuery UI to our app: you can delay it if you already have done it, or you can see the complete step by step  in a former tutorial.

This is the BEGIN of the  sub-tutorial section: -----------------------------------------------------
---------------------------------------------------------------------------------------------------

First of all, we need to download the Theme from the JQuery UI site, so browse to the site and go to the "Themes" tab:


Select from the "Gallery" the theme you like the most, and download it. I choose the "Le-Frog":



After you download it, extract all from the .ZIP file :


We'll use some of these files. But first , we need to update the javascript jQuery files in our project, because the files which comes with Visual Studio will be obsolete. Open the NuGet utility:


Make a search for the JQuery package and install it:


Do the same for the JQuery UI package:


Now open the Theme's folder we downloaded and copy the entire theme which is inside the "development-bundle" folder:



Paste the folder inside the "themes" folder inside your project:


It will show as follows:


We'll make use of the .CSS file jquery-ui.css. Open the BundleConfig file:



Take a look at these ScriptBundles, because we'll use them inside our app:



Replace the "base" folder by the "le-frog" folder, and add the jquery-ui.css file to the script:


Next, we'll call the bundles from the _Layout file:


Locate the Render "jquery" section and cut it:


Locate the render at the <head> section:


Paste there the "jquery" section , and add the "jqueryui" bundle:



We're done with the javascript and the style we'll need.
Now we'll add the Dialog widget to the Index View. Create a new View:


This is the END of the  sub-tutorial section --------------------------------------------------------
---------------------------------------------------------------------------------------------------



To get the code of the Dialog, browse to the "Development" tab at the JQUERY UI site, and select  the "Dialog" Widget, and the "Animation" example:




Then go to the "View source", because we'll customize the STYLE, SCRIPT and DIV sections of the Widget to make our own Dialog:




Copy the <SCRIPT> tag from the JQuery UI site:



Put  an <script> tag inside your Index file, and customize the Dialog with the following code:




Here we have the following code:
1) "modal" = true: to make MODAL the Dialog;
2) "buttons" : when the button is pressed, will close the Dialog;
3) a #btnOpenDialog : when the "Open..." button is clicked, show the Dialog using the dialog("open ") function;

<script>
    $(function () {
        $("#btnOpenDialog").click(function () {
            $("#dialog-message").dialog("open");
        });
        $("#dialog-message").dialog({
            autoOpen: false,
            show: {
                effect: "blind",
                duration: 1000
            },
            hide: {
                effect: "explode",
                duration: 1000
            },
            modal: true,
            buttons: {
                Ok: function () {
                    $(this).dialog("close");
                }
            }
        });
    });
</script> 

Copy the Dialog <div> markup:



And paste it somewhere inside our Index file, with the following customization:



<div id="dialog-message" title="Settings for the Modal Dialog">
    <p>
        <span class="ui-icon ui-icon-circle-check" style="float: left; margin: 0 7px 50px 0;"></span>
        "autoOpen" must be set to "false"
        <br />
        "modal" must be set to "true"
    </p>
    <p>
        <span class="ui-icon ui-icon-circle-check" style="float: left; margin: 0 7px 50px 0;"></span>
        To modify the Show and the Hide effects :
        <br />
        <cite>show: {
                effect: "blind",
                duration: 1000
            }<br />
            hide: {
                effect: "explode",
                duration: 1000
            }
        </cite>
    </p>
</div> 

Also, add a button to open the dialog from the Client side:


<a id="btnOpenDialog" href="#" class="ui-state-default ui-corner-all">
    Open the Modal Dialog
</a>


Last but not least, we need to customize the style of the button:



<style>
    #btnOpenDialog {
        position: fixed;
        left: 50%;
        width: 150px;
        margin-left: -75px;
        padding: 5px 5px;
        text-decoration: none;
        font: 900 12px georgia;
        color: #FFF;
    }
</style>




Finally, let's run the application to see the Dialog in action. The Button when clicked will open the Dialog:





The Dialog remains opened in a Modal way:







...and when closed it "explodes" :




If you don't want the Dialog to be Modal, just write "modal = false" in the script:






That's all!! 
In this tutorial we've learn how to create a Modal JQuery UI Dialog widget in 10 minutes.  

Happy programming.....


כתב: כרמל שוורצמן