在按钮上的onClick事件之后将下拉值设置回占位符或第一个选项?

问题描述 投票:0回答:2

在下面的代码中,我有一个下拉菜单和一个按钮。在下拉菜单中选择一个选项并单击按钮后,该值将发送到活动单元格中的Excel。将此数据发送到Excel之后,可以将Dropdown设置回占位符吗?还是可以将“下拉列表”设置为第一个值(在这种情况下为空白)?如何将Dropdown值设置回占位符或空白?

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);
    }
  };
reactjs typescript drop-down-menu office-js office-js-helpers
2个回答
1
投票

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'},
    })
  };


0
投票

您应该在执行Excel操作后更新状态。

addToExcel = async () => {
    try {
      await Excel.run(async context => {
        const range = context.workbook.getSelectedRange();
        range.load("address");
        await context.sync();
        range.values = (this.state.operationType); 
      });
      // update state after asyn operations is done
        this.setState({
            selectedItem:undefined
        })
    } catch (error) {
      console.error(error);
    }

  };
© www.soinside.com 2019 - 2024. All rights reserved.