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

Showing a list of actions to perform using the ActionSheet widget (Advanced)


The Kendo Mobile ActionSheet widget is used to show a list of actions to perform when a user taps on an element on the screen. The ActionSheet widget slides from the bottom and shows a list of actions to perform against the tapped element on the screen.

How to do it...

  1. Create a ListView widget with each list item containing an anchor element with its data-rel attribute set to actionsheet.

  2. Create an ActionSheet widget by setting the data-role attribute to actionsheet.

  3. The href attribute of the anchor elements in the ListView widget should be the same as the id attribute of the actionsheet widget.

    <div data-role="view">
      <ul data-role="listview"
        data-style="inset"
        data-type="group">
    
        <li>
          <ul>
            <li>
              <a data-rel="actionsheet"
                href="#optionActionSheet">
                Action 1
              </a>
            </li>
            <li>
              <a data-rel="actionsheet"
                href="#optionActionSheet">
                Action 2
              </a>
            </li>
            <li>
              <a data-rel="actionsheet"
                href="#optionActionSheet">
                Action 3
              </a>
            </li>
          </ul>
        </li>
    
      </ul>
    
      <ul data-role="actionsheet" id="optionActionSheet">
        <li><a data-action="option1">Option 1</a></li>
        <li><a data-action="option2">Option 2</a></li>
        <li><a data-action="option3">Option 3</a></li>
      </ul>
    </div>

The previous code snippet defines a view containing a ListView widget. The ListView widget contains anchor elements of the following form:

<a data-rel="actionsheet"
  href="#optionActionSheet">
  Action 1
</a>

Here, the anchor element specifies the data-rel attribute with its value as actionsheet. This data-rel attribute is used to open the ActionSheet widget from any navigational widget such as a button, an anchor link item, or a tabstrip. Also, the href attribute of the anchor element specifies the id value of the ActionSheet widget (openActionSheet) to be invoked.

The ActionSheet widget is defined with the data-role attribute set to actionsheet and its id attribute set to openActionSheet. This widget contains three list items containing a list of actions to perform. The data-action attribute is used to specify the JavaScript function to invoke when the user taps one of the options.

How it works...

When you execute the previous code snippet, you will see that a ListView widget is shown as follows:

On clicking on any of the list items in the ListView widget, the ActionSheet widget will slide in from the bottom of the screen to show a list of options, as shown in the following screenshot:

Notice that along with the three options, another option with the value Cancel is also added to the list. This allows the user to cancel the task and hide the ActionSheet widget.

There's more...

As mentioned earlier, the data-action attribute is used to specify the JavaScript callback function to be invoked when the user selects one of the options in the ActionSheet widget. This callback function receives an object with two fields, namely, target and context. The target field holds the reference to the DOM element that opened the ActionSheet widget and the context field will contain the value passed from the same DOM element. To pass a value to the ActionSheet widget, specify the data-actionsheet-context attribute with the value that you need to pass.

<a id="firstItem"
  data-rel="actionsheet"
  data-actionsheet-context="1"
  href="#optionActionSheet">
  Action 1
</a>

In the previous markup, one of the list items is updated to contain a data-actionsheet-context attribute with the value 1. The JavaScript function is defined as follows:

function option1 (e) {
  console.log(e.target); // $('#firstItem')
  console.log(e.context); // 1
}

Here, the target field will refer to the DOM element that invoked the ActionSheet widget and the context field will contain the value 1, which was passed from the DOM element that invoked the ActionSheet widget.

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