【问题标题】:react-awesome-query-builder empty select when click outside select当点击外部选择时,react-awesome-query-builder 空选择
【发布时间】:2021-11-28 10:38:00
【问题描述】:

我使用 asyncFetch 并从 SelectWiget 中选择值,在外部进行任何点击后,将此选择设为空值。

我对字段使用下一个配置

 products: {
      type: '!struct',
      label: 'Products',
      subfields: {
        title: {
          type: 'select',
          label: 'Name',
          fieldSettings: {
            asyncFetch: async (search, offset) => {
              const toSearch = _.isEmpty(search) ? 'null' :  search;
              const prodApi = (await axios.get(`http://localhost:8002/api/products/1/${toSearch}?offset=${offset}`, { headers: authHeader() }));
              const productsValues = prodApi.data.data.map(
                product => ({
                  title: product.title,
                  value: product.id
                })
              )

              return {
                values: productsValues,
                hasMore: true,
              }
            },
            useAsyncSearch: true,
            useLoadMore: true,
            forceAsyncSearch: false,
            allowCustomValues: false
          }
        },

另一个错误是当我选择某个值时再次调用 asyncFetch。

这是该软件包中的错误还是我缺少某些配置?

我使用的包是 react-awesome-query-builder

【问题讨论】:

    标签: reactjs query-builder


    【解决方案1】:

    函数异步结果的值需要是字符串,所以函数是这样的:

    asyncFetch: async (search, offset) => {
              const toSearch = _.isEmpty(search) ? 'null' :  search;
              const prodApi = (await axios.get(`http://localhost:8002/api/products/1/${toSearch}?offset=${offset}`, { headers: authHeader() }));
              const productsValues = prodApi.data.data.map(
                product => ({
                  title: product.title,
                  value: product.id.toString(),
                })
              )
    
              return {
                values: productsValues,
                hasMore: true,
              }
            }
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    相关资源
    最近更新 更多