4

I'm having trouble in React Js. My data table is successfully populated but still showing "No data available in table" on the first row https://i.sstatic.net/nrZaL.jpg

This is screenshot of displayed html https://i.sstatic.net/JUxkz.jpg

Also when I using the sorting and search thingy all my data will be gone and just displays "No data available in table"

Network result : https://i.sstatic.net/zTOpZ.jpg My code to display data from database

class Uptime extends Component {
    constructor(props) {
        super(props);
        this.state = {
            error: null,
            isLoaded: false,
            data: []
        }
    }

    componentDidMount() {
        fetch('/uptime')
            .then(res => res.json())
            // .then(res => res.text())          // convert to plain text
            // .then(text => console.log(text))
            .then(
                (result) => {
                    this.setState({
                        isLoaded: true,
                        data: result.data
                    })
                },
                (error) => {
                    this.setState({
                        isLoaded: true,
                        error
                    })
                }
            )
    }

    render() {
        const {error, isLoaded, data} = this.state;
        // if (error) {
        //     return <div>Error: {error.message}</div>;
        // } else if (!isLoaded) {
        //     return (
        //         <div className="spinner-border text-primary" role="status">
        //             <span className="sr-only">Loading...</span>
        //         </div>
        //     )
        // } else {    WHEN I ADD THIS CODE, THE SEARCH FUNCTION AND SORT FUNCtION IS GONE , SO I JUST REMOVE IT
        return (
            <div id="content-wrapper" className="d-flex flex-column">
                <div id="content">
                    <div className="container-fluid">
                        <div className="card shadow mb-4 mt-5">
                            <div className="card-header py-3 text-center">
                                <h6 className="m-0 font-weight-bold text-primary">DataTables Example</h6>
                            </div>
                            <div className="card-body">
                                <div className="table-responsive">
                                    <table className="table table-bordered" id="dataTable" width="100%"
                                           cellSpacing="0">

                                        <thead>
                                        <tr>
                                            <th width="10%">Name</th>
                                            <th width="40%">Url</th>
                                            <th width="10%">Downtime</th>
                                            <th width="20%">date</th>
                                            <th width="10%">Status Code</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                          {data.map(item => (
                                                        <tr key={item[0]}>
                                                        <td>{item[1]}</td>
                                                        <td>{item[2]}</td>
                                                        <td>{item[5]}</td>
                                                        <td>{item[4]}</td>
                                                        <td>{item[3]}</td>
                                                        </tr>
                                                      ))}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>

                    </div>

                </div>
                <footer className="sticky-footer bg-white">
                    <div className="container my-auto">
                        <div className="copyright text-center my-auto">
                            <span>Copyright &copy; Your Website 2019</span>
                        </div>
                    </div>
                </footer>

            </div>

        )

    }
}
12
  • 1
    Hello, please provide a Minimal, Complete, and Reproducible code example, i.e. the component code that fetches, processes, and renders the data. Commented May 12, 2020 at 6:12
  • Please share the full code so that we can help you here. Commented May 12, 2020 at 6:13
  • Ive update my code , please check Commented May 12, 2020 at 6:14
  • Looks like "No data available in table" is part of your data that is being mapped into the DOM. Can you also provide your response from network? Unlikely but I wonder if it's a react key issue. Commented May 12, 2020 at 6:16
  • @DrewReese, Nope "No data available in table" is NOT part of my data Commented May 12, 2020 at 6:17

2 Answers 2

4

Issue :

Datatable is being initialized before the data comes, so it will show "No data available in table" , once table is initialized then after you are making changes in that table , so you data got append after the row "No data available in table" , you can check that issue by running below snippet.

const { useState , useEffect } = React;

const App = () => {

  const [users,setUsers] = useState([]);

  useEffect(() => {
    setTimeout(() => {
      setUsers([[0,"Vivek"],[1,"Darshita"]]);
    },2000);
  },[]);

  return (
    <table className="table table-bordered" id="dataTable" width="100%" cellSpacing="0">
    <thead>
        <tr>
            <th width="10%">Name</th>
        </tr>
    </thead>
    <tbody>
        {
            users.map(item => (
                    <tr key={item[0]}>
                        <td>{item[1]}</td>
                    </tr>))
        }
    </tbody>
    </table>
  );
}

ReactDOM.render(<App />, document.getElementById('react-root'));

$(document).ready(function() {
    $('#dataTable').DataTable();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.js"></script>
<div id="react-root"></div>

Solution :

As datatable is being initialized automatically, you have to destroy it before setting up the state and and reinitialized it once data is available, as you can see the same in the below snippet

const { useState , useEffect } = React;

const App = () => {

  const [users,setUsers] = useState([]);

  useEffect(() => {
    setTimeout(() => {
      $('#dataTable').DataTable().destroy();
      setUsers([[0,"Vivek"],[1,"Darshita"]]);
    },2000);
  },[]);

  useEffect(() => {
      $('#dataTable').DataTable();
  },[users]);

  return (
    <table className="table table-bordered" id="dataTable" width="100%" cellSpacing="0">
    <thead>
        <tr>
            <th width="10%">Name</th>
        </tr>
    </thead>
    <tbody>
        {
            users.map(item => (
                    <tr key={item[0]}>
                        <td>{item[1]}</td>
                    </tr>))
        }
    </tbody>
    </table>
  );
}

ReactDOM.render(<App />, document.getElementById('react-root'));

$(document).ready(function() {
    $('#dataTable').DataTable();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.js"></script>
<div id="react-root"></div>

Sign up to request clarification or add additional context in comments.

7 Comments

let me try this and will get back with the result :)
"Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:" I still dont know about useEffects so I cant seem to make this work :), althought I got your logic on how to solve it
You have to put that in componentDidMount , put $('#dataTable').DataTable().destroy(); before fetch and $('#dataTable').DataTable(); after you fetch the data
@Yan, check now
I have added an error on the code link, I think this is already related to jquery
|
0

This solution have no sense but solved this problem for me :

useEffect(()=>{
        const timer = setTimeout(function () {
            ApplyDataTable()// here you call the datatable logic 
        }, 0);  // you see here , it run immediatly no ,dely !!!!but it solve it :D
        return () => clearTimeout(timer);

})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.