3月 31, 2020 Sencha
![](https://cdn.19site.net/files/2c/75/2c757dca-db3a-4598-80c0-16db230f1a37.png '在 Grid 即時修改資料')

要達到可以在 Grid 內即時修改 Cell 的內容,我們要為 Grid 加入 Plugin 設定才可以。

### 為 Grid Panel 加入 Plugin 設定

我們需要為 Grid Panel 加入以下的設定 :

```js
{
	selModel: 'cellmodel',
	plugins: {
		ptype: 'cellediting',
		clicksToEdit: 1
	}
}
```

### 參數解說 

-	selModel : 選擇 Grid 內容時所以使用的 Model
-	ptype : Plugin 的種類
-	clicksToEdit : 按多少下滑鼠可以進入修改模式

### Column 設定

除了要設定好 Grid 外,還需要為你目標相修改的 column 進行設定 :

```js
{
	columns: [
		{header: 'name', dataIndex: 'name', editor: 'textfield'},
		{header: 'name', dataIndex: 'name', 
			editor: {
				completeOnEnter: false,
				field: {
					xtype: 'textfield',
					allowBlank: false
				}
		},
	]
}
```

### 參數解說 

-	editor (String) - 可以直接使用 xtype 來指定修改器的種類
-	editor (Object) - 可以通過使用 field 方法來指定修改器的種類,更適合像 Combobox 一類的 Component。

### 例子

以下例子是從 ExtJS 的官方說明文件中節錄出來 :  
https://docs.sencha.com/extjs/6.0.2/classic/Ext.grid.plugin.CellEditing.html

```js
Ext.create('Ext.data.Store', {
	storeId: 'simpsonsStore',
	fields:[ 'name', 'email', 'phone'],
	data: [
		{ name: 'Lisa', email: 'lisa@simpsons.com', phone: '555-111-1224' },
		{ name: 'Bart', email: 'bart@simpsons.com', phone: '555-222-1234' },
		{ name: 'Homer', email: 'homer@simpsons.com', phone: '555-222-1244' },
		{ name: 'Marge', email: 'marge@simpsons.com', phone: '555-222-1254' }
	]
});

Ext.create('Ext.grid.Panel', {
	title: 'Simpsons',
	store: Ext.data.StoreManager.lookup('simpsonsStore'),
	columns: [
		{header: 'Name', dataIndex: 'name', editor: 'textfield'},
		{header: 'Email', dataIndex: 'email', flex:1,
			editor: {
				completeOnEnter: false,

				// If the editor config contains a field property, then
				// the editor config is used to create the Ext.grid.CellEditor
				// and the field property is used to create the editing input field.
				field: {
					xtype: 'textfield',
					allowBlank: false
				}
			}
		},
		{header: 'Phone', dataIndex: 'phone'}
    	],
	selModel: 'cellmodel',
	plugins: {
		ptype: 'cellediting',
		clicksToEdit: 1
	},
	height: 200,
	width: 400,
	renderTo: Ext.getBody()
});
```
過去文章
2025 (9)
4 (5)
3 (1)
2 (3)
2024 (25)
11 (3)
10 (3)
9 (1)
3 (18)
2022 (6)
10 (1)
6 (2)
5 (1)
3 (1)
1 (1)
2021 (21)
11 (7)
7 (1)
6 (2)
5 (2)
4 (6)
3 (2)
2 (1)
2020 (92)
12 (1)
11 (2)
10 (4)
9 (10)
8 (5)
7 (1)
6 (3)
5 (1)
4 (4)
3 (25)
2 (7)
1 (29)
2019 (57)
12 (25)
11 (7)
9 (25)