0

グリドルコンポーネントから現在のフィルタリングされたアイテムを取得する方法を見つけようとしています。

ソースコードで次の関数を見つけました。

var filteredDataSelector = exports.filteredDataSelector = (0, _reselect.createSelector)(dataSelector, filterSelector, function (data, filter){
        return data.filter(function (row) {
        return Object.keys(row.toJSON()).some(function (key) {
        return row.get(key) && 
        row.get(key).toString().toLowerCase().indexOf(filter.toLowerCase()) > -1;
      });
   });
});

しかし、react-component でfilteredDataSelector を使用するにはどうすればよいでしょうか? ボタンをクリックしてExcelにエクスポートすると、フィルタリングされたアイテムを取得したいと思います。

リスト内の各アイテムを操作する方法(プレーンテキストではなくハイパーリンクをレンダリングする方法)について、次の例を見つけました 。グリドル/イシュー/586

これが redux と接続機能でどのように機能するかはよくわかりませんが、上記の例はうまく機能しました。

フィルタリングされたアイテムのリストを取得するために、同様の方法で redux/connect を使用する必要があるかどうかわかりませんか?

グリドルのドキュメント: https://griddlegriddle.github.io/Griddle/docs/

4

2 に答える 2

0

これを解決する方法を見つけましたが、より良い解決策があると確信していますが、教えてください。

まず、TableBody の griddle-component をオーバーライドし、Internet Explorer 11 で正しく動作するように小さな修正を加えました ( list._tail.array )。現在フィルタリングされているデータ行インデックスをthis.rowIdsに保存します。

                TableBody: (_ref) => {
                    var rowIds = _ref.rowIds,
                        Row = _ref.Row,
                        style = _ref.style,
                        className = _ref.className;

                    this.rowIds = rowIds._tail.array;

                    var list = rowIds && rowIds.map(function (r) {
                        return React.createElement(Row, { key: r, griddleKey: r });
                    });

                    //ie11 felsökningsfix:
                    //console.log(list);
                    //console.log(list._tail.array);

                    return React.createElement(
                        'tbody',
                        { style: style, className: className }, list._tail.array //fix för ie11! orginalkoden skickade bara in list, men krachade då i ie11.
                    );
                }

この記事で説明されているようにrowDataSelectorを使用します: https : //griddlegriddle.github.io/Griddle/examples/getDataFromRowIntoCell/、次にrowIdsフィールドを使用して、最初の行インデックスでのみ選択されたデータを取得します。

    this.rowDataSelector = (state, { griddleKey }) => {
        if (griddleKey === this.rowIds[0]) {
            this.selectedData = this.rowIds.map(function (id) {
                return state.get('data')
                    .find(rowMap => rowMap.get('griddleKey') === id)
                    .toJSON();
            });
        } 
        return state
            .get('data')
            .find(rowMap => rowMap.get('griddleKey') === griddleKey)
            .toJSON();
    }

それを使用して:

exportToExcel() {
if (this.selectedData.length != 0) {
    var results = this.selectedData;
    Api.exportToExcelPre('/product/exporttoexcelpre/', results).then(result => {
        window.location = Constants.API_URL + '/product/exporttoexcel/';
    });
}
}

最終的なコード:

import { connect } from 'react-redux';

export default class ProductList extends Component {
constructor(props) {
    super(props);
    this.state = {
        data: [],
        loading: true
    };
}

updateProductList() {
    Api.getProducts().then(products => {
        this.setState({ data: products, loading: false });
    });
}

componentDidMount() {
    this.updateProductList();
}

componentWillReceiveProps(props) {
    if (props.refreshProductList) {
        this.updateProductList();
    }
}

render() {
    if (this.state.loading) {
        return (
            <div>Läser in produkter...</div>
        );
    }
    return (
        <div>
            <ProductResultList products={this.state.data} />
        </div>
    );
}
}    
class Filter extends Component {
constructor(props) {
    super(props);
}

onChange(e) {
    this.props.setFilter(e.target.value);
}

render() {
    var imgExcelLogo = <img src={PathHelper.getCurrentPathAppend("img/excel_logo.png")} className="export-symbol" onClick={this.props.export} style={{ float: "right" }} />;
    return (
        <div>
            <div className="row">
                <div className="form-group col-md-6 label-floating is-empty">
                    <label class="control-label" htmlFor="search-product">Sök på valfritt fält, exempelvis produktnamn/produktkategori osv.</label>
                    <input className="form-control" type="text" name="search-product" id="search-product" onChange={this.onChange.bind(this)} />
                </div>
                <div className="col-md-6 form-group" style={{ minHeight: "35px" }}>
                    {imgExcelLogo}
                </div>
            </div>
        </div>
    );
}
}    

export class ProductResultList extends Component {
constructor(props) {
    super(props);
    this.rowIds = [];
    this.selectedData = [];
    this.styleConfig = {
        classNames: {
            Row: 'row-class',
            Cell: "col-md-2",
            TableHeadingCell: "col-md-2 cursor",
            Table: "table table-striped table-hover",
            Filter: "form-control"
        },
    }
    this.sortMethod = {
        data: this.props.products,
        column: "generatedId"
    }
    this.settings = {
        // The height of the table
        tableHeight: 100,
        // The width of the table
        tableWidth: null,
        // The minimum row height
        rowHeight: 10,
        // The minimum column width
        defaultColumnWidth: null,
        // Whether or not the header should be fixed
        fixedHeader: false,
        // Disable pointer events while scrolling to improve performance
        disablePointerEvents: false
    };
    this.pageProps = {
        currentPage: 1,
        pageSize: 1000
    }

    this.rowDataSelector = (state, { griddleKey }) => {
        if (griddleKey === 0) {
            this.selectedData = this.rowIds.map(function (id) {
                return state.get('data')
                    .find(rowMap => rowMap.get('griddleKey') === id)
                    .toJSON();
            });
        } 
        return state
            .get('data')
            .find(rowMap => rowMap.get('griddleKey') === griddleKey)
            .toJSON();
    }
    this.exportToExcel = this.exportToExcel.bind(this);
    this.enhancedWithRowData = connect((state, props) => {
        return {
            // rowData will be available into MyCustomComponent
            rowData: this.rowDataSelector(state, props)
        };
    });
}

exportToExcel() {
    if (this.selectedData.length != 0) {
        var results = this.selectedData;
        Api.exportToExcelPre('/product/exporttoexcelpre/', results).then(result => {
            window.location = Constants.API_URL + '/product/exporttoexcel/';
        });
    }
}

LinkComponent({ value, griddleKey, rowData }) {
    return (
        <Link to={PathHelper.getCurrentPath() + "product/edit/" + rowData.id}>{rowData.name}</Link>
    );
}

render() {
    return (
        <Griddle
            ref='Griddle'
            styleConfig={this.styleConfig}
            data={this.props.products}
            pageProperties={this.pageProps}
            sortProperties={this.sortProperties}
            components={{
                Layout: ({ Table, Filter }) => <div><Filter /><div className="table-responsive"><Table /></div></div>,
                Settings: () => <span />,
                SettingsContainer: () => <span />,
                SettingsToggle: () => <span />,
                PageDropdown: () => <span />,
                NextButton: () => <span />,
                Filter: (filter) => {
                    return <Filter products={this.props.products} export={this.exportToExcel} setFilter={filter.setFilter} />
                },
                TableBody: (_ref) => {
                    var rowIds = _ref.rowIds,
                        Row = _ref.Row,
                        style = _ref.style,
                        className = _ref.className;

                    this.rowIds = rowIds._tail.array;

                    var list = rowIds && rowIds.map(function (r) {
                        return React.createElement(Row, { key: r, griddleKey: r });
                    });

                    //ie11 felsökningsfix:
                    //console.log(list);
                    //console.log(list._tail.array);

                    return React.createElement(
                        'tbody',
                        { style: style, className: className }, list._tail.array //fix för ie11! orginalkoden skickade bara in list, men krachade då i ie11.
                    );
                }
            }}
            plugins={[plugins.LocalPlugin]}>
            <RowDefinition>
                <ColumnDefinition id="generatedId" title="Produkt-Id" />
                <ColumnDefinition id="name" title="Namn" customComponent={this.enhancedWithRowData(this.LinkComponent)} />
                <ColumnDefinition id="productCategoryName" title="Produktkategori" />
                <ColumnDefinition id="productTypeName" title="Produkttyp" />
                <ColumnDefinition id="supplierName" title="Leverantör" />
                <ColumnDefinition id="toBeInspectedString" title="Besiktigas" />
            </RowDefinition>
        </Griddle>
    );
};
}
于 2017-06-05T12:46:56.930 に答える