简体   繁体   English

获取大数据以在Datatable上呈现-ReactJS

[英]Fetching Large Data to render on Datatable - ReactJS

I have a list of 20,000 rows in my database that I am fetching to my table. 我的数据库中有一个20,000行的列表,该列表正在读取到表中。 I have this 20,000 rows stored to a localStorage to improve speed. 我将这20,000行存储到localStorage以提高速度。 My issue is, when I am navigating to the page (which contains the 20,000 rows), it lags for few seconds before the page is loaded. 我的问题是,当我导航到该页面(包含20,000行)时,它会在加载页面之前滞后几秒钟。

How can I let my page render and wait for the huge data to be fetched ? 如何让我的页面呈现并等待巨大的数据被提取?

PS: getItem function fetches the data and store in array items and stored to localStorage.setItem('my_items') . PS: getItem function fetches the data and store in array items and stored to localStorage.setItem('my_items')

Index.js Index.js

 state: {
     items:[],
     loading:true;
   }

componentDidMount()
{       

let items = JSON.parse(localStorage.getItem('my_items'));

if(items)
    {
      this.setState({
      items: items
    })      
     this.setState({ loading: false });
    }
    else
    {
        this.getItems();
    }
}





  return (

{loading &&
           <div className="d-flex justify-content-center loader-overlay">
              <CircularProgress />
           </div>
        }

       <MuiThemeProvider theme={this.getMuiTheme()}>

                <MUIDataTable

                    title={
                    }
                    data={this.state.items.map(item => {
                        return [
                           item.name,
                           item.type
                        ]

                     })}
                    columns={columns}
                    options={options}
                        />
            </MuiThemeProvider>   
        );

This problem is usually solved by using server side pagination. 通常通过使用服务器端分页来解决此问题。 It should work something like this: 它应该像这样工作:

  1. Open page with data table and you fetch first 20 items (or whatever size of a page is). 用数据表打开页面,您将获取前20个项目(或任何页面大小)。 You send /api/tableData?offset=0&limit=20 . 您发送/api/tableData?offset=0&limit=20 This will only return first 20 items. 这只会返回前20个项目。

  2. User clicks on next page, you fetch the second page using /api/tableData?offset=20&limit=20 . 用户单击下一页,即可使用/api/tableData?offset=20&limit=20获取第二页。 This will return items with id 21-40. 这将返回ID为21-40的项目。

  3. User clicks on next page, you fetch the third page using /api/tableData?offset=20&limit=20 用户点击下一页,您使用/api/tableData?offset=20&limit=20获取第三页

If you are using mui-datatables NPM package, it has option serverSide and it helps you to do it easily. 如果您使用的是mui-datatables NPM软件包,则它具有选项serverSide ,它可以帮助您轻松实现。 Chjeck Remote Data section in the documentation of MUI datatables package https://www.npmjs.com/package/mui-datatables#remote-data . MUI Remote Datahttps://www.npmjs.com/package/mui-datatables#remote-data的文档中的Chjeck Remote Data部分。

But if you are sure about your use case and you don't want to do server side pagination, you can use Web Worker . 但是,如果您确定自己的用例,并且不想执行服务器端分页,则可以使用Web Worker The problem in your application is that JSON.parse for such a huge collection takes way too long. 您的应用程序中的问题是,对于如此庞大的集合, JSON.parse花费的时间太长。 With Web Worker , you can start another thread that will not block the UI thread. 使用Web Worker ,您可以启动不会阻塞UI线程的另一个线程。 In the worker thread, you will start parsing the collection and when you finish parsing, you will send the data back to the UI thread. 在辅助线程中,您将开始解析集合,并且在完成解析后,会将数据发送回UI线程。 Web workers were designed for exactly this use case. Web工作人员正是为此用例而设计的。 You can learn about it more here: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers 您可以在这里了解更多信息: https : //developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers

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

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