純乾貨分享: 如何在 React 框架中使用SpreadJS

做者:葡萄城控件
SpreadJS最好的功能之一就是可以在不一樣的框架中使用它。本文將演示如何在簡單的Web頁面中使用Babel JavaScript編譯器將SpreadJS與React快速結合。css


第1步:設置HTML5頁面

首先,咱們須要在頁面中添加對React的引用:html

<!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8" />
        <title>SpreadJS React Demo</title>
        <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
        <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    </head>
    </html>

在這個頁面中,咱們將使用Babel的預編譯版本(稱爲babel-standalone),所以咱們也會添加一個對此的引用:前端

<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>

最後,添加對Spread.Sheets的引用:react

<script src="http://cdn.grapecity.com/spreadjs/hosted/scripts/gc.spread.sheets.all.11.0.0.min.js"></script>
    <link rel="stylesheet" type="text/css" href="http://cdn.grapecity.com/spreadjs/hosted/css/gc.spread.sheets.excel2013white.11.0.0.css">

在咱們編寫任何腳本以前,咱們須要定義一個DIV元素來包含Spread實例。咱們稱之爲「root」。安全

<div id="root"></div>

第2步:爲Spread.Sheets建立一個React類

接下來,在頁面中添加一個腳本元素。咱們將把全部的代碼放在這裏:babel

<script type="text/babel">
    </script>

而後,爲Spread.Sheets定義一個React組件,以便咱們能夠定義一個擴展React.Component的類:app

class ReactSpreadJS extends React.Component{
    }

該類須要在其中定義componentDidMount和render函數。componentDidMount函數在組件被掛載後當即被調用,因此咱們用它來初始化Spread實例:框架

componentDidMount() {
        //In the DidMount life cycle, we initialize Spread Sheet instance, and the host is defined in the Component template.
        let spread = new GC.Spread.Sheets.Workbook(this.refs.spreadJs, {sheetCount: 3});
    
        if(this.props.workbookInitialized){
            this.props.workbookInitialized(spread);
        }
    }

接下來,在渲染函數中定義Spread.Sheets DOM元素:dom

render() {
        //Define the Spread.Sheets DOM template
        return(
            <div ref="spreadJs"  style={{width:'100%',height:'100%'}}>
            </div>);
    }

第3步:爲組件建立一個應用程序類

首先,經過App類定義應用程序React組件:函數

//Define the application react component.
    class App extends React.Component{
    }

接下來,添加一個您將調用ReactSpreadJS組件的渲染函數:

render(){
        //In the root component, it include one ReactSpreadJS component.
        return(
                <div style={{width:'800px',height:'600px'}}>
                    <ReactSpreadJS workbookInitialized = {(spread)=>{console.log(spread)}}>
    
                    </ReactSpreadJS>
                </div>
        )
    }

要完成腳本,請告訴React經過使用ReactDOM.render來初始化應用程序:

ReactDOM.render(
        //Main entry, initialize application react component.
            <App/>,
        document.getElementById('root')
    );

這就是使用React將Spread.Sheets添加到HTML頁面所需的所有內容。這只是React和Spread.Sheets的基本使用,但能夠輕鬆擴展。


關於SpreadJS前端表格控件

SpreadJS 純前端表格控件是基於 HTML5 的 JavaScript 電子表格和網格功能控件,適用於.NET、Java 、Web應用程序、移動端等多種平臺的表格數據處理和類Excel功能的表格程序開發。全中文操做界面,零學習成本!便於您在系統開發過程當中,更安全的管理Excel 數據,更快捷的完成海量數據交互,更方便的進行數據導出、導入、排序、過濾、增刪改查、可視化及Excel 導入/導出等操做。SpreadJS自面世以來,備受華爲、中通、中國民航飛行學院、中國平安 、中國能建、浪潮等國內知名企業客戶青睞。

相關文章
相關標籤/搜索