首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在typescript中调用react-bootstrap-table元素的方法?

如何在typescript中调用react-bootstrap-table元素的方法?
EN

Stack Overflow用户
提问于 2017-08-19 05:15:32
回答 3查看 2.2K关注 0票数 1

我认为我遇到的问题是将"any“bootstrapTableRef转换到react-bootstrap表,设置ref,或者导入错误。我不知道如何启用对导入的表的方法的访问。我看到了适用于HTMLInputElement的this,但无法使其适用于bootstrapTable类型。具体来说,this是我想要调用的方法:

代码语言:javascript
复制
this.refs.table.cleanSelected();  // this.refs.table is a ref for BootstrapTable  

在ResultsTables.tsx中,我以这种方式引用

代码语言:javascript
复制
import Select from "react-select";
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table'; //
import "../../../node_modules/react-bootstrap-table/css/react-bootstrap-table.css";
import { Link } from "react-router";
import * as ReactDOM from 'react-dom';

然后在下面

代码语言:javascript
复制
export class ResultsTable extends React.Component<IResultsTableProps, 
    IResultsTableState>{
    bootstrapTableRef: any;

...

public render() {
           ...

            return (
                 <BootstrapTable data={this.props.lots} keyField="lotNumber" striped
                        condensed={true} id="searchResultsTable" selectRow={selectRow} hover ref={(i) => this.bootstrapTableRef = i} ...>
                   ...
                 </BootstrapTable> );

我希望能够做到这一点,但得到一个错误,即该方法在ReactInstance/Element类型上不存在。我尝试了不同的类型转换方法,但也不能让转换类型被识别。

代码语言:javascript
复制
clear() {
   this.refs.bootstrapTableRef.cleanSelected();
}

我也尝试过这两种方式,但都没有成功:

代码语言:javascript
复制
clearSelectedRow() {

    var table = ReactDOM.findDOMNode<BootstrapTable>(this.refs.bootstrapTableRef);
    table.cleanSelected();

    var tableRef2 = <BootstrapTable>document.getElementById("searchResultsTable");
    tableRef2.cleanSelected();

}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-08-23 22:47:38

主要问题是DOM对象和TSX类之间的上下文不同,因此在React-Bootstrap-Table中没有调用该方法。所以关键部分是顶部的"this“绑定。

代码语言:javascript
复制
export class SearchResultsTable2 extends React.PureComponent<foo,bar>{
   bootstrapTableRef: any;
   constructor(props) {
      super(props);
      this.onBootstrapTableRef = this.onBootstrapTableRef.bind(this);

    clearSelectedRow() {
       this.bootstrapTableRef.cleanSelected();
    }

    onBootstrapTableRef(instance) {
       this.bootstrapTableRef = instance;
    }

    componentDidUpdate() {
        this.clearSelectedRow();
    }

    public render() {

       ...
        return (
                <BootstrapTable data={this.props.lots} keyField="lotNumber" striped
                    condensed={true} id="searchResultsTable" selectRow={selectRow} hover ref={this.onBootstrapTableRef} options={{ noDataText: 'No lots to display.' }}>....</BootstrapTable>);
票数 1
EN

Stack Overflow用户

发布于 2017-08-23 10:28:56

尝试:

代码语言:javascript
复制
<BootstrapTable ref="bootstrapTable" />
const table: any = this.refs.bootstrapTable;
table.cleanSelected();
票数 0
EN

Stack Overflow用户

发布于 2020-08-20 16:53:11

我以azulBonnet的身份实现了它,但更改了对引用的访问权限:

代码语言:javascript
复制
this.bootstrapTableRef.selectionContext.selected = [];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45765150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档