鍍金池/ 問(wèn)答/HTML/ 在react中做一個(gè)輪詢(xún)的表格,每次表格展示五個(gè)數(shù)據(jù),如果數(shù)據(jù)為空,則表格內(nèi)容顯

在react中做一個(gè)輪詢(xún)的表格,每次表格展示五個(gè)數(shù)據(jù),如果數(shù)據(jù)為空,則表格內(nèi)容顯示“沒(méi)內(nèi)容“怎么實(shí)現(xiàn)

要求1

每次表格展示五個(gè)數(shù)據(jù),不斷輪詢(xún)

要求2

如果數(shù)據(jù)為空,則表格內(nèi)容顯示“沒(méi)內(nèi)容“

要求3

當(dāng)單元格內(nèi)容為告警時(shí),這個(gè)單元格的樣式改變,字體傾斜,并且背景顏色變成紅色

新補(bǔ)充,輪詢(xún)寫(xiě)完了,能不能幫我把這段優(yōu)化下

import React from 'react'
import './styles/'
import Red from './images/red.png'
import Blue from './images/blue.png'
import Yellow from './images/yellow.png'

class Table extends React.Component {

    constructor() {
        super();
        this.state = {
            tableHead: [],
            tableBody: []
        }
        this.tableTimer = null;
    }

    render() {

        let backgroundColor = ['rgba(56,145,255,0.1)','rgba(0,0,0,0)'];

        let tdStyle = {
            height:60,
            fontSize: 20,
            color: 'rgba(255,255,255,0.6)',
        };
        let tdStyleOdd = {
            ...tdStyle,
            backgroundColor: backgroundColor[1],
        };
        let tdStyleEven = {
            ...tdStyle,
            backgroundColor: backgroundColor[0],
        };

        let thStyle = {
            textAlign: 'center',
            fontSize: 20,
            height:32
        }

        let emptyTdStyle = {
            width: 500,
            height:300,
            fontSize: 30,
            //backgroundColor: 'rgba(255,255,255,1)',
            textAlign: 'center'
        }

        let imgSmall = {
            width: 18,
            height:18
        }

        let emptyContent = (
            <tr>
                <td colSpan={6}  // 跨列
                    style={emptyTdStyle}>當(dāng)前無(wú)告警</td>
            </tr>
        );

        let tableHead = this.state.tableHead&&this.state.tableHead.length !==0
            &&this.state.tableHead.map(function (head,index) {
                if(head == 'index'){
                    //return '序號(hào)';
                    return (<th style= {{
                        ...thStyle,
                        width:70
                    }} key={index}>序號(hào)</th>)
                } else if (head == 'type') {
                    //return '任務(wù)名稱(chēng)';
                    return (<th style= {{
                        ...thStyle,
                        width:180
                    }} key={index}>任務(wù)名稱(chēng)</th>)
                } else if (head == 'level') {
                    //return '告警等級(jí)';
                    return (<th style= {{
                        ...thStyle,
                        width:80
                    }} key={index}>告警等級(jí)</th>)
                } else if (head == 'message') {
                    //return '告警信息';
                    return (<th style= {{
                        ...thStyle,
                        width:237
                    }} key={index}>告警信息</th>)
                }
            })

        const showCell = function(cell,index) {
            //
        }

        let tableBody = this.state.tableBody && this.state.tableBody.length != 0&&
            this.state.tableBody.map((row,index)=>{
                return (
                    <tr key={index} style={{

                    }}>
                        {index%2 == 0?
                            row.map((cell,index)=> {
                                if(cell=='故障'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleEven,

                                        }}
                                    ><img src={Red} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='正常'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleEven,

                                        }}
                                    ><img src={Blue} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='告警'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleEven,

                                        }}
                                    ><img src={Yellow} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else {
                                    if(index != 3){
                                        if(cell.length>=10){cell = cell.substring(0,10)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleEven,
                                                textAlign: 'center',
                                            }}
                                        >{cell}</td>
                                    } else {
                                        if(cell.length>=10){cell = cell.substring(0,10)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleEven
                                            }}
                                        >{cell}</td>
                                    }
                                }
                            }):
                            row.map((cell,index)=>{
                                if(cell=='故障'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleOdd
                                        }}
                                    ><img src={Red} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='正常'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleOdd
                                        }}
                                    ><img src={Blue} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='告警'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleOdd
                                        }}
                                    ><img src={Yellow} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else {
                                    if(index != 3){
                                        if(cell.length>=10){cell = cell.substring(0,10)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleOdd,
                                                textAlign: 'center',
                                            }}
                                        >{cell}</td>
                                    } else {
                                        if(cell.length>=10){cell = cell.substring(0,10)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleOdd
                                            }}
                                        >{cell}</td>
                                    }

                                }

                            })
                        }
                    </tr>
                )
            })

        return (
            <div>
                <table className={'telecommunication_table'}
                       border={0}
                       cellSpacing = {0}
                       cellPadding = {0}>
                    <thead className={'theads'}>
                        <tr>
                            {this.state.tableHead&&this.state.tableHead.length !==0?
                                tableHead: ''
                            }
                        </tr>
                    </thead>
                    <tbody>
                        {this.state.tableBody && this.state.tableBody.length != 0?
                            tableBody: emptyContent
                        }
                    </tbody>
                </table>
            </div>
        )
    }



    separateBody(tableBody,separateLength){

        let tableBodyList = [];
        let tableTempBody = [];

        tableBody.map(function(item,index){

            tableTempBody.push(item)
            if(tableTempBody.length == separateLength){

                tableBodyList.push(tableTempBody);
                tableTempBody = [];
            } else if (index == tableBody.length-1) {
                tableBodyList.push(tableTempBody);
                tableTempBody = [];
            }
        })

        return tableBodyList;
    }





    componentDidMount() {

        const _self = this;
        // let tableBody = [];
        let tableHead = ['index','type','level','message'];
        let tableBody = [
            [10,"任務(wù)5","告警","樵坪山到云篆山"],
            [11,"任務(wù)6","故障","樵坪山到云篆山不通"],
            [12,"任務(wù)3","故障","樵坪山到云篆山不通"],
            [13,"任務(wù)13","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [14,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [15,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [16,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [57,"任務(wù)1","故障","網(wǎng)絡(luò)鏈路不通"],
            [18,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [19,"任務(wù)1","告警","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [510,"任務(wù)1","故障","樵坪山到云篆山"],
            [21,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [22,"任務(wù)1","告警","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [23,"任務(wù)1","正常","樵坪山到云篆山"],
            [24,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [25,"任務(wù)1","告警","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [26,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [27,"任務(wù)1","故障","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [218,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [29,"任務(wù)1","故障","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [630,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [31,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [32,"任務(wù)1","告警","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [33,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [34,"任務(wù)1","故障","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
            [35,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"]
        ]

        // let tableBody = [
        //     [10,"任務(wù)5","告警","樵坪山到云篆山"],
        //     [11,"任務(wù)6","故障","樵坪山到云篆山不通"],
        //     [12,"任務(wù)3","故障","樵坪山到云篆山不通"],
        //     [13,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
        //     [14,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
        //     [15,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
        //     [16,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"],
        //     [17,"任務(wù)1","正常","網(wǎng)絡(luò)鏈路不通"],
        //     [18,"任務(wù)1","正常","樵坪山到云篆山匯聚點(diǎn)網(wǎng)絡(luò)鏈路不通"]
        // ];

        let separateLength = 10;
        let tableSeparateData = _self.separateBody(tableBody,separateLength)
        //console.log('tableSeparateData =', tableSeparateData);
        this.stateChange(tableHead,tableSeparateData[0]);
        if(tableSeparateData&&tableSeparateData.length>1){
            _self.tableTimer = _self._setInterval(tableSeparateData);
        }



        //this.fetchMockData();


    }

    _setInterval(param){
        const _self= this;
        let table_index = 1;
        setInterval(function(){
            _self.setState({
                tableBody: param[table_index]
            });
            if( table_index+1 == param.length){
                table_index = 0;
            }else {
                table_index = table_index+1;
            }

        },5000)
    }

    stateChange(head, body){
        //console.log(receiveData);
        const _self= this;
        _self.setState({
            tableHead:head,
            tableBody: body
        })
    }

    tableBodyChange(){
        console.log('tableBody ==', this.state.tableBody)
    }

    componentWillReceiveProps(nextProps) {
        const _self= this;
        console.log('組件將被移除')
        if(this.tableTimer != null){
            window.clearInterval(this.tableTimer)
        }
        let separateLength = 10;
        let tableSeparateData = _self.separateBody(nextProps.tableBody,separateLength)
        this.stateChange(nextProps.tableHead,tableSeparateData[0]);
        if(tableSeparateData&&tableSeparateData.length>1){
            _self.tableTimer = _self._setInterval(tableSeparateData);
        }
    }

    componentWillUnmount() {
        console.log('組件將被移除')
        if(this.tableTimer != null){
            window.clearInterval(this.tableTimer)
        }

    }
}

export default Table;
let tableBody = this.state.tableBody && this.state.tableBody.length != 0&&
            this.state.tableBody.map((row,index)=>{
                return (
                    <tr key={index} style={{

                    }}>
                        {index%2 == 0?
                            row.map((cell,index)=> {
                                if(cell=='故障'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleEven,

                                        }}
                                    ><img src={Red} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='正常'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleEven,

                                        }}
                                    ><img src={Blue} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='告警'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleEven,

                                        }}
                                    ><img src={Yellow} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else {
                                    if(index != 3){
                                        if(cell.length>=10){cell = cell.substring(0,10)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleEven,
                                                textAlign: 'center',
                                            }}
                                        >{cell}</td>
                                    } else {
                                        if(cell.length>=10){cell = cell.substring(0,10)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleEven
                                            }}
                                        >{cell}</td>
                                    }
                                }
                            }):
                            row.map((cell,index)=>{
                                if(cell=='故障'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleOdd
                                        }}
                                    ><img src={Red} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='正常'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleOdd
                                        }}
                                    ><img src={Blue} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='告警'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleOdd
                                        }}
                                    ><img src={Yellow} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else {
                                    if(index != 3){
                                        if(cell.length>=10){cell = cell.substring(0,10)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleOdd,
                                                textAlign: 'center',
                                            }}
                                        >{cell}</td>
                                    } else {
                                        if(cell.length>=10){cell = cell.substring(0,10)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleOdd
                                            }}
                                        >{cell}</td>
                                    }

                                }

                            })
                        }
                    </tr>
                )
            })

能不能幫忙把這段優(yōu)化下

回答
編輯回答
初念

看你需求,是整個(gè)table都不要還是只是替換掉tbody里面的內(nèi)容。

如果是后者,只要把三元表達(dá)式內(nèi)的空字符串替換成

<tr><td colSpan={tableHeader.length}>沒(méi)內(nèi)容</td></tr>
2017年1月2日 02:43