code
Ext.require([
'Ext.grid.Panel',
'Ext.data.Store',
'Ext.dd.DropTarget',
'Ext.util.Format',
'Ext.view.DropZone'
]);
Ext.onReady(function() {
// 创建一个动态列的Store
var columnsStore = Ext.create('Ext.data.Store', {
fields: ['name', 'text'],
data: [{
name: 'col1',
text: '列1'
}, {
name: 'col2',
text: '列2'
}, {
name: 'col3',
text: '列3'
}, {
name: 'col4',
text: '列4'
}]
});
// 创建一个列配置项的Store
var columnConfigStore = Ext.create('Ext.data.Store', {
fields: ['name', 'text'],
data: [{
name: 'col1',
text: '列1'
}, {
name: 'col2',
text: '列2'
}, {
name: 'col3',
text: '列3'
}, {
name: 'col4',
text: '列4'
}]
});
// 创建一个空的数据Store
var dataStore = Ext.create('Ext.data.Store', {
fields: ['col1', 'col2', 'col3', 'col4'],
proxy: {
type: 'ajax',
url: 'data.json',
reader: {
type: 'json',
rootProperty: 'data'
}
},
autoLoad: true
});
// 创建一个包含列控件的面板
var columnPanel = Ext.create('Ext.grid.Panel', {
store: columnsStore,
title: '列控件',
columns: [{
text: '名称',
dataIndex: 'name',
flex: 1
}, {
text: '可见性',
xtype: 'checkcolumn',
dataIndex: 'visible'
}],
selModel: {
selType: 'checkboxmodel',
mode: 'SIMPLE'
},
dockedItems: [{
xtype: 'toolbar',
items: [{
text: '添加列',
handler: function() {
var selRecords = columnPanel.getSelectionModel().getSelection();
if (selRecords.length > 0) {
Ext.each(selRecords, function(record) {
// 添加到列配置项Store
columnConfigStore.add(record.copy());
// 更新数据Grid的列
updateDataGridColumns();
});
}
}
}, {
text: '移除列',
handler: function() {
var selRecords = columnConfigPanel.getSelectionModel().getSelection();
if (selRecords.length > 0) {
Ext.each(selRecords, function(record) {
// 从列配置项Store中移除
columnConfigStore.remove(record);
// 更新数据Grid的列
updateDataGridColumns();
});
}
}
}]
}]
});
// 创建一个包含列配置项的面板
var columnConfigPanel = Ext.create('Ext.grid.Panel', {
store: columnConfigStore,
title: '列配置项',
columns: [{
text: '名称',
dataIndex: 'name',
flex: 1
}, {
text: '可见性',
xtype: 'checkcolumn',
dataIndex: 'visible'
}],
selModel: {
selType: 'checkboxmodel',
mode: 'SIMPLE'
}
});
// 创建一个数据Grid
var dataGrid = Ext.create('Ext.grid.Panel', {
store: dataStore,
columns: [],
bbar: Ext.create('Ext.PagingToolbar', {
store: dataStore,
displayInfo: true,
displayMsg: '显示 {0} - {1} 共 {2}',
emptyMsg: "没有数据"
})
});
// 创建一个包含以上三个面板的窗口
var window = Ext.create('Ext.window.Window', {
title: '动态列分页示例',
width: 800,
height: 400,
layout: 'border',
items: [{
region: 'west',
width: 200,
split: true,
xtype: 'container',
layout: 'fit',
items: [columnPanel]
}, {
region: 'center',
xtype: 'container',
layout: 'fit',
items: [columnConfigPanel]
}, {
region: 'south',
height: 200,
split: true,
xtype: 'container',
layout: 'fit',
items: [dataGrid]
}]
});
window.show();
// 更新数据Grid的列
function updateDataGridColumns() {
var columns = [];
columnConfigStore.each(function(record) {
columns.push({
text: record.get('text'),
dataIndex: record.get('name'),
flex: 1
});
});
dataGrid.reconfigure([], columns);
}
});
在该示例中,我们创建了三个Store:columnsStore、columnConfigStore和dataStore。columnsStore存储了所有可供选择的动态列,columnConfigStore存储了用户选择的列配置项,dataStore存储了实际的数据。
我们创建了三个Grid面板:columnPanel、columnConfigPanel和dataGrid。columnPanel展示了所有可供选择的动态列,columnConfigPanel展示了用户选择的列配置项,dataGrid展示了实际的数据。columnPanel和columnConfigPanel都使用了checkboxmodel作为选择模式,而dataGrid则使用了分页工具栏。
在添加列和移除列的按钮点击事件中,我们对columnConfigStore进行操作,并调用updateDataGridColumns函数更新dataGrid的列展示。updateDataGridColumns函数遍历columnConfigStore,根据存储的列配置项创建列对象,并通过dataGrid的reconfigure方法重新配置dataGrid的列。
这样,在运行这段代码时,您将看到一个包含动态列分页功能的Ext.grid.Panel。用户可以从columnPanel中选择列,将其添加至columnConfigPanel配置列,并在dataGrid中展示数据。通过分页工具栏,用户可以在数据量较大时翻页查看数据。