首頁  >  文章  >  web前端  >  JavaScript中使用sencha gridpanel 編輯單元格、改變單元格顏色_javascript技巧

JavaScript中使用sencha gridpanel 編輯單元格、改變單元格顏色_javascript技巧

WBOY
WBOY原創
2016-05-16 15:29:311198瀏覽

表格GridPanel概述

      ExtJS中的表格功能非常強大,包括了排序、快取、拖曳、隱藏某一列、自動顯示行號、列總計、儲存格編輯等實用功能。

  表格由類別Ext.grid.GridPanel定義,繼承自Panel,其xtype為grid。 ExtJS中,表格Grid必須包含列定義訊息,並指定表格的資料記憶體Store。表格的列資訊由類別Ext.grid.Column(以前是由Ext.grid.ColumnModel定義)、而表格的資料記憶體由Ext.data.Store定義,資料記憶體依據解析的資料不同分為JsonStore、SimpleStroe、GroupingStore等。

以下透過一段程式碼跟大家介紹sencha gridpanel 編輯單元,具體程式碼如下圖:

{
xtype: 'gridpanel',
region: 'north',
height: 150,
title: 'My Grid Panel',
store: 'A_Test_Store',
columns: [
{
xtype: 'gridcolumn',
dataIndex: 'Name',
text: 'Name',
editor: {
xtype: 'textfield'
}
},
{
xtype: 'gridcolumn',
dataIndex: 'Content',
text: 'Content'
},
{
xtype: 'gridcolumn',
dataIndex: 'Time',
text: 'Time'
}
],
plugins: [
Ext.create('Ext.grid.plugin.CellEditing', {
clicksToEdit: 1, //点击单元格编辑
listeners: {
beforeedit: {
fn: me.onCellEditingBeforeEdit,
scope: me
},
validateedit: {
fn: me.onCellEditingValidateedit,
scope: me
}
}
})
]
}
onCellEditingBeforeEdit: function(editor, e, eOpts) {//动态赋值用.正常情况下不需要该事件.
   e.record.data[e.field]= "my test";
e.value="my test";
e.record.commit(); //提交,不提交无效
}
onCellEditingValidateedit: function(editor, e, eOpts) {
if(e.row==1) //验证逻辑
{
e.cancel=true; //取消
e.record.data[e.field] = e.value;
}
e.record.commit();
}

下面一段程式碼是關於sencha gridpanel改變儲存格顏色

標題列包含 審核通過則綠色,包含拒絕為紅色:

{
xtype: 'gridcolumn',
renderer: function(value, metaData, record, rowIndex, colIndex, store, view) {
metaData.tdAttr = 'data-qtip="'+record.data.Content+'"';

if(record.data.Content.indexOf('审核通过')!=-1)
{
metaData.style="color:green";
}
else if(record.data.Content.indexOf('拒绝')!=-1)
{
metaData.style="color:red";
}
return value;
}
,
width: '*',
dataIndex: 'Title',
text: '标题'
}

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn