reactjs - 在按钮上的 onClick 事件之后将下拉值设置回占位符或第一个选项?
问题描述
在下面的代码中,我有一个下拉菜单和一个按钮。在下拉列表中选择一个选项并单击按钮后,该值将发送到活动单元格中的 Excel。将此数据发送到 Excel 后,我可以将下拉菜单设置回占位符吗?或者我可以将下拉菜单设置为第一个值(在这种情况下为空白)?如何将下拉值设置回占位符或空白?
import * as React from "react";
import { Dropdown, DropdownMenuItemType, IDropdownOption } from 'office-ui-fabric-react/lib/Dropdown';
import { PrimaryButton } from 'office-ui-fabric-react/lib/';
export interface ParentProps {
};
export interface ParentState {
selectedItem?: { key: string | number | undefined };
operationType?;
};
export default class ParentComponent extends React.Component<ParentProps, ParentState> {
constructor(props, context) {
super(props, context);
this.state = {
operationType: '',
};
}
addToExcel = async () => {
try {
await Excel.run(async context => {
const range = context.workbook.getSelectedRange();
range.load("address");
await context.sync();
range.values = (this.state.operationType);
});
} catch (error) {
console.error(error);
}
this.setState({
})
};
render() {
const { selectedItem } = this.state;
const options: IDropdownOption[] = [
{ key: 'blank', text: '' },
{ key: 'topLevelMake', text: 'Parents', itemType: DropdownMenuItemType.Header },
{ key: 'topLevel', text: 'TOP LEVEL' },
{ key: 'make', text: 'MAKE ITEM' },
];
return (
<div>
<Dropdown
label="Operation"
selectedKey={selectedItem ? selectedItem.key : undefined}
onChange={this._onChange}
placeholder={"Select an option"}
options={options}
styles={{ dropdown: { width: 300 } }}
/>
<p></p>
<PrimaryButton
text="Enter"
onClick={this.addToExcel}
/>
</div>
);
}
private _onChange = (e, item: IDropdownOption): void => {
this.setState({ selectedItem: item });
this.setState({ operationType: item.text })
console.log(e);
}
};
解决方案
在您的 : 上尝试这样的事情addToExcel()
:
addToExcel = async () => {
try {
await Excel.run(async context => {
const range = context.workbook.getSelectedRange();
range.load("address");
await context.sync();
range.values = (this.state.operationType);
});
} catch (error) {
console.error(error);
}
this.setState({
selectedItem: {key:'blank'},
})
};
推荐阅读
- javascript - 为什么我能够使用'let'在for循环内再次初始化和分配变量?
- html - 制作其兄弟列高度的弹性列
- android - 是否可以在 Flutter 应用程序中使用 C++ 进行网络编程?
- php - 我怎样才能在 PHP 中正确地获得这个特定部门的其余部分?
- svelte - zeit/now 上的 svelte/sapper 直接链接问题 - 如何调试或修复?
- r - R - 为反向地理编码存储循环输出(google api)
- javascript - Auth0 未找到身份验证令牌
- python - 如何在我的代码中制作一个按钮来停止计时器?
- javascript - JavaScript - 将带有类的 div 旋转 x 度
- javascript - 刷新值反应不是动作后的值