Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi
i have a json file called employee.json, it is saved in my solution explorer, i want to show this json data into a html table using javascript or jquery, sample data in json file is like :-

[{"Address_Line_1":"331 Bangalore","Address_Line_2":"","Address_Line_3":"","Address_Line_4":"","AlternateAddresses":[],"City":"Bangalore","Country":"India","Distance":"0","FormattedAddress":"3s1 Bangalore, Bangalore, Bangalore 66","GeoCode":"POINT (-34.134345334349056 20.3254345354” }]

if possible pls provide some samples pls

thanks in advance
Posted
Updated 3-Jul-18 1:28am

1 solution

Hi nrkhi401,

If you are strictly looking for binding the data to a html table, go through the following.

the following is the ajax call method for your webservice. Please make necessary changes as per your requirements to suit your application.

JavaScript
$.ajax({
     type: "POST",
     contentType: "application/json; charset=utf-8",
     url: "~/YourWebserviceFile.asmx/YourWebServiceName",
     data: "{}",
     dataType: "json",
     success: function (data) 
              {
                   //Write functionality to display data
                   $("#bookContainer").append(CreateTableView(data.d, 'CssClassName', true));
              },
              error: function (result) {
                   alert("Error: " + result);
              }
        });


Now, use the next script for binding the data to a table. The Html code looks as follows:

HTML
<table id="bookContainer"></table>


The last part is to call the CreateTableView method which would create the rows and columns and will append them to the table (with id="bookContainer"). This method is called in the success property of the Ajax method.

This method has 3 parameters. First is the json data from webservice, second is the class name for adding any style to the table. Third one is to show/hide the header.

JavaScript
// This function creates a standard table with column/rows
// Parameter Information
// objArray = Anytype of object array, like JSON results
// theme (optional) = A css class to add to the table (e.g. <table class="<theme>">
// enableHeader (optional) = Controls if you want to hide/show, default is show
function CreateTableView(objArray, theme, enableHeader) {
            
    // set optional theme parameter
    if (theme === undefined) {
        theme = ''; //default theme
    }

    if (enableHeader === undefined) {
        enableHeader = true; //default enable headers
    }

    // If the returned data is an object do nothing, else try to parse
    var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;

    var str = '<table class="' + theme + '">';

    // table head
    if (enableHeader) {
        str += '<thead><table><tbody><tr>';
        for (var index in array[0]) {
            str += '<th scope="col">' + index + '</th>';
            break;
        }
        str += '</tr></tbody></table></thead>';
    }


    // table body
    str += '<tbody>';
    for (var i = 0; i < array.length; i++) {
        str += (i % 2 == 0) ? '<tr class="alt">' : '<tr>';
                
        for (var index in array[i]) {
            if (array[i][index] == null) {
                str += '<td> </td>';
            }
            else {
                str += '<td>' + array[i][index] + '</td>';
            }                    
        }
        str += '</tr>';
    }
    str += '</tr></tbody>'
    str += '</table>';
    return str;
}
</table>


Hope it helps you. Happy coding..!

Thank you,
Vamsi
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900