ExtJS 4 Grids 詳解

來源:互聯網
上載者:User

標籤:

Grid Panel是ExtJS最常用的組件之一,它的功能非常豐富,提供了非常便捷的方法執行排序,分組,編輯資料。 Basic Grid Panel 基本表格面板

讓我們建立一個簡單的表格,這有建立和運行表格的全部知識。 Model and Store 模型和儲存空間

Grid Panel展現Store中的資料,Store可以被認為是records的集合,或者模型(Model)執行個體的集合。更多關於Store和Model的內容請查看《ExtJS 4 資料(包)詳解》,講這些是為了明確一下概念,Grid Panel本身只關注如何展現資料,Store負責通過Proxy擷取和儲存資料。 首先我們需要定義一個Model,Model就是一組資料欄位,先定義一個User Model

 
Ext.define(‘User‘, {    extend: ‘Ext.data.Model‘,    fields: [ ‘name‘, ‘email‘, ‘phone‘ ]});

然後建立Store以容納若干User的執行個體

 
var userStore = Ext.create(‘Ext.data.Store‘, {    model: ‘User‘,    data: [        { name: ‘Lisa‘, email: ‘[email protected]‘, phone: ‘555-111-1224‘ },        { name: ‘Bart‘, email: ‘[email protected]‘, phone: ‘555-222-1234‘ },        { name: ‘Homer‘, email: ‘[email protected]‘, phone: ‘555-222-1244‘ },        { name: ‘Marge‘, email: ‘[email protected]‘, phone: ‘555-222-1254‘ }    ]});

簡單起見,這裡使用了內聯的資料,真實應用中,通常都是使用代理載入伺服器端的資料,有關代理可以查看《ExtJS 4 資料(包)詳解》Grid Panel 表格面板

現在我們已經有了模型定義資料結構,也載入了若干個模型執行個體到Store,已經為表格面板展示資料做好了準備

 
Ext.create(‘Ext.grid.Panel‘, {    renderTo: Ext.getBody(),    store: userStore,    width: 400,    height: 200,    title: ‘Application Users‘,    columns: [        {            text: ‘Name‘,            width: 100,            sortable: false,            hideable: false,            dataIndex: ‘name‘        },        {            text: ‘Email Address‘,            width: 150,            dataIndex: ‘email‘,            hidden: true        },        {            text: ‘Phone Number‘,            flex: 1,            dataIndex: ‘phone‘        }    ]});

這就是全部要做的。我們建立了一個表格面板渲染到了body中,並且告訴它從之前建立的userStore中取得資料,最後我們定義了表格面板有哪些列,用dataIndex屬性配置表格中的列和User Model中的欄位的對應關係,Name列寬度是100,不能排序也不能隱藏,Email Address列預設是隱藏的(可以通過其他列的菜單控制顯示),Phone Number列自適應表格剩餘的寬度Renderers 渲染器

你可以使用renderer屬性改變資料的展示方式,renderer是個function,它接收原始的資料並且需要返回一個處理過的資料,返回的資料將會被展示在表格上,一些最常用的渲染器在Ext.util.Format中可以找到,也可以自訂:

 
columns: [    {        text: ‘Birth Date‘,        dataIndex: ‘birthDate‘,        // format the date using a renderer from the Ext.util.Format class        renderer: Ext.util.Format.dateRenderer(‘m/d/Y‘)    },    {        text: ‘Email Address‘,        dataIndex: ‘email‘,        // format the email address using a custom renderer        renderer: function(value) {            return Ext.String.format(‘<a href="mailto:{0}">{1}</a>‘, value, value);        }    }]
Grouping 分組

讓表格中的行分組排列很容易,首先要在Store中指定一個groupField屬性

 
Ext.create(‘Ext.data.Store‘, {    model: ‘Employee‘,    data: ...,    groupField: ‘department‘});

關於分組的更多內容,請參見《ExtJS 4 資料(包)詳解》,接下來配置表格有分組屬性:

 
Ext.create(‘Ext.grid.Panel‘, {    ...    features: [{ ftype: ‘grouping‘ }]});
Selection Models 選中模式

有些時候,表格是用來在螢幕上展示資料的,但是經常會需要更新資料或者和表格中的資料做互動,所有表格都有個選中模式,用來控製表格中的資料是如何選中的,主要兩種選中模式是行模式(Row Selection Model)和儲存格模式(Cell Selection Model)
表格預設使用行模式,但是切換到儲存格模式也很容易:

 
Ext.create(‘Ext.grid.Panel‘, {    selType: ‘cellmodel‘,    store: ...});

使用儲存格模式改變了幾個事情,第一,點擊表格中的儲存格只會選中目前的儲存格,而在行模式的時候是選中整行的,第二,用鍵盤導航的時候會從一個儲存格走到另一個儲存格,而不是從一行走到另一行,儲存格模式通常配合可編輯的表格一起使用。Editing 編輯表格

表格內建了編輯能力,我們見識一下,行編輯和儲存格編輯Cell Editing 儲存格編輯

儲存格編輯允許你一次編輯一個儲存格內容,實現儲存格編輯,首先要為每一列配置編輯控制項,只要設定editor屬性即可,最簡單的辦法就是將editor屬性設定為需要使用的控制項的xtype

 
Ext.create(‘Ext.grid.Panel‘, {    ...    columns: [        {            text: ‘Email Address‘,            dataIndex: ‘email‘,            editor: ‘textfield‘       }    ]});

如果你需要控制編輯控制項的行為,可以把editor屬性設定為編輯控制項可接收的設定物件,例如我們設定一個不可為空的輸入框:


 
columns: [    {        text: ‘Name‘,        dataIndex: ‘name‘,        editor: {            xtype: ‘textfield‘,            allowBlank: false        }    }]

可以使用任意Ext.form.field包中的控制項作為編輯控制項,假設某一列是日期類型,需要使用Date Field編輯:

 
columns: [    {        text: ‘Birth Date‘,        dataIndex: ‘birthDate‘,        editor: ‘datefield‘    }]

沒有配置editor屬性的列,就是不可編輯的。
現在我們已經配置了哪一列可以編輯,也配置了用什麼控制項編輯,下一步就是制定選中模式,我們先使用一下 儲存格模式:

 
Ext.create(‘Ext.grid.Panel‘, {    ...    selType: ‘cellmodel‘});

最後我們需要安裝一個儲存格編輯外掛程式:

 
Ext.create(‘Ext.grid.Panel‘, {    ...    selType: ‘cellmodel‘,    plugins: [        Ext.create(‘Ext.grid.plugin.CellEditing‘, {            clicksToEdit: 1        })    ]});

以上就是全部實現一個可編輯儲存格的表格的內容。效果如下:

Row Editing 行編輯

行編輯允許你一次編輯一整行,和儲存格編輯的實現方式基本一致,不一樣的地方是,選中模式要設定為行模式,安裝一個行編輯外掛程式

 
Ext.create(‘Ext.grid.Panel‘, {    ...    selType: ‘rowmodel‘,    plugins: [        Ext.create(‘Ext.grid.plugin.RowEditing‘, {            clicksToEdit: 1        })    ]});

Paging 分頁

有時候資料很多不能一次全部展現在表格中。表格支援兩種分頁模式,一種是分頁工具條,有上一頁/下一頁按鈕,一種是無限捲軸Store Setup 儲存空間設定

開始之前首先要讓Store支援分頁,下面的例子中為store增加了pageSize屬性,並且給Reader配置了totalProperty告知Reader一共有多少行

 
Ext.create(‘Ext.data.Store‘, {    model: ‘User‘,    autoLoad: true,    pageSize: 4,    proxy: {        type: ‘ajax‘,        url : ‘data/users.json‘,        reader: {            type: ‘json‘,            root: ‘users‘,            totalProperty: ‘total‘        }    }});

totalProperty告知reader從哪裡取得資料集的總數目,然後Store需要的JSON資料應該像這樣:

 
{    "success": true,    "total": 12, //這告知一共多少行    "users": [ //當前頁只需要4行,因為pageSize是4        { "name": "Lisa", "email": "[email protected]", "phone": "555-111-1224" },        { "name": "Bart", "email": "[email protected]", "phone": "555-222-1234" },        { "name": "Homer", "email": "[email protected]", "phone": "555-222-1244" },        { "name": "Marge", "email": "[email protected]", "phone": "555-222-1254" }    ]}
Paging Toolbar 分頁工具條

現在我們已經讓Store支援分頁了,剩下的就是配置分頁工具條,你可以把分頁工具條放在任何位置,但是通常放在表格的底部

 
Ext.create(‘Ext.grid.Panel‘, {    store: userStore,    columns: ...,    dockedItems: [{        xtype: ‘pagingtoolbar‘,        store: userStore,   // same store GridPanel is using        dock: ‘bottom‘,        displayInfo: true    }]});

Paging Scroller 分頁捲軸

表格支援無限捲軸的方式分頁,有上千條資料時你可以一直滾動捲軸就可以載入(跟微博的無限捲軸一樣),沒有一次渲染數千條的效能問題,需要做如下的配置

 
Ext.create(‘Ext.grid.Panel‘, {    // Use a PagingGridScroller (this is interchangeable with a PagingToolbar)    verticalScrollerType: ‘paginggridscroller‘,    // do not reset the scrollbar when the view refreshs    invalidateScrollerOnRefresh: false,    // infinite scrolling does not support selection    disableSelection: true,    // ...});

ExtJS 4 Grids 詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.