您好,登錄后才能下訂單哦!
如何在React框架中使用SpreadJS,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
首先,我們需要在頁面中添加對React的引用:
<!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的預(yù)編譯版本(稱為babel-standalone),因此我們也會添加一個對此的引用:
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
最后,添加對Spread.Sheets的引用:
<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實(shí)例。我們稱之為“root”。
<div id="root"></div>
接下來,在頁面中添加一個腳本元素。我們將把所有的代碼放在這里:
<script type="text/babel"> </script>
然后,為Spread.Sheets定義一個React組件,以便我們可以定義一個擴(kuò)展React.Component的類:
class ReactSpreadJS extends React.Component{ }
該類需要在其中定義componentDidMount和render函數(shù)。componentDidMount函數(shù)在組件被掛載后立即被調(diào)用,所以我們用它來初始化Spread實(shí)例:
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); } }
接下來,在渲染函數(shù)中定義Spread.Sheets DOM元素:
render() { //Define the Spread.Sheets DOM template return( <div ref="spreadJs" style={{width:'100%',height:'100%'}}>); }
首先,通過App類定義應(yīng)用程序React組件:
//Define the application react component. class App extends React.Component{ }
接下來,添加一個您將調(diào)用ReactSpreadJS組件的渲染函數(shù):
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來初始化應(yīng)用程序:
ReactDOM.render( //Main entry, initialize application react component. <App/>, document.getElementById('root') );
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。