HTML таблица жонглирует назад при проверке или выборе строки после сортировки столбца - PullRequest
0 голосов
/ 02 марта 2020

Сетка перед сортировкой выглядит следующим образом:

enter image description here

Теперь я собираюсь отсортировать ее по идентификатору пациента

enter image description here

Теперь, когда я проверяю идентификатор пациента: 936447, который находится в последней позиции после сортировки сетки, возвращается к исходному порядку.

enter image description here

Но я хочу отсортированную сетку даже после установки флажка / выбора строки. Это код для отображения сетки и сортировки сетки.

class App extends Component {
 constructor(props) {
        super(props);
        this.state = {
            order: {},
            orderby: '',
            printQueueList: props.printQueueList && props.printQueueList,
        };

        this.sort = this.sort.bind(this);
    }

    componentWillReceiveProps(nextProps) {
        if (this.state.printQueueList != nextProps.printQueueList)
            this.setState({ printQueueList: nextProps.printQueueList && nextProps.printQueueList });
    }
    sort(event) {
        const { order } = this.state;
        let { printQueueList } = this.props;
        var gridData = printQueueList;
        order[event.target.id] = !order[event.target.id];
        gridData = _.orderBy(gridData, (o) => typeof o[event.target.id] === 'string' ? o[event.target.id].trim().toLowerCase() : o[event.target.id], order[event.target.id] ? 'asc' : 'desc');
        this.setState({
            orderby: event.target.id,
            printQueueList: gridData,
            order
        });
    }
  render() {
    return (
      <div className="App">
        <table >
    <thead >
    <tr >
        <th id="select" >
    <Checkbox
        input={{
        name: 'selectAll',
        onChange:  //function 
        value: allSelected
        }}
        />
    <label htmlFor="SelectAll" >Select All</label>
        </th>
    <th id="PatientID"  onClick={this.sort}>Patient ID {order.PatientID ? <i id="PatientID" className="fa fa-sort-asc" /> : <i id="PatientID" className="fa fa-sort-desc" />}</th>
    <th id="DocType"  onClick={this.sort}>Type {order.DocType ? <i id="DocType" className="fa fa-sort-asc" /> : <i id="DocType" className="fa fa-sort-desc" />}</th>                                   
    </tr>
    </thead>
    <tbody >

    printQueueList && printQueueList.map((Queue, i) => {                                   
    return (
    <tr key={Queue.PrintQueueID} 
    onClick={() => onSelectPrintQueueGrid(Queue.PrintQueueID)}>
        <td >
    <Checkbox 
        input={{
        name: Queue.PrintQueueID,
        onChange:  //function
        value: selectedPrintQueueList.indexOf(Queue.PrintQueueID) !== -1,
        }}
    />
    </td>
    <td className="dashboard_table-cell" title={'Patient ID:' + Queue.PatientID}>{Queue.PatientID}</td>
    <td className="dashboard_table-cell" title={'Type:' + Queue.DocType}>{Queue.DocType}></td>
    </tr>)
    }
    )
    </tbody>
</table>
      </div>
    )
  }
}

const mapStateToProps = state => {
    return {
        printQueueList: state.myDashboardReducer.printQueueList,
    };
};

const mapDispatchToProps = dispatch => {
    return {

    };
};

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(App);

1 Ответ

0 голосов
/ 03 марта 2020

Поскольку componentWillReceiveProps(nextProps) обновляет состояние с помощью newProps, массив printQueueList обновляется. По этой причине перед обновлением с newProps я проверил, отсортирован ли массив или нет.

Если он не отсортирован, обновите printQueueList с newProps.

componentWillReceiveProps(nextProps) {
        if(_.isEmpty(this.state.order)){
        if (this.state.printQueueList != nextProps.printQueueList)
            this.setState({ printQueueList: nextProps.printQueueList && nextProps.printQueueList });
        }
    }  
...