简体   繁体   English

在表格中显示ajax响应

[英]Display ajax response inside table

I want to display data from ajax response inside table. 我想在表格中显示来自ajax响应的数据。

Here is my table: 这是我的表:

<table data-toggle="table" data-show-columns="true" data-search="true" data-select-item-name="toolbar1" id="menu_table">
    <thead>
    <tr>
        <th class="centerText" data-field="item_id">ID</th>
        <th class="centerText" data-field="name">Name</th>
        <th class="centerText" data-field="price">Price</th>
        <th class="centerText" data-field="image">Image</th>
        <th class="centerText" data-field="edit">Edit</th>
    </tr>
    </thead>
    <tbody style="text-align:center;" id="menu_table_data"></tbody>
</table>

This is menu_table.php which gets data from database and returns response to ajax : 这是menu_table.php ,它从数据库获取数据并返回对ajax的响应:

$output = '';
$search = mysqli_query($link,"SELECT menu.id, menu.name, menu.price, menu.image ORDER BY menu.id ASC;");

while($data = mysqli_fetch_array($search))
{
    $output .= '<td>'.$data['id'].'</td>
                <td>'.$data['name'].'</td>
                <td>'.$data['price'].'</td>
                <td><div id="div_image">
                <img src="uploaded_images/'.$data['image'].'" class="thumbnail" height="100" width="80" /></div></td>
                <td><a class="btn btn-primary glyphicon glyphicon-edit" role="button"></a></td>
                ';
}
echo $output;

This is ajax function: 这是ajax功能:

$(document).ready(function(){
    function fetch_data(){
        $.ajax({
            url: "menu_table.php",
            method: "POST",
            success: function(data){
                $('#menu_table_data').html(data);
            }
        });
    }
    fetch_data();
});

$('#menu_table_data').html(data); $( '#menu_table_data')HTML(数据)。 is showing data but not like table data. 显示数据但不像表数据。 How can i show it inside table properly? 如何在桌子内正确显示?

You have missed <tr></tr> . 你错过了<tr></tr>

try this: 尝试这个:

$output = ''; 
$search = mysqli_query($link,"SELECT menu.id, menu.name, menu.price, menu.image ORDER BY menu.id ASC;"); 
while($data = mysqli_fetch_array($search)) { 
    $output .= '
    <tr>
      <td>'.$data['id'].'</td>
      <td>'.$data['name'].'</td>
      <td>'.$data['price'].'</td>
      <td>
        <div id="div_image">
          <img src="uploaded_images/'.$data['image'].'" class="thumbnail" height="100" width="80" /></div>
      </td>
      <td>
        <a class="btn btn-primary glyphicon glyphicon-edit" role="button"></a>
      </td>
    </tr>
    '; 
}
echo $output;

AJAX - JS - 从 AJAX 响应创建表并放入<div></div><div id="text_translate"><p>在这个方法中,我以文本形式传递了这个动作的响应,但是我想使用表格格式,我该怎么做?</p><pre> function loadAditivos(){ $('#aditivoAbertoInformacoesTexto').html('&lt;div id="loaderMaiorDemandante"&gt;&lt;/div&gt;'); $("#loader").show(); var jsonHide = $('#activeJsonHide').html(); if(jsonHide) { $.ajax({ url: 'search.action', // action to be perform type: 'POST', //type of posting the data data: { dataJson: jsonHide }, // data to set to Action Class dataType: 'text', success: function (html) { $("#loader").hide(); $('#showAdditiveasText').html(html); //How to pass a tablw to That DIV? }, }); } }</pre><p> 如何使用表格格式将此数据传递给 div: "$('#showAdditiveasText').html(html)"?</p><p> 例子:</p><pre> [ { "UserID": 1, "UserName": "rooter", "Password": "12345", "Country": "UK", "Email": "sac@gmail.com" }, { "UserID": 2, "UserName": "binu", "Password": "123", "Country": "uk", "Email": "Binu@gmail.com" }, { "UserID": 3, "UserName": "cal", "Password": "123", "Country": "uk", "Email": "cal@gmail.com" }, { "UserID": 4, "UserName": "nera", "Password": "1234", "Country": "uk", "Email": "nera@gmail.com" } ]</pre></div> - AJAX - JS - Create a table from a AJAX response and put inside a <div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在表中显示 ajax 响应 - Display ajax response in Table 在表中显示ajax响应值 - Display ajax response value in table 将 ajax 的 JSON 响应显示为 HTML 或表格 - Display JSON response from ajax as HTML or table Ajax响应未填充INSIDE HTML <table> 标签 - ajax response not populating INSIDE html <table> tag AJAX - JS - 从 AJAX 响应创建表并放入<div></div><div id="text_translate"><p>在这个方法中,我以文本形式传递了这个动作的响应,但是我想使用表格格式,我该怎么做?</p><pre> function loadAditivos(){ $('#aditivoAbertoInformacoesTexto').html('&lt;div id="loaderMaiorDemandante"&gt;&lt;/div&gt;'); $("#loader").show(); var jsonHide = $('#activeJsonHide').html(); if(jsonHide) { $.ajax({ url: 'search.action', // action to be perform type: 'POST', //type of posting the data data: { dataJson: jsonHide }, // data to set to Action Class dataType: 'text', success: function (html) { $("#loader").hide(); $('#showAdditiveasText').html(html); //How to pass a tablw to That DIV? }, }); } }</pre><p> 如何使用表格格式将此数据传递给 div: "$('#showAdditiveasText').html(html)"?</p><p> 例子:</p><pre> [ { "UserID": 1, "UserName": "rooter", "Password": "12345", "Country": "UK", "Email": "sac@gmail.com" }, { "UserID": 2, "UserName": "binu", "Password": "123", "Country": "uk", "Email": "Binu@gmail.com" }, { "UserID": 3, "UserName": "cal", "Password": "123", "Country": "uk", "Email": "cal@gmail.com" }, { "UserID": 4, "UserName": "nera", "Password": "1234", "Country": "uk", "Email": "nera@gmail.com" } ]</pre></div> - AJAX - JS - Create a table from a AJAX response and put inside a <div> 如何在相扑选择下拉列表中显示Ajax响应? - How to display the ajax response inside sumo select dropdown? 如何在Ajax响应表中附加复选框 - How to append check box inside a table of Ajax response jQuery中模态内的html表上未显示ajax响应数据 - ajax response data is not showing on html table inside modal in Jquery ajax响应显示为零 - ajax response display zero Ajax POST-显示响应 - Ajax POST - display response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM