Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Kendo UI Mobile

You're reading from   Instant Kendo UI Mobile Practical recipes to learn the Kendo UI Mobile library and its various components for building mobile applications effectively

Arrow left icon
Product type Paperback
Published in Jul 2013
Publisher Packt
ISBN-13 9781849699112
Length 60 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Sagar Ganatra Sagar Ganatra
Author Profile Icon Sagar Ganatra
Sagar Ganatra
Arrow right icon
View More author details
Toc

Displaying a Modal window using the ModalView widget (Advanced)


A ModalView widget is similar to an ActionSheet widget, which is invoked by tapping on one of the widgets on the screen. A ModalView widget is used to present a window-like view as an overlay over an existing view in the page. In case of ActionSheet, the widget will slide up from the bottom of the screen, whereas a ModalView widget will be shown at the center of the screen.

How to do it...

  1. Create an anchor element with its data-rel attribute set to modalview.

  2. Create a ModalView widget by setting the data-role attribute to modalview.

  3. The anchor element's href attribute should be same as the modal view's id attribute, as shown in the following code:

    <div data-role="view">
      <a data-role="button"
        data-rel="modalview"
        href="#modalview-form">
    
        Login
      </a>
    </div>
    
    <div data-role="modalview"
      id="modalview-form"
      style="width: 70%; height: 270px;">
    
      <div data-role="header">
        <div data-role="navbar">
    
          <span>Login</span>
          <a data-click="closeBtnHandler"
            data-role="button"
            data-align="right">
    
            Cancel
          </a>
    
        </div>
      </div>
    
      <ul data-role="listview"
        data-style="inset">
    
        <li>
          <label for="username">Username:</label>
          <input type="text" id="username" />
        </li>
        <li>
          <label for="password">Password:</label>
          <input type="password" id="password" />
        </li>
    
      </ul>
    
      <a data-click="loginBtnHandler"
        type="button"
        data-role="button"
        style="margin-left: 40%;">
    
        Login
      </a>
      <a data-click="registerBtnHandler"
        type="button"
        data-role="button">
    
        Register
      </a>
    </div>

The previous code is very similar to the ActionSheet widget that we created in the last recipe. Here, the view specifies the value of the data-rel attribute as modalview and also specifies the id value of the ModalView widget (that is, modalview-form) as it's href attribute.

The ModalView widget has the id attribute set to modalview-form and the data-role attribute set to modalview to mark the target element as a ModalView widget. The widget can then contain any number of elements in it. The following widgets are added to the previous code snippet: a header containing the Navbar with the Cancel button, form elements that would capture the user's login credentials, and buttons to handle the login and register functionality.

How it works...

When the page loads, you will see a Login button shown. On clicking on the Login button, a modal view containing the form elements is shown.

There's more...

For a ModalView widget, you can specify the data-open attribute whose value will be the name of a JavaScript callback function. This function is invoked whenever the ModalView widget is opened.

<div
  data-role="modalview"
  id="modalview-form"
  data-open="openModalView">

  ....
</div>

This is particularly helpful when you are managing the state of the application. For example, you may want to execute a JavaScript function that changes the state of the application when the ModalView widget is opened.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime