2017-05-31 42 views
0

Im試圖找出如何從Griddle組件獲取當前過濾項目。ReactJS - Griddle v1.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; 
     }); 
    }); 
}); 

但我怎麼會使用filteredDataSelector我的反應成分?點擊按鈕導出爲Excel時,我想獲得過濾的項目。

我發現如何與各項目進行交互,例如(以呈現超鏈接,而不僅僅是純文本)在列表中,我認爲在similair方式應該工作到什麼我想要實現: https://github.com/GriddleGriddle/Griddle/issues/586

我真的不明白這是如何與redux和連接功能,但上面的例子工作得很好。

不知道我是否需要以類似的方式使用redux/connect來獲取篩選過的項目列表?

幹鍋的文檔: https://griddlegriddle.github.io/Griddle/docs/

回答

0

找到一種方法來解決這個問題,即使Im相當肯定有一個更好的解決方案,那麼請讓我知道。

首先,我重寫了TableBody Griddle組件,並做了一個小的修復,使其在Internet Explorer 11中正常工作,即11(list._tail.array)。我將當前過濾的數據行索引存儲在this.rowId中。

   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/,然後使用的rowid領域獲得所選擇的數據。

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