简体   繁体   English

将jQuery Plugin Pagination.js与React结合使用

[英]Use jQuery Plugin Pagination.js with React

I want to use a pagination plugin in React. 我想在React中使用分页插件。 The original use in jQuery: 最初在jQuery中使用:

$('#demo').pagination({
    dataSource: [1, 2, 3, 4, 5, 6, 7, 8],
    pageSize: 3,
    callback: function (data, pagination) {
        // template method of yourself
        var html = template(data);
        $("#dataContainer").html(html);
    }
});

I want to use it in React for a search function. 我想在React中使用它作为搜索功能。 Data source will change based on search result. 数据源将根据搜索结果而变化。 Also DOM contains search result will change. 另外DOM包含的搜索结果将更改。

This is container of search result: 这是搜索结果的容器:

<MovieList data={this.state.searchList} handleAdd={this.addList}/>

searchList will be updated as state. searchList将被更新为状态。

This is MovieList component: 这是MovieList组件:

var MovieList = React.createClass({

renderTemplate: function (data) {
    return data.map(function (movie) {
        return <Movie data={movie} onAdd={this.props.handleAdd}>
        </Movie>;
    });
},

componentDidMount: function () {
    $(ReactDOM.findDOMNode(this)).pagination({
        dataSource: this.props.data,
        pageSize: 6,
        callback: function (data, pagination) {
            // template method of yourself
            var html = this.renderTemplate(data);
            $(this.children).html(html);
        }
    }).bind(this);
},

componentWillUnmount: function () {

    $(ReactDOM.findDOMNode(this)).pagination('destroy');
},

render: function () {
    return (
        <div className={this.props.className}>
            <div className="data-container"></div>
        </div>
    );
}

}); });

After bind it in componentDidMount , should I use componentWillReceiveProps or other lifecycle methods to update it? componentDidMount中绑定它之后,我应该使用componentWillReceiveProps或其他生命周期方法来更新它吗?

Follow question : 追问

I'm totally new to React. 我是React的新手。 I don't know how to use React library. 我不知道如何使用React库。 From online resources, I found that I should install React component by npm . 从在线资源中,我发现我应该通过npm安装React组件。 I never used npm. 我从未使用过npm。 In my project, I used Django+React. 在我的项目中,我使用了Django + React。 Also I only use the classical method to include reference like this: 另外,我仅使用经典方法来包含如下引用:

<script src={% static 'pages/js/jquery-1.11.1.min.js' %}></script>
<script src={% static 'pages/bootstrap/js/bootstrap.min.js' %}></script>
<script src="https://unpkg.com/react@15.3.2/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15.3.2/dist/react-dom.js"></script>
<script src="https://unpkg.com/babel-core@5.8.38/browser.min.js"></script>

Where should I install React library by npm install? 我应该在哪里通过npm install安装React库? How to use it if I don't want to change the current structure of my project? 如果我不想更改项目的当前结构,该如何使用?

First up you should try and avoid jquery within React. 首先,您应该尝试避免在React中使用jquery。 Please read through this article " http://tech.oyster.com/using-react-and-jquery-together/ " 请通读本文“ http://tech.oyster.com/using-react-and-jquery-together/

Setting the state will call your render method, but the property updated by the state has to be set in the render method. 设置状态将调用您的render方法,但是必须在render方法中设置由状态更新的属性。 Your jquery code is unaware of the updated state, a state value is nothing but a property on your Movielist function and Jquery has no way of reading it. 您的jquery代码没有意识到更新后的状态,状态值不过是Movielist函数上的一个属性,而Jquery无法读取它。

Secondly componentWillRecieveProps is called when props are getting updated from parent or some other component, it is used for doing validations or calculations on set before render is called. 其次,当从父级或其他组件获取道具更新时,将调用componentWillRecieveProps,它用于在调用渲染之前对集合进行验证或计算。

Finally you can use https://github.com/ultimate-pagination/react-ultimate-pagination for pagination it is very flexible works with Bootstrap, MUI and few others you can write your own skin as well. 最后,您可以使用https://github.com/ultimate-pagination/react-ultimate-pagination进行分页,它与Bootstrap,MUI配合使用非常灵活,您也可以编写自己的皮肤。

You can use "DataTable" javascript functionality for this problem. 您可以使用“ DataTable” javascript功能来解决此问题。 Please refer https://www.datatables.net/ . 请参考https://www.datatables.net/

In this you just need to give your table Id in below code. 在这种情况下,您只需要在下面的代码中提供表ID。

$(document).ready(function(){
    $('#myTable').DataTable();
});

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

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