简体   繁体   English

将Kendo小部件值绑定到Kendo模板/可使用React观察到?

[英]Binding Kendo widget values to a Kendo template/observable using React?

I am messing around with Kendo UI React as I have a lot of dynamic component creation and being able to house them in react classes and instantiate instances as they are needed seems quite nice. 我正在搞混Kendo UI React,因为我有很多动态组件创建功能,并且能够将它们容纳在react类中并根据需要实例化实例似乎很不错。

However, in JQuery, I would do something like: <select id="multi-select" data-role="multiselect" data-bind="value: my-multiselect"></select> 但是,在JQuery中,我将执行以下操作: <select id="multi-select" data-role="multiselect" data-bind="value: my-multiselect"></select>

Then when the template that multiselect was in would be bound to an observable, any changes were reflected in that observable. 然后,当多重选择所在的模板将绑定到一个可观察对象时,任何更改都会反映在该可观察对象上。

However, I am not sure how this is done with the React widgets, since their syntax is slightly different. 但是,由于它们的语法略有不同,因此我不确定使用React小部件如何完成此操作。 I so far have a multiselect that is rendered like this: 到目前为止,我有一个multiselect呈现如下:

            render() {
                return (
                    <div>
                        <window.KendoDropdownsReactWrapper.MultiSelect 
                            id="user-filter"
                            change={this.onChange}
                            select={this.onSelect}
                            dataSource={this.dataSource}
                            placeholder={this.placeholder}
                            value={this.values}
                            dataTextField={this.dataTextField}
                            dataValueField={this.dataValueField}
                            template={this.template}
                            tagTemplate={this.tagTemplate}
                            filter={this.filter}
                            autoClose={this.autoClose} />
                    </div>
                );
            }

How would I set up binding so that this multiselect is bound to a value in an observable? 我将如何设置绑定,以便将此多选绑定到可观察对象中的值?

I believe that the case with React is not so straightforward as with JQuery and Angular. 我相信React的情况并不像JQuery和Angular那样简单。 The solution that works for me is to subscribe to the changes of the Observable and pass a new dataSource to the MultiSelect component each time. 对我有用的解决方案是订阅Observable的更改,并每次将新的dataSource传递给MultiSelect组件。

Here is an example: 这是一个例子:

class MultiSelectContainer extends React.Component {
    constructor(props) {
        super(props);
        this.state = { items: [] };
    }

    render() {
        var dataSource = new kendo.data.DataSource({ data: this.state.items });

        return (
            <div className="row">
                <div className="col-xs-12 col-sm-6 example-col">
                    <MultiSelect dataSource={dataSource} />
                </div>
            </div>
        );
    }

    componentDidMount() {
        var observable = Rx.Observable.create(function (observer) {
            observer.next("Item 1");
            observer.next("Item 2");
            observer.next("Item 3");
            observer.complete();
        });
        observable.subscribe(
            value => {
                this.setState(prevState => {
                    return { items: [...prevState.items, value] };
                });
            }
        );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM