`
dsotbs936
  • 浏览: 38029 次
  • 性别: Icon_minigender_1
  • 来自: 浙江
文章分类
社区版块
存档分类
最新评论

Buttons, Menus, and Toolbars in Ext JS

阅读更多
by Colin Ramsay Shea Frederick Steve 'Cutter' Blades | June 2009 | AJAX Open Source

The unsung heroes of every application are the simple things like buttons, menus, and toolbars. In this article by Shea Frederick , Steve 'Cutter' Blades , and Colin Ramsay , we will cover how to add these items to our applications.

Our example will contain a few different types of buttons, both with and without menus. A button can simply be an icon, or text, or both. Toolbars also have some mechanical elements such as spacers and dividers that can help to organize the buttons on your toolbars items.

We will also cover how to make these elements react to user interaction.

 

A toolbar for every occasion

Just about every Ext component—panels , windows , grids can accept a toolbar on either the top or the bottom. The option is also available to render the toolbar standalone into any DOM element in our document. The toolbar is an extremely flexible and useful component that will no doubt be used in every application.

  • Ext.Toolbar : The main container for the buttons
  • Ext.Button : The primary handler for button creation and interaction
  • Ext.menu : A menu

Toolbars

Our first toolbar is going to be rendered standalone in the body of our document. We will add one of each of the main button types, so we can experiment with each:

  • Button—tbbutton : This is the standard button that we are all familiar with.
  • Split Button—tbsplit : A split button is where you have a default button action and an optional menu. These are used in cases where you need to have many options in the same category as your button, of which there is a most commonly used default option.
  • Menu—tbbutton+menu : A menu is just a button with the menu config filled in with options.
Ext.onReady(function(){
  new Ext.Toolbar({
    renderTo: document.body,
    items: [{
	xtype: 'tbbutton',
	text: 'Button'
    },{
	xtype: 'tbbutton',
	text: 'Menu Button',
	menu: [{
	  text: 'Better'
	},{
	  text: 'Good'
	},{
	  text: 'Best'
	}]
    },{
	xtype: 'tbsplit',
	text: 'Split Button',
	menu: [{
	   text: 'Item One'
	},{
	   text: 'Item Two'
	},{
	   text: 'Item Three'
        }]
     }]
  });
});

Buttons, Menus, and Toolbars in Ext JS

As usual, everything is inside our onReady event handler. The items config holds all of our toolbars elements—I say elements and not buttons because the toolbar can accept many different types of Ext components including form fields—which we will be implementing later on in this article.

The default xtype for each element in the items config is tbbutton. We can leave out the xtype config element if tbbutton is the type we want, but I like to include it just to help me keep track.

The button

Creating a button is fairly straightforward; the main config option is the text that is displayed on the button. We can also add an icon to be used alongside the text if we want to.

Here is a stripped-down button:

{
   xtype: 'tbbutton
',
   text: 'Button'
}

Buttons, Menus, and Toolbars in Ext JS

Menu

A menu is just a button with the menu config populated—it's that simple. The menu items work along the same principles as the buttons. They can have icons, classes, and handlers assigned to them. The menu items could also be grouped together to form a set of option buttons, but first let's create a standard menu.

This is the config for a typical menu config:

{
    xtype: 'tbbutton
',
    text: 'Button',
    menu
: [{
	text: 'Better'
    },{
	text: 'Good'
    },{
	text: 'Best'
    }]
}

Buttons, Menus, and Toolbars in Ext JS

As we can see, once the menu array config is populated, the menu comes to life. To group these menu items together, we would need to set the group config and the boolean checked value for each item:

menu: [{
    text: 'Better',
    checked: true,
    group: 'quality'
}, {
    text: 'Good',
    checked: false,
    group: 'quality'
}, {
    text: 'Best',
    checked: false,
    group: 'quality'
}]

Buttons, Menus, and Toolbars in Ext JS

Split button

The split button sounds like a complex component, but it's just like a button and a menu combined, with a slight twist. By using this type of button, you get to use the functionality of a button while adding the option to select an item from the attached menu. Clicking the left portion of the button that contains the text triggers the button action. However, clicking the right side of the button, which contains a small down arrow, triggers the menu.

{
    xtype: 'tbsplit
',
    text: 'Split Button',
    menu
: [{
	text: 'Item One'
    },{
	text: 'Item Two'
    },{
	text: 'Item Three'
    }]
}

Buttons, Menus, and Toolbars in Ext JS

Toolbar item alignment, dividers, and spacers

By default, every toolbar aligns elements to the leftmost side. There is no alignment config for a toolbar, so if we want to align all of the toolbar buttons to the rightmost side, we need to add a fill as the first item in the toolbar. If we want to have items split up between both the left and right sides, we can also use a fill:

{
   xtype: 'tbfill'
}

Pop this little guy in a tool-bar wherever you want to add space and he will push items on either side of the fill to the ends of the tool bar, as shown below:

Buttons, Menus, and Toolbars in Ext JS

We also have elements that can add space or vertical dividers, like the one used between the Menu Button and the Split Button .

The spacer adds a few pixels of empty space that can be used to space out buttons, or move elements away from the edge of the toolbar:

{
    xtype: 'tbspacer'
}

A divider can be added in the same way:

{
    xtype: 'tbseparator'
}

Shortcuts

Ext has many shortcuts that can be used to make coding faster. Shortcuts are a character or two that can be used in place of a configuration object. For example, consider the standard toolbar filler configuration:

{
    xtype: 'tbfill'
}

The shortcut for a toolbar filler is a hyphen and a greater than symbol:

'->'

Not all of these shortcuts are documented. So be adventurous, poke around the source code, and see what you can find. Here is a list of the commonly-used shortcuts:

 

http://www.packtpub.com/article/buttons-menus-toolbars-in-ext-js

分享到:
评论

相关推荐

    Ext JS in Action

    - **Menus, Buttons, and Toolbars (菜单、按钮和工具栏)**:这些基本组件构成了用户界面的核心部分,是实现各种功能的基础。 **4. 高级主题** - **Drag-and-Drop Basics (拖放基础)**:介绍了Ext JS中的拖放功能...

    Ext JS in Action (Final Edition).pdf

    - **第12章:菜单、按钮与工具栏**(Menus, Buttons, and Toolbars):讨论了用户界面中常见的交互组件。 - **第四部分:高级Ext JS**(第13章至第15章) - **第13章:拖拽基础**(Drag-and-drop basics):介绍...

    Ext JS Excel前台导出

    Ext JS 是一款基于 JavaScript 的前端框架,它提供了丰富的 UI 组件库以及强大的数据处理能力。通过使用 Ext JS,开发者可以快速构建复杂的企业级 Web 应用程序。 #### 三、Excel导出原理 在前端实现Excel导出通常...

    ext 教材 ext js 教程

    EXT JS 是一种流行的JavaScript库,专门用于构建富客户端Web应用程序。它提供了丰富的用户界面组件和强大的数据绑定功能。EXT JS 教材和教程的目标是帮助开发者更好地理解和掌握这个框架,从而提升他们的Web开发技能...

    Ext js学习资料

    - Ext JS不仅有表格,还包括窗口(Windows)、面板(Panels)、菜单(Menus)、按钮(Buttons)、表单(Forms)等多种UI组件,可以构建复杂的布局和交互。 - 数据绑定机制使得UI组件能与后台数据源实时同步,增强...

    learning ext js 中文版之在对话框中添加图标和按钮事件

    在EXT JS这个强大的JavaScript框架中,创建用户界面和交互元素是一项关键任务。"学习EXT JS中文版之在对话框中添加图标和按钮事件"这一主题深入探讨了如何为EXT JS的对话框(Modal Dialog)增添视觉吸引力和功能性,...

    ext学习资料ext学习资料

    Ext Js 是一个强大的JavaScript库,专门用于构建富客户端的Web应用程序。它提供了一整套的组件,包括数据绑定,事件处理,以及丰富的用户界面元素,使得开发者能够创建复杂的交互式应用而无需深入底层的HTML和CSS...

    Ext JS框架 经验之作2000页

    Ext JS是一款非常强大的JavaScript库,用于构建交互式的Web应用程序。它以其丰富的组件库、高性能以及易于使用的API而闻名。本篇文档不仅覆盖了Ext JS的基础知识,还深入探讨了一些高级主题,例如自定义组件、数据...

    ext学习资料 20篇详细学习笔记 初学者ext学习的文档

    EXT,全称EXT JS,是一种基于JavaScript的前端框架,由Sencha公司开发,主要用于构建富互联网应用程序(RIA)。EXT提供了一套完整的组件模型、数据绑定、事件系统和强大的布局管理,使得开发者能够创建功能丰富的、...

    Ext--MessageBox教程

    EXT JS是一个强大的JavaScript库,专为构建富客户端Web应用程序设计,而`MessageBox`则是EXT JS提供的一种轻量级的提示对话框,它提供了多种类型的提示,如信息、警告、确认和错误,以增强用户界面的交互性。...

    EXT弹出框改装实现

    EXT是一个流行的JavaScript框架,主要用于构建富客户端应用。它提供了一整套组件,包括弹出框(Window),使得开发者能够轻松创建交互式的用户界面。EXT弹出框改装通常涉及到自定义样式、行为以及功能,以满足特定...

    3D-Buttons-And-Boxes.zip

    3D-Buttons-And-Boxes.zip,2016年在Unity 3D中开发的Sokoban式拼图游戏,3D建模使用专门的软件来创建物理对象的数字模型。它是3D计算机图形的一个方面,用于视频游戏,3D打印和VR,以及其他应用程序。

    源码+书Hands-on Data Structures and Algorithms with JavaScript

    What you will learnBuild custom Back buttons embedded within your applicationBuild part of a basic JavaScript syntax parser and evaluator for an online IDEBuild a custom activity user tracker for your...

    This demonstrates how to add radio buttons to your menus

    "This demonstrates how to add radio buttons to your menus"这个主题聚焦于如何在菜单中集成单选按钮(Radio Buttons),这是一个常见的图形用户界面(GUI)控件,用于让用户在一组互斥选项中进行选择。...

    ext 编程开发指南

    EXT(Ext JS)是一个强大的JavaScript库,用于构建现代化的Web应用程序。它提供了丰富的用户界面组件、数据处理能力以及与服务器端进行交互的功能。对于初学者和想要深入了解EXT的人来说,本文档旨在提供一个简明...

    3------通过实例学习------Ext.js------.pdf

    Ext.js 是一个强大的JavaScript库,主要用于构建富客户端的Web应用程序。它的主要特点是提供了一套完整的组件模型、数据绑定机制和丰富的用户界面控件。在深入理解Ext.js之前,我们需要了解其基本结构和如何引入到...

    各种弹出窗口 ext窗口

    EXT 是一个流行的 JavaScript 框架,特别是它的 EXT JS 库,用于构建富客户端应用程序。在 EXT 2.0 版本中,MessageBox 是一个核心组件,提供了多种类型的弹出对话框。 EXT 2.0 MessageBox 的主要功能包括: 1. **...

Global site tag (gtag.js) - Google Analytics