1

I am very new to React and I am trying to use DataTables with table which I create in render(). So far my code looks like this- searchForm.js:

componentDidMount() {
    $(this.refs.table).DataTable({});
  }

  render() {
    var headerComponents = this.generateHeaders(),
        rowComponents = this.generateRows();
    return (
      <div>
        <form onSubmit={this.handleSubmit}>
          <input className="resizedTextbox" type="text" value={this.state.searchString} onChange={this.handleChange} placeholder="Enter Material to search"/>
          <br />
          <br />
          <input className="btn btn-primary" type="submit" value="Search" />
          <button type="button" className="btn btn-primary" onClick={this.resetSearch}>Reset</button>
        </form>
        <br />
        <div className="table-responsive">
            <table ref="table" className="table">
                <thead> {headerComponents} </thead>
                <tbody> {rowComponents} </tbody>
            </table>
        </div>
      </div>
    );
  }

Index.html:

<!DOCTYPE html>
<html>
  <body>
    <header style="background-color: #990000;margin-top: 0;position: relative;top: -10px;">
      <h1 style="margin-top: 0;color: white;padding: 10px;">DMFT Search</h1>
    </header>
    <div align="center" >
      <div id="main"></div>
    </div>
    <div id="search"></div>
    <!-- scripts -->
    <script src="{{ url_for('static', filename='bower_components/jquery/dist/jquery.min.js') }}"></script>
    <script src="{{ url_for('static', filename='bower_components/react/react.js') }}"></script>
    <script src="{{ url_for('static', filename='bower_components/react/react-dom.min.js') }}"></script>
    <script src="{{ url_for('static', filename='scripts/js/searchForm.js') }}"></script>
    <script src="{{ url_for('static', filename='bower_components/datatables.net/js/jquery.dataTables.js') }}"></script>
  </body>
</html>

The problem is that when I load the index.html, I do not see DataTables working, I get an exception in console :

Uncaught TypeError: $(...).DataTable is not a function
    at SearchForm.componentDidMount

Kindly advise if I am doing it the right way. I did not find ant help regarding this.

3
  • 1
    you are executing react code before jquery DataTable is loaded... Change the order of the loading scripts so your react code loads last Commented Mar 15, 2017 at 1:35
  • Thanks, it works. Now Datatables is not working but I will have to open a new thread. Please vote the question if you found it relevant! Commented Mar 15, 2017 at 2:12
  • jQuery Data table is not the right thing to select in Reactjs. I would suggest you to explore Reactjs fixed data table from Facebook facebook.github.io/fixed-data-table Commented Mar 15, 2017 at 3:30

2 Answers 2

1

I know its an old one, But can help anybody else by trying the following.

  1. Include the datatables js file or cdn link in your public/index.html after jquery file.
  2. Create a js folder in public directory and create a custom.js file with code

    $(function() { $("#example").DataTable(); });

  3. Create a Table component , and inside componentDidMount or useEffect add this code

    const script = document.createElement("script"); script.src = "/js/custom.js"; script.async = true; document.body.appendChild(script);

  4. From the Table component return the table with id "example"

    <table id="example" className="table table-striped table-bordered"> <thead> <tr> <th>head 1</th> <th>head 2</th> </tr> </thead> <tbody> <tr>Data 1</tr> <tr>Data 2</tr> </tbody> </table>

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

Comments

0

Index.html is not processed by react so just use plain strings. Try this:

<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/react/react.js"></script>
<script src="bower_components/react/react-dom.min.js"></script>
<script src="scripts/js/searchForm.js"></script>
<script src="bower_components/datatables.net/js/jquery.dataTables.js"></script>

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.