extjs study notes (3) The most basic grid_extjs
jquery is just the opposite in this regard. Its UI is provided in the form of plug-ins. You can reference whatever you need, so it is very small and flexible. However, since plug-ins are often provided by different people or teams, the interfaces and interfaces are often not so consistent. Anyway, each has its own merits.
Today I’m learning grid in extjs. It can be said to be powerful and unparalleled. There are only things you can’t think of and there’s nothing it can’t do. Haha, it seems to be a bit exaggerated. Okay, without further ado, let’s start with the simplest grid and see step by step what functions the grid provided by extjs provides us.
A grid includes some rows and columns. In extjs, columns are managed by Ext.grid.ColumnModel. Let’s take a look at how to create a ColumnModel object:
var cm = new Ext.grid.ColumnModel([
{id:'company',header: "Company", width: 160, sortable: true, dataIndex: 'company'},
{header: "Price", width: 75, sortable: true, dataIndex: 'price'},
{header: "Change", width: 75, sortable: true, dataIndex: 'change'},
{header: "% Change", width: 75, sortable: true, dataIndex: 'pctChange'},
{header: "Last Updated ", width: 85, sortable: true, dataIndex: 'lastChange'}
]);
Five columns are defined here, and the columns can be configured through parameters: id is used to identify the column , use this id in css to set styles for all cells in the entire column. Automatically expandable columns are also identified according to this id; header is the column name; width is the width of the column; sortable is used to indicate whether the column can be sorted, dataIndex , ignore it for now. The more commonly used parameters include: editable, indicating whether the column is editable; renderer, indicating how the column is presented, which will be introduced in detail later.
With the columns, we still need some data to fill the rows, let’s construct an array:
var myData = [
['3m Co',71.72,0.02,0.03,'9/1 12:00am'],
['Alcoa Inc',29.01,0.42 ,1.47,'9/1 12:00am'],
['Altria Group Inc',83.81,0.28,0.34,'9/1 12:00am'],
['American Express Company',52.55 ,0.01,0.02,'9/1 12:00am'],
['American International Group, Inc.',64.13,0.31,0.49,'9/1 12:00am'],
['AT&T Inc.',31.61,-0.48,-1.54,'9/1 12:00am'],
['Boeing Co.',75.43,0.53,0.71,'9/1 12:00am'],
['Caterpillar Inc.',67.27,0.92,1.39,'9/1 12:00am'],
['Citigroup, Inc.',49.37,0.02,0.04,'9/1 12:00am'] ,
['E.I. du Pont de Nemours and Company',40.48,0.51,1.28,'9/1 12:00am'],
['Exxon Mobil Corp',68.1,-0.43,-0.64,' 9/1 12:00am'],
['General Electric Company',34.14,-0.08,-0.23,'9/1 12:00am'],
['General Motors Corporation',30.27,1.09 ,3.74,'9/1 12:00am'],
['Hewlett-Packard Co.',36.53,-0.03,-0.08,'9/1 12:00am'],
['Honeywell Intl Inc',38.77,0.05,0.13,'9/1 12:00am'],
['Intel Corporation',19.88,0.31,1.58,'9/1 12:00am'],
['International Business Machines',81.41,0.44,0.54,'9/1 12:00am'],
['Johnson & Johnson',64.72,0.06,0.09,'9/1 12:00am'],
[ 'JP Morgan & Chase & Co',45.73,0.07,0.15,'9/1 12:00am'],
['McDonald's Corporation',36.76,0.86,2.40,'9/1 12:00am'],
['Merck & Co., Inc.',40.96,0.41,1.01,'9/1 12:00am'],
['Microsoft Corporation',25.84,0.14,0.54,'9/1 12 :00am'],
['Pfizer Inc',27.96,0.4,1.45,'9/1 12:00am'],
['The Coca-Cola Company',45.07,0.26,0.58,'9 /1 12:00am'],
['The Home Depot, Inc.',34.64,0.35,1.02,'9/1 12:00am'],
['The Procter & Gamble Company',61.91 ,0.01,0.02,'9/1 12:00am'],
['United Technologies Corporation',63.26,0.55,0.88,'9/1 12:00am'],
['Verizon Communications', 35.57,0.39,1.11,'9/1 12:00am'],
['Wal-Mart Stores, Inc.',45.45,0.73,1.63,'9/1 12:00am']
];
Now everything is ready. The columns are defined and the data is available. The next step is to assemble them into a grid. Take a look at the complete code:
///
/**//*
*Author: Daben
*Date: 2009-10-13
*Version: 1.0
*/
Ext.onReady(function() {
//构造列
var cm = new Ext.grid.ColumnModel([
{ id: 'company', header: "Company", width: 160, sortable: true, dataIndex: 'company' },
{ header: "Price", width: 75, sortable: true, dataIndex: 'price' },
{ header: "Change", width: 75, sortable: true, dataIndex: 'change' },
{ header: "% Change", width: 75, sortable: true, dataIndex: 'pctChange' },
{ header: "Last Updated", width: 85, sortable: true, dataIndex: 'lastChange' }
]);
//构造数据
var myData = [
['3m Co', 71.72, 0.02, 0.03, '9/1 12:00am'],
['Alcoa Inc', 29.01, 0.42, 1.47, '9/1 12:00am'],
['Altria Group Inc', 83.81, 0.28, 0.34, '9/1 12:00am'],
['American Express Company', 52.55, 0.01, 0.02, '9/1 12:00am'],
['American International Group, Inc.', 64.13, 0.31, 0.49, '9/1 12:00am'],
['AT&T Inc.', 31.61, -0.48, -1.54, '9/1 12:00am'],
['Boeing Co.', 75.43, 0.53, 0.71, '9/1 12:00am'],
['Caterpillar Inc.', 67.27, 0.92, 1.39, '9/1 12:00am'],
['Citigroup, Inc.', 49.37, 0.02, 0.04, '9/1 12:00am'],
['E.I. du Pont de Nemours and Company', 40.48, 0.51, 1.28, '9/1 12:00am'],
['Exxon Mobil Corp', 68.1, -0.43, -0.64, '9/1 12:00am'],
['General Electric Company', 34.14, -0.08, -0.23, '9/1 12:00am'],
['General Motors Corporation', 30.27, 1.09, 3.74, '9/1 12:00am'],
['Hewlett-Packard Co.', 36.53, -0.03, -0.08, '9/1 12:00am'],
['Honeywell Intl Inc', 38.77, 0.05, 0.13, '9/1 12:00am'],
['Intel Corporation', 19.88, 0.31, 1.58, '9/1 12:00am'],
['International Business Machines', 81.41, 0.44, 0.54, '9/1 12:00am'],
['Johnson & Johnson', 64.72, 0.06, 0.09, '9/1 12:00am'],
['JP Morgan & Chase & Co', 45.73, 0.07, 0.15, '9/1 12:00am'],
['McDonald's Corporation', 36.76, 0.86, 2.40, '9/1 12:00am'],
['Merck & Co., Inc.', 40.96, 0.41, 1.01, '9/1 12:00am'],
['Microsoft Corporation', 25.84, 0.14, 0.54, '9/1 12:00am'],
['Pfizer Inc', 27.96, 0.4, 1.45, '9/1 12:00am'],
['The Coca-Cola Company', 45.07, 0.26, 0.58, '9/1 12:00am'],
['The Home Depot, Inc.', 34.64, 0.35, 1.02, '9/1 12:00am'],
['The Procter & Gamble Company', 61.91, 0.01, 0.02, '9/1 12:00am'],
['United Technologies Corporation', 63.26, 0.55, 0.88, '9/1 12:00am'],
['Verizon Communications', 35.57, 0.39, 1.11, '9/1 12:00am'],
['Wal-Mart Stores, Inc.', 45.45, 0.73, 1.63, '9/1 12:00am']
];
//构造grid
var grid = new Ext.grid.GridPanel({
renderTo: "grid",
store: new Ext.data.ArrayStore({
fields: [
{ name: 'company' },
{ name: 'price', type: 'float' },
{ name: 'change', type: 'float' },
{ name: 'pctChange', type: 'float' },
{ name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia' }
],
data:myData
}),
cm: cm,
stripeRows: true,
autoExpandColumn: 'company',
height: 350,
width: 600,
title: 'Array Grid'
});
})
In extjs, Ext.grid.GridPanel represents a grid, which requires a json object as a parameter for configuration. Let’s take a look at the configuration used:
renderTo: indicates where the grid will be rendered after it is constructed. It can be the id of an element, a Dom node or an Element object. If there is no such parameter, the render method of Ext.grid.GridPanel must be called to render the grid.
stroe: It can be abbreviated as ds, providing data to the grid with a unified interface. We know that the data may have many formats. In addition to the arrays we use, it can also be json, xml, etc. If the grid is responsible It is obviously not a good design idea to identify each data format, so there is a special class Ext.data.Store in extjs to be responsible for data format conversion. Here we use its subclass ArrayStore, which, as the name suggests, is a specialized For conversion to arrays. We will have a dedicated series to discuss some classes under the Ext.data namespace, and then we will have an in-depth understanding of the Store class. Now we only look at the fields we use, which is a collection of the Ext.data.Field class. This class has a name attribute. The dataIndex we ignored in the ColumnModel previously quoted the value of this attribute to correspond to the column and Field. The relationship between, type is used to indicate the type, the default is string type, and dateFormat indicates the format of the date.
cm: The abbreviation of colModel, just put the ColumnModel object we constructed earlier here.
stripeRows: Whether to display stripes.
autoExpandColumn: an automatically expanded column that automatically fills the empty space of the grid.
height and width: the height and width of the grid.
title: The title of the grid.
Now let’s run it and see the effect. It should be said that this grid is quite beautiful. Clicking on the column name can also be sorted. The width of the column can be freely dragged and the position can also be changed. By pressing the ctrl key, we can select multiple rows. When we move the mouse over the column name, we can also see a small inverted triangle appearing. Click to display a menu, and you can see that the columns can be sorted and the columns can be hidden. However, we have also seen that the date display is not satisfactory, and the percentage is still a floating point number, and we usually use deficit to represent loss or the more popular term negative growth. If it is in our grid, negative growth can also be represented by deficit. Come out, the effect should be better. extjs provides us with a very convenient thing to realize our ideas: renderer. In the ColumnModel, we can add renderer to the required columns to achieve the effect we want. The following is the modified ColumnModel:
//Construct column
var cm = new Ext.grid.ColumnModel([
{ id: 'company', header: "Company", width: 160, sortable: true, dataIndex: 'company' },
{ header: "Price", width: 75, sortable: true, dataIndex: 'price' },
{ header: "Change", width: 75, sortable: true, dataIndex: 'change', renderer: change },
{ header: "% Change", width: 75, sortable: true, dataIndex: 'pctChange', renderer: pctChange },
{ header: "Last Updated", width: 120, sortable: true, dataIndex: ' lastChange',renderer:Ext.util.Format.dateRenderer("Y-m-d h:i") }
]);
// Change column renderer function
function change(val) {
if (val > 0) {
return '' val '';
} else if (val < 0) {
return '' val '';
}
return val;
}
// % Change column renderer Function
function pctChange(val) {
if (val > 0) {
return '' val '%';
} else if (val < 0) {
return '' val '%';
}
return val;
}
renderer can be understood as an "interpreter" method, which is used to transform data before displaying it. There are three ways to implement a renderer:
Use a renderer function that returns HTML markup
An attribute of the Ext.util.Format class that provides a renderer function
An object that includes the renderer function and scope
Our example uses the first two methods. The renderer function passes 6 parameters and saves all the information of the cell. Only the first parameter is used here, which saves the value of the cell. We can refer to the help document for the meaning of other parameters.
Now run the program and you can see the effect we want: negative growth is represented by deficit, as a comparison, positive growth is represented by green color, and the time is also displayed in the format we want.
Sometimes, we also want to number each row. This is easy to implement. We only need to add new Ext.grid.RowNumberer() to the constructor of ColumnModel:
var cm = new Ext.grid.ColumnModel([
new Ext.grid .RowNumberer(), //Implement automatic numbering
{ id: 'company', header: "Company", width: 160, sortable: true, dataIndex: 'company' },
{ header: "Price" , width: 75, sortable: true, dataIndex: 'price' },
{ header: "Change", width: 75, sortable: true, dataIndex: 'change', renderer: change },
{ header : "% Change", width: 75, sortable: true, dataIndex: 'pctChange', renderer: pctChange },
{ header: "Last Updated", width: 120, sortable: true, dataIndex: 'lastChange', renderer:Ext.util.Format.dateRenderer("Y-m-d h:i") }
]);
There are two commonly used parameters in the configuration of Ext.grid.GridPanel:
viewConfig: We can use this parameter to make some settings for the grid interface. For details, please view the help document.
selModel: Can be abbreviated as sm, select the model, such as selecting cells or selecting the entire row. The default is to select the row.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In front-end interviews, we are often asked how to implement dice/mahjong layout using CSS. The following article will introduce to you how to use CSS to create a 3D dice (Flex and Grid layout implement 3D dice). I hope it will be helpful to you!

PHP study notes: Overview of data structures and algorithms: Data structures and algorithms are two very important concepts in computer science. They are the key to solving problems and optimizing code performance. In PHP programming, we often need to use various data structures to store and operate data, and we also need to use algorithms to implement various functions. This article will introduce some commonly used data structures and algorithms, and provide corresponding PHP code examples. 1. Linear structure array (Array) Array is one of the most commonly used data structures and can be used to store ordered data.

CSS layout framework: Explore the five commonly used layout frameworks Introduction: In web design, layout is a crucial part. The CSS layout framework can help us quickly build web pages with different layout styles. This article will introduce five commonly used CSS layout frameworks and provide specific code examples to help readers better understand and use these frameworks. 1. Bootstrap: Bootstrap is one of the most popular CSS layout frameworks currently. It has rich components and powerful responsive features that can

With the continuous development and popularity of Web applications, more and more companies and individuals are beginning to use PHP and ExtJS to build powerful Web applications. As a popular server-side scripting language, PHP is cross-platform and easy to learn, while ExtJS is a popular front-end framework that can help developers quickly build interactive web application interfaces. This article will introduce how to use PHP and ExtJS to implement powerful web application functions. Establish PHP and MySQL database connections for use

PHP study notes: Forum and blog system development In the field of Web development, forums and blog systems are very common applications. They provide a platform for users to communicate and share information. In this article, we will discuss how to use PHP to develop a simple forum and blog system, and attach specific code examples. Environment setup First, we need to set up a development environment suitable for PHP development. We can use AMP (Apache, MySQL and PHP) packages like XAMPP or WAM

Detailed explanation of CSS relative layout properties: position and relative In front-end development, layout is often a problem that developers need to face. In order to better control the position of elements on the page, CSS provides a variety of layout methods. Among them, relative layout is a very common layout method. By using the position and relative attributes, we can flexibly adjust the position and size of elements. The position attribute is used to define the positioning method of the element. Common values

CSS panel layout properties: grid and grid-template-columns In modern web page layout, panel layout is a common design method that can arrange web content in a grid. The grid layout attribute in CSS and the grid-template-columns attribute are the keys to realizing panel layout. 1. Introduction to the grid layout attribute The grid layout attribute is an attribute used to create a grid layout in CSS. By converting HTML

UniApp’s implementation skills for implementing page layout and responsive design Introduction: UniApp is a cross-platform development tool based on the Vue.js framework, which can simultaneously develop applications for multiple platforms such as iOS, Android, and H5. This article will introduce how to use UniApp to implement page layout and responsive design, and provide some practical code examples. 1. Page layout Flex layout Flex layout is a commonly used method in page layout, which can automatically adapt to different screen sizes and devices. In UniApp
