Code Monkey home page Code Monkey logo

jquery-load-json's Introduction

jQuery loadJSON plugin

This is a jQuery plugin that enables developers to load JSON data from the server and load JSON object into the DOM. No templating language is required - this plugin matches DOM elements in the HTML page with a JSON object using a DOM element properties.

Community

Want to contribute to jQuery loadJSON? Awesome! See CONTRIBUTING for more information.

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct to ensure that this project is a welcoming place for everyone to contribute to. By participating in this project you agree to abide by its terms.

Requirements

Requires jQuery 1.7+.

Download

CDN

<script src="https://cdn.jsdelivr.net/gh/kevindb/[email protected]/dist/jquery.loadJSON.min.js" integrity="sha384-ivtX4sn4dcdfHiO4e0/956wIQSerxsy2QZ6EHzdCVLlyGYYjSb8bqdxKY8IsfDGh" crossorigin="anonymous"></script>

or

<script src="https://cdn.rawgit.com/kevindb/jquery-load-json/v1.3.4/dist/jquery.loadJSON.min.js" integrity="sha384-ivtX4sn4dcdfHiO4e0/956wIQSerxsy2QZ6EHzdCVLlyGYYjSb8bqdxKY8IsfDGh" crossorigin="anonymous"></script>

Usage

To use loadJSON plugin two items should be defined:

  • HTML code that will be used as template
  • JSON object that will be used to populate template

HTML template

Template is plain HTML code. Only requirement is that elements that should be populated must have id, name, class, or rel attributes that have matching properties in the JSON object. Example of HTML template is shown below:

		<div id="data">
			<h1 id="Name"></h1>
			<label for="Address">Address:</label>
			<span id="Address"></span>	
			<label for="Contact">Contact by:</label>
			<span id="Contact"></span>
		</div>

In the template h1 tag with id Name, and span tags with ids Address and Contact will be populated with JSON properties.

JSON object

Once HTML template is defined JSON object that will be used to populate template must be defines. Example of object that can populate template shown above is shown in the following listing:

		data = {
					"Name":"Emkay Entertainments",
					"Address":"Nobel House, Regent Centre",
					"Contact":"Phone"
		}  

Object has three properties (Name, Address, and Contact) that will be injected into the template.

Binding JSON object to the template

Once HTML template and JSON object are defined, JSON object can be loaded into the HTML code. This can be achieved using the following line of JavaScript code:

		$('div#data').loadJSON(data);

As a result 'data' object will be loaded into the HTML fragment with id data. Resulting HTML is shown in the following listing:

		<div id="data">
			<h1 id="Name">Emkay Entertainments</h1>
			<label for="Address">Address:</label>
			<span id="Address">Nobel House, Regent Centre</span>	
			<label for="Contact">Contact by:</label>
			<span id="Contact">Phone</span>
		</div>

You can find detailed instructions how the plugin can be used in the following pages:

  1. How to load object into the HTML element where is explained how the JavaScript objects are loaded in the HTML elements
  2. Using loadJSON plugin as a template engine where is explained how this plugin can be used for generating output based on template
  3. Working with HTML forms where is explained how you can load HTML form with JavaScript object

Examples

You can see few live examples where it is shown how loadJSON plugin can be used:

  1. Creating list of elements - in this example is shown how the array of JSON objects can be bound to the simple HTML template,
  2. Showing details of the JSON object - this example shows how details about the single object can be bound to the HTML template,
  3. Populating form with JSON object - this example shows how you can populate form elements from the JSON object,
  4. Loading complex/hierarchical structure this example show how you can generate complex structures using loadJSON plugin,
  5. Generate template for array elements in this example is shown how you can define custom templates for different elements in the array,
  6. Adding load events in this example is shown how you can define on load events,
  7. Loading JSON from the external sites - in this example is shown how you can load JSON data that will be loaded from the external sites - in this case from the Bing Maps web service.
  8. Populating connected drop-downs using the Ajax calls - in this example is shown how you can load subcategories drop-downs when parent category drop-down is changed. Drop-down values are taken from the server-side via Ajax calls.
  9. Populating connected dropdowns using LINQ - in this example is shown how you can load subcategories dropdown when parent category dropdown is changed. Values are stored as a local JavaScript arrays and filtered using the LINQ library.

Contributors

This project has had several previous homes. See CONTRIBUTORS for details.

License

This repository is licensed under the GNU Lesser General Public License v2.1.
See LICENSE for details.

jquery-load-json's People

Contributors

epicfaace avatar fvarg00 avatar kevindb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

jquery-load-json's Issues

Patch for jquery.loadJSON.js version 1.2.5.

1.2.5.mod.1
-----------
    Removed unused mobile option
    Added option to avoid init method execution (to solve problem when used with other jQuery plugins -jQuery Form Plugin-)
    Added option to implement custom onSetValue method (so you can change default behavior for specific fields)
    Replaced $().attr("value", newVal) with $().val(newVal) (to solve problem when used with other jQuery plugins -jQuery Form Plugin-)

Original issue reported on code.google.com by rubensa on 24 Apr 2013 at 9:59

Attachments:

The list.html doesn't work well on my computer?

What steps will reproduce the problem?
1. I downloaded all the project files(include sub folders and files) to my 
folder named "demo"
2. Set "demo" webshare name "loadjson"(I have IIS installed on my computer)
3. In my browser(chrome), "http://localhost/loadjson/list.html" doesn't load 
JSON object into the DOM. 

What is the expected output? What do you see instead?
http://jquery-load-json.googlecode.com/svn/trunk/list.html    works fine.
http://localhost/loadjson/list.html  doesn't work well.

What version of the product are you using? On what operating system?
jquery.loadJSON.js 
Version:     1.1.0.

Please provide any additional information below.


Original issue reported on code.google.com by [email protected] on 30 Apr 2011 at 2:34

Issue with multiple checkboxes and textarea fixed

What steps will reproduce the problem?
1. The version 1.0.0 wasn't working with multiple check boxes
2. The version 1.0.0 wasn't properly populating the text areas

What is the expected output? What do you see instead?

For the checkbox it was limited for one true/false value per field.
The textarea wasn't showing the updated value

What version of the product are you using? On what operating system?
Firefox 2.5

Please provide any additional information below.

I have changed the code in order to fix the issues and add this functionality. 
For the checkboxes to work it needs to be initially (before load values) all 
unchecked - for me it works this way.

Please help testing validating. It would be good if it was possible to publish 
it as a new version.



Original issue reported on code.google.com by [email protected] on 10 Feb 2014 at 9:02

Attachments:

Unique selectors

What steps will reproduce the problem?
1. Adding jQuery callbacks on generated HTML

What is the expected output? What do you see instead?
I am using a <img> template with class n id as "imgUrl", when I do a 
$("imgUrl").slideToggle(); only the first img gets the event. How should I 
identify each element other than using .next()?

What version of the product are you using? On what operating system?
1.1

Please provide any additional information below.


Original issue reported on code.google.com by [email protected] on 3 Jun 2011 at 1:54

No longer works. At all.

1. Downloaded the source from the svn repository.
2. Attempted to load list.html and hierarchy.html into Safari, Firefox, and 
Chrome.
3. Neither page loads the JSON in either browser.

What is the expected output? What do you see instead?
I expect the page to load as it does when viewed on your web site.

What version of the product are you using? On what operating system?
Latest jquery-load-json from repository.
Mac OS X 10.9.4
Safari 7.0.6
Chrome 36.0.1985.143
Firefox 31.0

Please provide any additional information below.
Also tried replacing the very old jQuery (1.6.4) with the current release 
(2.1.1), with no better results.


Original issue reported on code.google.com by [email protected] on 14 Aug 2014 at 4:47

select option not giving desired result

What steps will reproduce the problem?
1. select is not working

What is the expected output? What do you see instead?
i am using "select option", but its not changing the value and keeping the 
default value.

What version of the product are you using? On what operating system?
i am using v1.0

Please provide any additional information below.
i have checked the code and found out, its setting the value using attr(), 
which is not giving desired results in case of select.
So i have changed the case for select as followed - 
case 'select':
     $(element).val(value);
     break;


Original issue reported on code.google.com by [email protected] on 24 Feb 2015 at 3:27

Successor of loadJSON - jquery view engine

Hi,

This is not actually an issue - just a notification.

I decided to resurrect my original loadJSON code and I'm maintaining it here:
https://github.com/JocaPC/jquery-view-engine

I have created a new project because I have removed some features such as selecting elements by rel attribute, I'm binding elements by classes that have bind- prefix, not supporting Url as an argument because I want to separate loading via Ajax from template code, etc. Due to backward compatibility I'm not claiming that the new code is just a new version of loadJSON.

You are free to use this version as your own and maintain it if you want or you can switch to new code. If you continue to use your fork, could you just for test reasons try to put my new version of library into some of your projects and let me know does it works.

Thanks,
Jovan Popovic

Original licence is changed?

Hi Kevin,

Thanks for maintaining this component, I have found several exports of original source, but it seems that you are the only one who is fixing issues. I would like to help if possible.

There might be a licencing problem with your copy.
Licence of original code is dual (BSD or GPL) and you have retained this in the source code; however, you stated on project home that the licence is GPL. If you look at the original slte https://code.google.com/p/jquery-load-json/ you will see that primary licence is BSD (in the left side).
The main difference is that with BSD people can use it in commercial software without need to make it GPL or to use other GLP components only. Primary BSD licence is chosen because we should not have any restriction.
It might be even better to change licence to MIT since it depends on jQuery, which is licensed under MIT licence. This might avoid any misunderstanding such as duality of licences.

Regards,
Jovan Popovic

Textareas that contain the Enter key display blanks

What steps will reproduce the problem?
1. create a form with some text inputs and a text area
2. enter data in the form; make sure to hit the Enter key inside the text area
3. submit the form
4. do some processing on the form input and redisplay the form

What is the expected output? What do you see instead?
I expected to see the inputs displayed on the form. All the inputs are blank.

What version of the product are you using? On what operating system?
1.0.0. Windows, Linux

Please provide any additional information below.
If I view the source for the page I can see the JSON data is constructed 
correctly. Including the CR LF for the Enter key.

Original issue reported on code.google.com by [email protected] on 2 Oct 2013 at 2:08

JSON Array response for multiple input elements causes incorrect GET

What steps will reproduce the problem?
1. Create multiple HTML text inputs such as <input type="text" 
name="element[0][0]"> where element has multiple dimensions (element[3][4])

2. Get a JSON response with a multidimension array such as:
Object {…}
elements: Array[7]
0: Array[6]
  0: "apple"
  1: ""


3. What is the expected output? What do you see instead?
I expect to see the input elements populated with the data from the array (e.g. 
element[0][0] = 'apple').

Instead a GET request is performed which results in a 403 response. The request 
is not to a page but just the current directory and is suffixed with some HTML 
(e.g. 
http://domain/myDir//<!DOCTYPE%20html><!--[if%20lt%20IE%207]>%20%20%20%20%20%20.
..


What version of the product are you using? 1.0.0
On what operating system? Linux Webserver


Please provide any additional information below.
Sorry I don't have a solution.

Original issue reported on code.google.com by [email protected] on 25 Dec 2013 at 1:09

Can we edit the data coming from the server and then display it in the field ?

What steps will reproduce the problem?
1. Get phone number from server in this format: 8888888888 
2. Want to display it in the UI in this format: 888-888-8888
3. But the name tag is binded with the bean class on the server

What is the expected output? What do you see instead?
I see the same display data of the server

What version of the product are you using? On what operating system?


Please provide any additional information below.
Is there a way in which we can manipulate the data thats coming from the server 
and then display it on the UI page

Original issue reported on code.google.com by [email protected] on 1 May 2014 at 7:02

JSLint not supported

Hi,
I use the v. 1.2.5. on Windows 7 Pro.

Please goto http://www.jslint.com/ for validation test code.

This code run fine, but the syntax code is very poor,
and total errors not permit minify of code.

Problem at line 1 character 367: Too many errors. (100% scanned).

Thank you for your work.

Bye,

Claudio


Original issue reported on code.google.com by [email protected] on 27 Nov 2011 at 8:34

Input type color is not getting updated from json data using loadjson

What steps will reproduce the problem?
1. input type color doesnt get populated with the json data
2. <input type="color" name="color_lo_idx" id="color_lo_idx" value="#00ff00"/>
3.

What is the expected output? What do you see instead?
Instead of the field being populated with JSON data it is not populated

What version of the product are you using? On what operating system?
v1.0.0

Please provide any additional information below.


Original issue reported on code.google.com by [email protected] on 1 Aug 2014 at 6:47

Checkbox issue

What steps will reproduce the problem?
1. create form with checkbox
2. load json containing checkbox checked = true
3. load json containing checkbox checked = false

What is the expected output? 
checkbox should be unchecked

What do you see instead?
checkbox still checked

What version of the product are you using? On what operating system?
1.0.0

Please provide any additional information below.
to correct use following code:

                case 'checkbox':
                    if (value)
                        $(element).attr("checked", "checked");
                     else   
                        $(element).removeAttr("checked");
                    break;




Original issue reported on code.google.com by [email protected] on 21 Jun 2012 at 5:07

Input text type= 'date' or type='time' doesn't work (solution)

What steps will reproduce the problem?
1. Add in a form <input id="ffind" type="date">
            <input id="ffint" type="time">
2. load Json

3. verify 

What is the expected output? What do you see instead?


What version of the product are you using? On what operating system?
Jquery Mobile 1.4.5 test with Crome

Please provide any additional information below.

The solution is 

change 
         case 'text':
     case 'hidden':

with:
        case 'text':
    case 'date':
    case 'time':
        case 'hidden':


Original issue reported on code.google.com by [email protected] on 18 Jan 2015 at 9:08

Cannot read property 'constructor' of null

We use this plugin extensively, recently updated to the latest 1.3.3 version to support HTML5 elements too.

While loading the form with the json data if one of the properties has a null value then we get this issue. Never occurred with the older version.

Uncaught TypeError: Cannot read property 'constructor' of null
   at h (jquery.loadJSON.min.js:18)
   at h (jquery.loadJSON.min.js:18)
   at HTMLFormElement.<anonymous> (jquery.loadJSON.min.js:18)

ID Length

What steps will reproduce the problem?
1. An HTML div with an id of 'tmd_charge_flag'
2.
3.

What is the expected output? What do you see instead?
Instead of the field being populated with JSON data it is not populated

What version of the product are you using? On what operating system?
v1.0.0

Please provide any additional information below.
I'm not sure if its the length or a combination of the length and the 
underscores?

Original issue reported on code.google.com by [email protected] on 9 Apr 2014 at 2:22

Load data with null values

We use this plugin extensively, recently updated to the latest 1.3.3 version, and are facing this issue


`Uncaught TypeError: Cannot read property 'constructor' of null
   at h (jquery.loadJSON.min.js:18)
   at h (jquery.loadJSON.min.js:18)
   at HTMLFormElement.<anonymous> (jquery.loadJSON.min.js:18)
   at Function.each (jQuery-2.1.4.min.js:2)
   at n.fn.init.each (jQuery-2.1.4.min.js:2)
   at n.fn.init.a.fn.loadJSON (jquery.loadJSON.min.js:18)
   at loadModal (queueConfiguration.js:82)
   at HTMLAnchorElement.onclick (queueConfigurationList.html:1)`

Select elements are not being selected (with solution).

What steps will reproduce the problem?
1. Have a JSON file with text that matches a select element.
2. Load that JSON file in IE9.
3. The selection is not changed.

What is the expected output? A field from my JSON that matches an option value 
should select that value. What do you see instead?  Nothing is selected.


What version of the product are you using? 1.0.0 On what operating system? 
Windows Vista 64 bit using Internet Explorer 9


Please provide any additional information below.

Change the code to this:
case 'select-one':
case 'select':
    for (var i = 0; i < element.options.length; i++) {
        element.options[i].selected |= element.options[i].value == value;
    }
    break;

case 'text':

Original issue reported on code.google.com by [email protected] on 18 Dec 2013 at 7:59

Enhance to support hidden+checkboxes with identical name

Hi Kevin, first of all, thank you for taking the effort to maintain this library.

I'm currently encountering a scenario where the library does not work. In essence, when I have the following (for the purpose of having a default value instead of "" when no checkbox is selected aka Ms MVC checkbox), jquery-load-json does not work:

<input type="checkbox" name="PreferencePrimary" value="P1" />P1
<input type="checkbox" name="PreferencePrimary" value="P2" />P2
<input type="hidden" name="PreferencePrimary" value="XXX" />

To enhance the library to support this, I've made the following changes.

Add before line 274 (v1.3.1)

else if (element.length > 0 && element.filter('input:hidden').length > 0 && element.filter(':checkbox').length > 0) {
                    element.filter(':checkbox').each(function () {
                        setElementValue(this, obj, name);
                    });
                } else if (element.length > 0 && element[0].type == 'checkbox') {

And line 318-328 (v1.3.1)

var value = obj;
                var type;
                if (element.length > 0) {
                    if (element.filter('input:hidden').length > 0 && element.filter(':checkbox').length > 0) {
                        setElementValue(element.filter(':checkbox').first()[0], obj, name);
                    } else {
                        var i = 0;
                        for (i = 0; i < element.length; i++) {
                            setElementValue(element[i], obj, name);
                        }
                    }
                } else {
                    setElementValue(element, obj, name);
                }

Thanks! Please accept my thanks for reviewing the code and apologies if I'm not adhering to any protocol since I'm still pretty new to Github.

Problem with Hierarchical JSON objects - variable scope issue causing duplication of parent data

First off, Great code - A HUGE time saver!

I have found that some Hierarchical data scenarios cause your code to produce 
unexpected results.  It looks like a bug with the scope of your variables in 
the BrowseJSON() function source lines 120,128, and 140 your for() loops do not 
have var declarations to make them local on the counters (iExist, iCreate and i 
respectively), which when called iteratively can cause some unexpected 
duplication of parent rows due to the lingering of previous values.

Once those are made local, things appear to work okay.

Thanks for the code!

Original issue reported on code.google.com by [email protected] on 11 Sep 2011 at 2:58

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.