Mustafa Berkan Demir
Mustafa Berkan Demir

Reputation: 133

Filling html table with json data in jquery

i want to fill my html table with json data when the button is clicking. I wrote some jquery like this

<script>
function getData(){
    var URL_PREFIX="http://localhost:8983/solr/archiveCore/select?q=strSO:";
    var URL_MIDDLE="AND PackName:";
    var URL_SUFFIX="AND DocType:";
    var strSO="\"" + $("#ngramBoxstrSO").val() + "\"";
    var PackName="\"" + $("#ngramBoxPackName").val() + "\"";
    var DocType="\"" + $("#ngramBoxDocType").val() +"\"";
    var URL=URL_PREFIX + strSO + URL_MIDDLE + PackName + URL_SUFFIX + DocType;
    $.ajax({
        url:URL,
        dataType:'jsonp',
        jsonp : 'json.wrf',
        type :'get',
        cache :false,
        success: function(data){
            var docs=JSON.stringify(data.response.docs);
            var jsonData=JSON.parse(docs);
            var html='';
            $.each(jsonData,function(value,key){
                html+='<tr>';
                html+='<td>'+value.id+'</td>';
                html+='<td>'+value.strSO+'</td>';
                html+='<td>'+value.PackName+'</td>';
                html+='<td>'+value.DocType+'</td>';
                html+='<td>'+value.DocName+'</td>';
                html+='<td>'+value.FilePath+'</td>';
                html+='</tr>';
            });
            $("#tbody").append(html);
        },
    });
}

</script> 

and when i run my project the table fills 'undefined' word like this. what is the wrong code? can you help me?

Upvotes: 0

Views: 40

Answers (2)

Mustafa Berkan Demir
Mustafa Berkan Demir

Reputation: 133

okey, i solve my problem but another problem is here. when i am clicking button more than once it gives me this error. i researched other subjects in web but i dont fix it.

DataTables warning: table id=example - Cannot reinitialise DataTable

function getData(){
    var URL_PREFIX="http://localhost:8983/solr/archiveCore/select?q=strSO:";
    var URL_MIDDLE="AND PackName:";
    var URL_SUFFIX="AND DocType:";
    var strSO="\"" + $("#ngramBoxstrSO").val() + "\"";
    var PackName="\"" + $("#ngramBoxPackName").val() + "\"";
    var DocType="\"" + $("#ngramBoxDocType").val() +"\"";
    var URL=URL_PREFIX + strSO + URL_MIDDLE + PackName + URL_SUFFIX + DocType;
    $.ajax({
        url:URL,
        dataType:'jsonp',
        jsonp : 'json.wrf',
        type :'get',
        cache :false,
        success: function(data){
            var docs=JSON.stringify(data.response.docs);
            var jsonData=JSON.parse(docs);
            var html='';

            $.each(jsonData,function(key,value){
                html+='<tr>';
                html+='<td>'+value.id+'</td>';
                html+='<td>'+value.strSO+'</td>';
                html+='<td>'+value.PackName+'</td>';
                html+='<td>'+value.DocType+'</td>';
                html+='<td>'+value.DocName+'</td>';
                html+='<td class="text-center"><button id="'+value.FilePath+""+'type="button onclick="openDocument(this.id)" class="btn btn-sm" >OPEN</td>';
                html+='</tr>';
            });
            $("#tbody").append(html);
            $(document).ready(function() {
                $('#example').dataTable({
                    /* No ordering applied by DataTables during initialisation */
                    "aaSorting" : [],
                });
            })
        },

    });

}

Upvotes: 0

Snow
Snow

Reputation: 4097

If you look at the docs, you'll see that the callback should be in the format:

Function( String propertyName, Object valueOfProperty )

So, your

function(value,key){

is in the wrong order - the key should be the first argument. Change to

function(key,value){

You might consider using the built-in forEach for iterating over arrays instead, there's no need to use jQuery for that:

jsonData.forEach((value) => {
  // do stuff with value
});

Upvotes: 1

Related Questions