JQXGrid以自定义数据为表格下拉框的值

本文介绍了一种表格编辑器的配置方法,通过设置不同的列类型如下拉框和组合框等,实现对表格中特定字段的高效编辑。具体包括如何为这些控件分配数据源、如何处理空值输入等问题。
columns: [
{
text: 'Ship City', datafield: 'ShipCity', width: 150, columntype: 'combobox',
createeditor: function (row, column, editor) {
// assign a new data source to the combobox.
var list = ['Stuttgart', 'Rio de Janeiro', 'Strasbourg'];
editor.jqxComboBox({ source: list, promptText: "Please Choose:" });
},
// update the editor's value before saving it.
cellvaluechanging: function (row, column, columntype, oldvalue, newvalue) {
// return the old value, if the new value is empty.
if (newvalue == "") return oldvalue;
}
},
{
text: 'Ship Country', datafield: 'ShipCountry', width: 150, columntype: 'dropdownlist',
createeditor: function (row, column, editor) {
// assign a new data source to the dropdownlist.
var list = ['Germany', 'Brazil', 'France'];
editor.jqxDropDownList({ source: list });
},
// update the editor's value before saving it.
cellvaluechanging: function (row, column, columntype, oldvalue, newvalue) {
// return the old value, if the new value is empty.
if (newvalue == "") return oldvalue;
}
},
{ text: 'Ship Name', datafield: 'ShipName', columntype: 'combobox' }
]
在Qt中,你可以自定义QComboBox(下拉框)以呈现出类似表格的形式,以便用户可以选择行项目。这种操作通常通过组合QAbstractItemView、QTableWidget等控件以及信号槽机制来实现。以下是一个简单的步骤: 1. **创建QTableWidget**:首先,你需要创建一个QTableWidget,并设置列数和行数,添加需要显示的数据。 ```cpp QTableWidget *table = new QTableWidget; table->setRowCount(numRows); table->setColumnCount(numCols); for (int i = 0; i < numRows; ++i) { for (int j = 0; j < numCols; ++j) { table->setItem(i, j, new QTableWidgetItem(data[i][j])); } } ``` 2. **将QTableWidget连接到QComboBox**:使用QComboBox的`addItem()`方法添加表头,然后设置其样式,使其看起来像一个下拉列表。使用`setCurrentCell()`方法选择默认项。 ```cpp QComboBox *comboBox = new QComboBox; comboBox->addItems(table->horizontalHeaderLabels()); comboBox->setStyleSheet("QComboBox { padding-left: 25px; border: none; background-color: transparent; }"); comboBox->setCurrentCell(0, 0); // 设置默认选中的单元格 connect(comboBox, &QComboBox::currentIndexChanged, this, [table](int index) { int row = table->row(index); table->selectRow(row); }); ``` 3. **处理选择事件**:当用户在下拉框中选择一个选项时,会触发`currentIndexChanged`信号,这时可以更新对应的QTableWidget的内容。 当你想要从下拉框选择数据时,可以直接获取当前选中的行数据: ```cpp int selectedRow = comboBox->currentIndex(); QString selectedValue = table->item(selectedRow, 0)->text(); // 假设第一列是 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值