【问题标题】:admin on rest: inject format change to filter input管理员休息:注入格式更改以过滤输入
【发布时间】:2017-09-29 15:33:09
【问题描述】:

我有几天一直在解决的问题,我的管理员正在处理休息实施。我们有一个要求,让我们的过滤器输入表现得更像“包含”而不是“相等”

我的 API 支持这样的查找,在 GET 上,通过在您的输入中传递 % 字符(例如 /app?foo=%25bar%25 返回 foo 类似于 %bar% 的应用)

虽然在理想情况下,我可以告诉我的用户在过滤器的 TextInput 字段中提供 %s,但大多数人不会因为插入特殊字符而烦恼。

那么,我的问题是,我可以在哪里注入一些东西来将通配符包裹在我的对象中的对象中输入的输入周围?

我已尝试重新实现 TextInput 并在 OnChange、渲染等上更新 input.value,但 restClient 仍在使用预先更改的输入值。

这是我对自定义 inputComponent 的破解

class ContainsTextProps {
    public addField?: PropTypes.bool.isRequired = true;
    public elStyle?: PropTypes.object;
    public input?: PropTypes.object;
    public isRequired?: PropTypes.bool;
    public label?: PropTypes.string;
    public meta?: PropTypes.object;
    public name?: PropTypes.string;
    public options?: PropTypes.object = {};
    public resource?: PropTypes.string;
    public source?: PropTypes.string;
    public type?: PropTypes.string = 'text';
    public onBlur?: PropTypes.func = () => {};
    public onChange?: PropTypes.func = () => {};
    public onFocus?: PropTypes.func = () => {};
}
class ContainsTextInputInternal extends React.Component<ContainsTextProps, {}> {
    constructor(props) {
        super(props);
        this.handleBlur = this.handleBlur.bind(this);
        this.handleFocus = this.handleFocus.bind(this);
        this.handleChange = this.handleChange.bind(this);
    }

    public handleBlur = (eventOrValue) => {
        // this.props.onBlur(eventOrValue);
        this.props.input.onBlur(eventOrValue);
    }

    public handleFocus = (event) => {
        // this.props.onFocus(event);
        this.props.input.onFocus(event);
    }

    public handleChange = (eventOrValue, newvalue) => {
        // this.props.onChange(eventOrValue);
        this.props.input.onChange(eventOrValue, newvalue);
    }


    public render() {
        const {
            elStyle,
            input,
            label,
            meta: { touched, error },
            options,
            type,
        } = this.props;

        if (input && input.value && input.value.length > 0 && input.value.indexOf('%') < 0) {
            input.value = `%${input.value}%`;
        }

        return (
            <TextField
                {...input}
                onBlur={this.handleBlur}
                onFocus={this.handleFocus}
                onChange={this.handleChange}
                type={type}
                // floatingLabelText={<FieldTitle label={label} source={source} resource={resource} isRequired={isRequired} />}
                floatingLabelText={label}
                errorText={touched && error}
                style={elStyle}
                value={input.value}
                {...options}
            />
        );
    }
}

export const ContainsTextInput = pure(ContainsTextInputInternal);

【问题讨论】:

  • 您可以发布您的自定义TextInput 吗?
  • 欢迎来到 Stack Overflow!请花一些时间阅读帮助页面,尤其是Stack Overflow question checklist 部分。您可能还想了解Minimal, Complete, and Verifiable Examples
  • 用一个例子更新了问题
  • 并且,为了真正解决我的问题,我不想让我的自定义组件做我需要的事情,我想要一些关于我应该如何尝试完成我想要做的事情的方向做。

标签: admin-on-rest react-admin


【解决方案1】:

您需要的是一个自定义 REST 客户端,它可以检测查询并将特殊字符注入输入。

https://marmelab.com/admin-on-rest/RestClients.html#writing-your-own-rest-client

这应该可以帮助您入门。如果您只有一种特殊情况,您只需要注入特殊字符,那么您也可以使用 REST 包装器。

https://marmelab.com/admin-on-rest/RestClients.html#decorating-your-rest-client-example-of-file-upload

【讨论】:

  • 谢谢,我试图避免修改我的 REST 查询,但我想我已经决定采用这种方法。我将向所有需要以这种方式运行的 List 对象添加 filter = {defaultToContains=true} 的道具,这样至少我仍然能够保持当前的基本行为。感谢大家的帮助!
  • 您的回答使我朝着正确的方向前进。我刚刚在我的列表组件中添加了一个永久过滤器filter={{ __contains: true }}。在我的 REST 客户端中,我检测到 __contains 并用星号 * 为每个值添加前缀和后缀(如果它们还没有的话)。您可以通过将字段名称数组而不是 true 传递给 __contains 来进一步采用此解决方案,并让 REST 客户端仅将星号应用于这些字段。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-16
  • 1970-01-01
  • 2014-05-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-24
相关资源
最近更新 更多