Home

Loading JSON in the list

JQuery loadJSON plugin enables you to load JSON array of objects and generate list of HTML elements based on the template. For templating is not used any specific templating language - as a template is used basic HTML that will be linked to the JSON object using standard HTML attributes. This plugin binds JavaScript object properties with HTML elements in the template using id, class, name, or rel attributes.

Live Example

HTML generated using loadJSON plugin is shown below:


In this example array of JavaScript objects is used as a data source for the list of elements. Details about implementation is described in the sections below.


Implementation

JQuery loadJSON plugin enables you to load array of JavaScript objects into the list.

In the HTML should be placed one item that represents a template item that will be populated with elements from the JSON array. Example is shown below:

<ul>
	<li> 
		<a href="details.html" class="ID">
		<span class="Name"></span></a> <span class="Address"></span>
	</li>
</ul>
In this template are placed three elements where properties ID, Name, and Address of some JSON object will be populated. loadJSON plugin will take JSON array, bind properties of the elements of the array to template and replicate template item bound with each element in the array.
Once HTML template item is prepared it is required to provide a JSON array that will be used to populate template item shown above. Example of JSON array used in this example is shown below:
[
    {
	"ID": 17,
        "Name": "Emkay Entertainments",
        "Address": "Nobel House, Regent Centre"
    },
    {
        "ID": 18,
        "Name": "The Empire",
        "Address": "Milton Keynes Leisure Plaza"
    },
    {
        "ID": 19,
        "Name": "Asadul Ltd",
        "Address": "Hophouse"
    },
    {
        "ID": 20,
        "Name": "Star Records",
        "Address": "St Danny Street"
    },
    {
        "ID": 21,
        "Name": "Ashley Mark Publishing Company",
        "Address": "1-2 Vance Court"
    }
]
	
This array contains elements with properties ID, Name, and Address that will be bound to the elements in the HTML template.
When template and JSON are prepared they should be bound toghether in order to generate output. Template is defined in the LI HTML element, and under assumption that JSON array is placed into the "list.js" file, the following code generates output HTML using template and JSON array:
            $('li').loadJSON('list.js');