简体   繁体   中英

Jquery Datatables and Jeditable - Data is not displaying

I am using Jquery DataTables and jEditable. I have a correct JSON response as follows:

[{"country_id":"18","country":"Aruba","country_enabled":"1"},{"country_id":"19","country":"Afghanistan","country_enabled":null},{"country_id":"22","country":"Angola","country_enabled":"1"},{"country_id":"23","country":"Anguilla","country_enabled":null},{"country_id":"24","country":"\u00c5land Islands","country_enabled":null},{"country_id":"25","country":"Albania","country_enabled":null},{"country_id":"26","country":"Andorra","country_enabled":null},{"country_id":"27","country":"United Arab Emirates","country_enabled":null},{"country_id":"29","country":"Argentina","country_enabled":null},{"country_id":"30","country":"Armenia","country_enabled":null},{"country_id":"31","country":"American Samoa","country_enabled":null},{"country_id":"32","country":"Antarctica","country_enabled":null},{"country_id":"33","country":"French Southern Territories","country_enabled":null},{"country_id":"34","country":"Antigua and Barbuda","country_enabled":null},{"country_id":"35","country":"Australia","country_enabled":null},{"country_id":"36","country":"Austria","country_enabled":null},{"country_id":"37","country":"Azerbaijan","country_enabled":null},{"country_id":"38","country":"Burundi","country_enabled":null},{"country_id":"39","country":"Belgium","country_enabled":null},{"country_id":"40","country":"Benin","country_enabled":null},{"country_id":"41","country":"Bonaire, Sint Eustatius and Saba","country_enabled":null},{"country_id":"42","country":"Burkina Faso","country_enabled":null},{"country_id":"43","country":"Bangladesh","country_enabled":null},{"country_id":"44","country":"Bulgaria","country_enabled":null},{"country_id":"45","country":"zoo","country_enabled":null},{"country_id":"46","country":"Xylaphone","country_enabled":null}]

The above is taken from Chrome's developer tools and from the XHR window and, therefore, I know the response looks correct and the data is being received.

Here is my HTML to display the data:

<div class="content">
<div id="pad-wrapper" class="form-page">
    <div class="row">
        <div class="col-md-12">
            <h2>List of Countries</h2>
        </div>
        <div class="bs-example">
            </br>
            <form>
<div align = "left">
                <button type="button" class="btn btn-success" onclick="window.location='<?php echo site_url("admin/country_add");?>'">
                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add Country
                </button>
                <button type="button" class="btn btn-danger" onclick="window.location='<?php echo site_url("admin/country_delete");?>'">
                    <span class="glyphicon glyphicon-minus" aria-hidden="true"></span> Delete Countries
                </button>
            </div>

            <!-- start table listing -->
     <table id="myDataTable">
    <thead>
        <tr>
            <th>country_id</th>
            <th>country</th>
            <th>country_enabled</th>
    </thead>

    <tbody>         
    </tbody>

</table>
<button id="btnAddNewRow">Add</button>
<button id="btnDeleteRow">Delete</button>
        </div>
    </div>
</div>

The datatable appears in the view but just says, "loading..." and no data is ever displayed.

I have renamed the column headers the same as my database but it still does not display the data.

There is an error in the console as follows: 在此处输入图片说明

In the client-side processing mode data provided via Ajax should have the following structure, see ajax option for more details.

{
    "data": [
        // row 1 data source,
        // row 2 data source,
        // etc
    ]
}

The solution is to correct your JSON data to look like:

{ 
    "data": [
        {"country_id":"18","country":"Aruba","country_enabled":"1"}
    ]
}

Alternative solution is to set ajax.dataSrc to empty string to indicate that you're returning plain array, see the sample code below:

$('#example').dataTable({
  "ajax": {
    "url": "data.json",
    "dataSrc": ""
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM