Grid中使用的Combobox

22 篇文章 0 订阅
Ext的Grid中使用combobox做为编辑控件时,遇到在combobox中选择了相应的选项时(displayField),在grid中却填入了valueField值的问题,困扰了很久,在Extjs官方网站的Forums中看到了一篇关于此问题的article,问题迎刃而解,在此感谢原文作者durlabh。全文的扼要翻译如下

原文位置:[url]http://extjs.com/forum/showthread.php?t=34691[/url]

[size=medium][b]ComboBox for Grid (combo having local store)[/b][/size]

Many people have been having confusion over how to handle rendering of combo boxes having a separate valueField and displayField. This is how we have been using in our applications:
很多人困惑于怎样处理带有valueField和displayField的ComboBoxes渲染问题。这是我们如何在应用中使用ComboBoxes的:

Ext.ns("Ext.ux.renderer");

Ext.ux.renderer.ComboRenderer = function(options) {
var value = options.value;
var combo = options.combo;

var returnValue = value;
var valueField = combo.valueField;

var idx = combo.store.findBy(function(record) {
if(record.get(valueField) == value) {
returnValue = record.get(combo.displayField);
return true;
}
});

// This is our application specific and might need to be removed for your apps
// 这是我们应用中所特定的可能需要从你的应用中移除
if(idx < 0 && value == 0) {
returnValue = '';
}

return returnValue;
};

Ext.ux.renderer.Combo = function(combo) {
return function(value, meta, record) {
return Ext.ux.renderer.ComboRenderer({value: value, meta: meta, record: record, combo: combo});
};
}


Then, in grid config you can use the same renderer for combo:
然后,在你的grid配置属性中对于combo可以使用同样的renderer:


// Your combo definition
// 你的combo定义
var addressTypeCombo = Ext.form.ComboBox();

var cm = new Ext.grid.ColumnModel([
{header: 'Type', dataIndex: 'AddressTypeId', width: 100, renderer: Ext.ux.renderer.Combo(addressTypeCombo), editor: addressTypeCombo},
{header: 'Address', dataIndex: 'Address1', width: 130, editor: new Ext.form.TextField({maxLength:50})},
{header: 'Address 2', dataIndex: 'Address2', width: 110, editor: new Ext.form.TextField({maxLength:50})},
{header: 'Address 3', dataIndex: 'Address3', width: 110, editor: new Ext.form.TextField({maxLength:50})},
{header: 'Zip Code', dataIndex: 'PostalCode', editor: new Ext.form.TextField({maxLength:10}), width: 80},
{header: 'City', dataIndex: 'City', width: 80, editor: new Ext.form.TextField({maxLength:20})}]
])


Please note that the above combo box assumes that the store for combo will be local. This approach won't work with remote combos. For remote combo, we update a "proxy" field in the grid store that is used for rendering.
请注意关于combo box假设其store为本地的。此文对于远程combo不适用。远程combo,我们为grid的store中所使用的渲染器替换为"proxy"域。

Let me know your suggestions/ improvements.
请给出你的建议或与我们分享你的改进。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 WPF ComboBox 示例程序,其 ComboBox 显示一组颜色名称,用户可以从选择一个颜色: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Color Selector" Height="150" Width="300"> <Grid> <ComboBox x:Name="colorComboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="10" SelectionChanged="colorComboBox_SelectionChanged"> <ComboBoxItem>Red</ComboBoxItem> <ComboBoxItem>Green</ComboBoxItem> <ComboBoxItem>Blue</ComboBoxItem> <ComboBoxItem>Yellow</ComboBoxItem> <ComboBoxItem>Orange</ComboBoxItem> </ComboBox> <TextBlock x:Name="colorTextBlock" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="140,10,0,0" Text="Selected color: " /> </Grid> </Window> ``` 在这个示例程序,我们创建了一个名为 "colorComboBox" 的 ComboBox 控件,并向其添加了五个 ComboBoxItem,它们分别代表五个颜色名称。我们还创建了一个名为 "colorTextBlock" 的 TextBlock 控件,用于显示用户所选择的颜色。 在 C# 代码,我们处理了 ComboBox 的 SelectionChanged 事件,以便在用户选择一个颜色时更新 "colorTextBlock" 的文本内容: ```csharp private void colorComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ComboBoxItem selectedItem = colorComboBox.SelectedItem as ComboBoxItem; if (selectedItem != null) { colorTextBlock.Text = "Selected color: " + selectedItem.Content.ToString(); } } ``` 在这个事件处理程序,我们首先将 ComboBox 的 SelectedItem 属性转换为 ComboBoxItem 类型,然后将其文本内容添加到 "colorTextBlock"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值