Получите данные dataTable, используя ajax - PullRequest
0 голосов
/ 06 ноября 2018

Я пытаюсь отобразить данные dataTable с помощью ajax, но это не сработало.

Это мой код jquery:

$('#example').DataTable({
                ajax: {
                    type: 'GET',
                    url: 'data.php',
                    success: function (msg) {
                        $("#tbody_example").html(msg);
                    }
                }
            });

Это моя страница data.php:

    <?php   echo '<td>Name</td>
            <td>Position</td>
            <td>Office</td>
            <td>Extn.</td>
            <td>Start date</td>
            <td>Salary</td>'; ?>

и это моя таблица данных:

 <table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Extn.</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </thead>
    <tbody id="tbody_example">

    </tbody>
    <tfoot>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Extn.</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </tfoot>
</table>

Моя таблица данных все еще пуста, и я получаю MOCK GET: data.php как ошибку. Любой может помочь мне.

Ответы [ 3 ]

0 голосов
/ 06 ноября 2018

Это не сработает.

Пожалуйста, обратитесь к документации DataTables , чтобы увидеть, что success должен не быть переопределенным, поскольку он используется внутри DataTables.

Вместо этого вы можете использовать function ajax( data, callback, settings )

$('#example').DataTable({
    ajax: function(data, callback, settings) {
        $.get({
            type: 'GET',
            url: 'data.php',
            success: function (msg) {
                $("#tbody_example").html(msg); // this is NOT how the table should be populated

                // invoke callback(msg) to populate the table
            });
    }
});

Таким образом, вы заполняете таблицу, вызывая callback(response_data) с response_data типа string[][] или object[]. В последнем случае вам может понадобиться добавить "columns" свойство в конфигурацию DataTables.

0 голосов
/ 06 ноября 2018

Это работает для меня. Я просто добавляю dataType

                ajax: {
                   type: 'GET',
                   url: 'data.php',
                   dataType : 'html',
                   success: function (msg) {
                      $("#tbody_datatable").html(msg);

                    },
                    error: function (req, status, err) {
                         console.log('Something went wrong', status, err);
                    }
                }
0 голосов
/ 06 ноября 2018

Попробуйте следующую вещь, она должна работать

В php файле

$content = "<td>Name</td>
            <td>Position</td>
            <td>Office</td>
            <td>Extn.</td>
            <td>Start date</td>
            <td>Salary</td>";

$return["json"] = json_encode();
echo json_encode($return);

В AJAX

ajax: {
         type: 'GET',
         url: 'data.php',
         success: function (msg) {
               $("#tbody_example").html(msg["json"]);
         }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...