简体   繁体   English

如何基于onclick在数据表上填充数据

[英]How to populate data on datatables based on onclick

I have ajax callback already and it based on the clicked button and will pass username then return it like this 我已经有ajax回调,它基于点击的按钮,将传递用户名,然后像这样返回它

在此输入图像描述

and this is my AJAX 这是我的AJAX

$(document).on("click", ".view", function(e){
    e.preventDefault();
    var nomination_result_id = $(this).data('id');

    $.ajax({
        type: "GET",
        dataType: "json",
        url: "json/getNominationVoters",
        data: { nomination_result_id: nomination_result_id }, //PASS DATA 
        success: function(data){ // RECEIVE DATA
            console.log(data);
        }
    });
});

and it open on my modal, this is the content of body 它以我的模态打开,这是身体的内容

<div class="modal-body">
    <div class="table-responsive">
        <table id="voters" class="table">
            <thead>
                <tr>
                    <th>Nominee</th>
                    <th>Vote Count</th>         
                </tr>
            </thead>
            <tbody>
                ....................
            </tbody>
        </table>
    </div>
</div>

and on my script 在我的剧本上

<script type="text/javascript">
    $('#voters').DataTable();
</script>

I don't know how to populate cause the ajax callback on documentation are for document.ready() only not on click. 我不知道如何填充因为文档上的ajax回调仅针对document.ready()而不是单击。 Please need help. 请帮忙。

Update 1 更新1

My Current Script 我目前的剧本

$.ajax({
    type: "GET",
    dataType: "json",
    url: "{{ URL::route('json/getNominationVoters') }}",
    data: { nomination_result_id: nomination_result_id }, //PASS DATA 
    success: function(data){ // RECEIVE DATA
        console.log(data);
        $.each(data, function(key, value){
            append_tr = "<tr>"+
                            "<td>"+value.firstname+"</td>"+
                            "<td>"+value.lastname+"</td>"+
                        "</tr>";
            $('#voters > tbody:last-child').append(append_tr);      
        });     
    }
});

Result: I've got a "No data.." 结果:我有一个“没有数据......” 在此输入图像描述

HTML Data: HTML数据:

<table id="voters" class="table">

</table>

your scripts are in a document.ready but since your view is loaded after that the scripts don't affect your partial, Use this to solve the Problem 你的脚本在document.ready中,但是因为你的视图被加载后脚本不会影响你的部分,使用它来解决问题

function YourScript(){
   // Your script here
}

and then call this function $('#voters').DataTable(); 然后调用此函数$('#voters').DataTable(); after you have loaded the partial view 加载局部视图后

$('#voters').html(data);
YourScript();

Hopefully this helps. 希望这会有所帮助。

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

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