javascript - 在 react-bootstrap-table 的单元格编辑中,在下拉列表中我想要所选值的唯一索引
问题描述
我有一个正在使用的单元格编辑功能editable={ { type: 'select', options: { values} } }
。选择一个值后,我想要它的唯一索引值,因为选项具有共同的值。有什么办法吗?
这是表的代码:
<BsTable
errorMessage={ '' }
loading={ false }
options={ STATIC_TABLE_OPTIONS }
data={ this.state.updatedSteps }
rowsCount={ this.state.updatedSteps.length }
fetchInfo={ {
dataTotalSize: this.state.updatedSteps.length
} }
cellEdit={ cellEditProp }
headerContainerClass="my-header-class"
headerStyle={ {
fontSize: 'smaller'
} }
bordered={ false }
>
<Column dataField="idx" isKey hidden />
<Column dataField="selectedBusinessStep"
width="15%"
editable={ { type: 'select',
options: { values: arrBizStep }
} }
> </BsTable>
在 cellEditProp 中,我的代码是:
const cellEditProp = {
mode: 'click',
blurToSave: true,
afterSaveCell: ( row, cellName, cellValue ) => {
this.props.editCombinationHandler( row );
}
};
当我从位于以下位置的下拉菜单中更改值时:
<Column dataField="selectedBusinessStep"
width="15%"
editable={ { type: 'select',
options: { values: arrBizStep }
} }
我在上面提到的 cellEditProp 中的 afterSaveCell 函数中得到了行、cellName 和 cellValue。有什么方法可以获取下拉列表中所选项目的索引<Column dataField="selectedBusinessStep"
。如果您需要更多说明,请告诉我?
解决方案
这是您在 SELECT 下拉列表中获取所选项目索引的方法。
const cellEditProp = {
mode: 'click',
blurToSave: true,
afterSaveCell: handleChange
};
和handleChange
function handleChange(oldValue, newValue, row, column) {
let indexOfSelectedItem = -1;
column.editor.options.forEach((item, idx) => {
if (newValue === item.value) {
indexOfSelectedItem = idx;
}
});
if (indexOfSelectedItem > -1) {
console.log(indexOfSelectedItem);
}
// indexOfSelectedItem <<<<
}
我用 react-bootstrap-table-2 做了这个简单的演示,但同样的逻辑也可以用于以前的版本。希望这可以帮助您解决问题。
单击价格进行编辑并检查控制台输出: https ://codesandbox.io/s/react-bootstrap-table-next-with-editable-price-tgy0y
推荐阅读
- android - Android上不同的屏幕尺寸布局
- php - 使用 Instagram 基本显示 API 时出现“无效平台应用”错误
- android - 国家/地区定位如何在 Google Play 管理中心工作?
- javascript - Vuex - 更新突变中的对象
- c# - Android Xamarin - 带有 IP 地址的网络调用无法获取移动数据
- karate - 空手道加特林的 maxDuration 似乎无法正常工作
- python - 编写 CSV 时保留小数
- django - NameError:在新创建的文件中导入模型后未定义名称“模型”
- reactjs - React componentDidUpdate() 不会触发
- excel - Excel中如何将复选框的名称分配给单元格