gpt4 book ai didi

java - 将值设置为 ComboBoxItem smartgwt

转载 作者:行者123 更新时间:2023-12-01 09:45:30 25 4
gpt4 key购买 nike

在下拉菜单中我得到这样的结果。找到附件图片。实际上,在“名称”字段中,“名称”和“描述”都显示为逗号(,)分隔。

final ComboBoxItem comboBoxItem = new ComboBoxItem("attributeTypeId","Attr. Type");
ListGridField nameField = new ListGridField("name", "Name");
ListGridField descField = new ListGridField("description","Description");
descField.setShowHover(true);
comboBoxItem.setPickListFields(nameField, descField);
comboBoxItem.setPickListWidth(200);
comboBoxItem.setFilterLocally(true);
comboBoxItem.setColSpan(2);
comboBoxItem.setAddUnknownValues(false);
comboBoxItem.setValueField(FieldNames.ID_FIELD);
comboBoxItem.setDisplayField(FieldNames.NAME_FIELD);
comboBoxItem.setAutoFetchData(true);

OptionListDataSource attrTypeds = OptionListDataSource.getInstance(FieldNames.ATTRIBUTE_TYPE_FIELD);
attrTypeds.fetchData(null, new DSCallback() {
@Override
public void execute(final DSResponse response, final Object rawData, final DSRequest request) {

Record[] recList = response.getData();
LinkedHashMap<String, String[]> dataLinkMap = new inkedHashMap<String,String[]>(); //LinkedHashMap<String,

dataLinkMap.put("0", new String[]{"Select",""});
for (Record record : recList) {

String attrId = record.getAttribute(FieldNames.ID_FIELD);
String attrName = record.getAttribute(FieldNames.NAME_FIELD);
String attrDesc = record.getAttribute(FieldNames.DESCRIPTION_FIELD);

dataLinkMap.put(attrId, new String[]{attrName,attrDesc});
}
comboBoxItem.setValueMap(dataLinkMap);
}
});

Screen Shot

最佳答案

这里是一些示例代码,可以实现我理解您想要实现的目标:

public class TestCases implements EntryPoint {  

public void onModuleLoad() {
DataSource logDS = DataSource.get("yourDSName");

final DynamicForm form = new DynamicForm();
form.setWidth(550);
form.setNumCols(2);

ListGridField nameField = new ListGridField(FieldNames.NAME_FIELD);
ListGridField descriptionField = new ListGridField(FieldNames.NAME_DESCRIPTION);

LinkedHashMap<String,String> hashMap = new LinkedHashMap<String,String>();
hashMap.put("-1", "Select");

ComboBoxItem myItem = new ComboBoxItem();
myItem.setTitle("ComboBox");
myItem.setOptionDataSource(logDS);
myItem.setDisplayField("category");
myItem.setValueField(FieldNames.ID_FIELD);
myItem.setSpecialValues(hashMap);
myItem.setPickListWidth(300);
myItem.setPickListFields(nameField, descriptionField);

form.setItems(myItem);
form.draw();
}
}

注意:

  • 为了显示各种字段,您需要使用 setPickListFields 以及对这些字段的引用。
  • 您不需要对 DataSource 本身调用 fetch()。当您使用 DataBound 组件(例如 ComoBoxItem)时,这会自动为您完成。
  • 您可以使用 setSpecialValues() 添加额外的值,而无需修改您的 DSResponse 数据(这就是为什么您不需要使用直接fetch())。

编辑

您遇到的问题是 ValueMap,它只是一个 Map(换句话说,只是一组键/值对),您是提供给 ComboBoxItemDataSource 直接提供的 Record[] 对象不同,它本质上只是一个由几个Map,每个代表一个字段名称及其值。这样,除了值字段之外,您还可以向 ComboBoxItem 提供多个字段以用于显示目的,例如特定情况下的“名称”和“说明”。

从 API 来看,我认为您无法手动向 ComboBoxItem 提供 Record[],因此您要么通过 DMI 获取数据(这对我来说是最简单的)或其他方法,允许您使用数据绑定(bind)功能自动修改服务器所需的响应并将其返回到 ComboBoxItem,或者您坚持只显示“值”(这就是您要显示的值)现在就可以了,但当然你可以更好地格式化数据)。

我对格式化的意思是,如果您选择使用 setValueMap() 的原始方法,则需要提供一个 Map,其中Map 只是 ComboBoxItem 上的一个值及其各自的显示“文本”,它可以是组合其他几个字段的值的任何字符串,并使用字符串连接根据需要进行格式化(例如,您可以使它

nameField + ": " + descriptionField

但这种方法已经是最好的了。

现在,通过 DMI,您需要定义服务器类,该类将在数据源描述符(ds.xml 文件)中提供格式正确的数据:

<operationBindings>  
<operationBinding operationType="fetch" serverMethod="fetchComboBoxData">
<serverObject lookupStyle="new" className="com.myApp.ComboBoxDMI"/>
</operationBinding>
</operationBindings>

然后创建类和方法来提供您需要的内容:

public class ComboBoxDMI {

public DSResponse fetchComboBoxData(DSRequest dsRequest) throws Exception {
DSResponse response = dsRequest.execute();
if (response.statusIsSuccess()) {
@SuppressWarnings("unchecked")
List<Map<String, Object>> recList = response.getRecords();
List<Map<String, Object>> comboBoxList = new ArrayList<Map<String,Object>>();
// Add here the new record... for each field in your DataSource, you need to set a Map
// with the key being the field name and the value being the field value. So you need
// 1 Map entry per field. All your Map entries form 1 record, and that's what you add
// to your List of Maps
return constructDSResponse(comboBoxList);
}
return response;
}

private DSResponse constructDSResponse(List<Map<String, Object>> comboBoxList) {
DSResponse response = new DSResponse();
int totalRows = comboBoxList.size();
response.setStartRow(totalRows > 0 ? 1 : 0);
response.setEndRow(totalRows);
response.setTotalRows(totalRows);
response.setData(comboBoxList);
return response;
}
}

最后,您可以遵循我在原始答案中建议的原始方法,但现在您不需要使用 setSpecialValues API,您的版本不支持该 API。

关于java - 将值设置为 ComboBoxItem smartgwt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38083700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com