【问题标题】:How can i sorting combobox items on React with Typescript?如何使用 Typescript 对 React 上的组合框项目进行排序?
【发布时间】:2020-11-16 22:01:39
【问题描述】:

我有一个项目,在项目中有一年的组合框。我将年份排序(asc to desc 或 desc to asc)。

我有一个表单 utils,我在组件中调用了 utils。

【问题讨论】:

  • 在您提供有关您的问题的足够信息之前,没有人可以帮助您。至少你可以包含你正在使用的代码。

标签: reactjs typescript react-native


【解决方案1】:

试试这个方法

import React, { Component } from "react";
import Select from "react-select";

function compare(a, b) {
  return a.label > b.label ? 1 : b.label > a.label ? -1 : 0;
}

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      options: [
        {
          label: 1940,
          value: 1
        },
        {
          label: 1945,
          value: 2
        },
        {
          label: 1942,
          value: 3
        },
        {
          label: 1941,
          value: 4
        },
        {
          label: 1946,
          value: 5
        }
      ]
    };
  }

  render() {
    return (
      <Select
        //isMulti
        hideSelectedOptions={false}
        options={this.state.options.sort(compare)}
      />
    );
  }
}

这是Example on Snack.io

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多