How do I occupy a textbox with this JSON data - javascript

I am currently using JSON, AJAX, JQuery, and javascript. My JSON look like this
[{[{
"eventName":"Event Name",
"operationgDivision":"Operating Division",
"startDate":"05/14/2014",
"endDate":"05/19/2014",
"siteStartDate":"05/14/2014",
"siteEndDate":"05/19/2014",
"relatedEventName":"Related Event Name",
"relatedEventStartDate":"05/14/2014",
"relatedEventEndDate":"05/19/2014",
"objective":"Objective Info",
"targetAudience":"Target Audience",
"howToReachTargetAudience":"How to reach target audience",
"singleMessageToCommunicate":"Single message to communicate",
"challengesToConsider":"Challenge to consider",
"creativeConsideration":"Creative Consideration",
"creativeConsiderationAttachement":"",
"competitiveConsideration":"Competitive Consideration",
"competitiveConsiderationAttachement":"",
"pOSStartDate":"05/15/2014",
"pOSEndDate":"05/19/2014",
"ID":1
}]
I am currently trying to set the value of the my txtEventName textbox to the eventName data that is within the array in my callback.js file, but I'm having difficulty. Any help would be appreciated.

Related

How Do I See the Output of Changes to JSON

Updated to try to be more clear given the comments (Thank you for comments)
I apologize in advance for this question. I may just not have the vocabulary to properly research it. If I have an array of objects called restaurants stored in my project, for example:
restaurants: [
{
"name": "joes's pizza",
"url": "joespizza.com",
"social properties": {
"Facebook":"fb.com/pizza",
"Instagram":"instagram.com/pizza",
"googlePlus":"pizza+",
"Twitter":"twitter.com/pizza"
}
},
{
"name": "tony's subs",
"url": "tonys.com",
"social properties": {
"Facebook":"fb.com/subs",
"Instagram":"instagram.com/subs",
"googlePlus":"subs+",
"Twitter":"twitter.com/subs"
}
},
{....}
]
I then run a function to add a unique idea to all the objects in the array. The result of console.log(restaurants) is this:
{
"id": 3472,
"name": "joes's pizza",
"url": "joespizza.com",
"social properties": {
"Facebook":"fb.com/pizza",
"Instagram":"instagram.com/pizza",
"googlePlus":"pizza+",
"Twitter":"twitter.com/pizza"
}
},
{
"id": 9987,
"name": "tony's subs",
"url": "tonys.com",
"social properties": {
"Facebook":"fb.com/subs",
"Instagram":"instagram.com/subs",
"googlePlus":"subs+",
"Twitter":"twitter.com/subs"
}
},
{....}
]
I would now like to have this updated array of objects available to look at in my project, via the text editor, as a variable or restaurants.json file. How do I actually see the new modified json array and how do i save it so that i can work with it the same way i did this one above? I am currently doing this in the browser. I can see the results if i log it to the console but I need to be able to work with the new output. Thanks for taking the time to answer.
You can encode/decode JSON with JSON.stringify() and JSON.parse().
Aside from converting to/from JSON, you work with standard JS objects and arrays:
var array = JSON.parse(json_str);
array[0].address = "5th Avenue";
console.log(JSON.stringify(array));
Well, there's really not enough information in your question but I assume a few things:
You've loaded the json data from somewhere and it has been turned into a javascript object.
You've edited the object somehow and wish to convert it back to json and save the changes.
Assuming the above to be true, you just need to serialize the object back to json and submit it back to your server where you can save it in any manner you deem appropriate.
You can serialize the javascript object with JSON.stringify() (see https://stackoverflow.com/a/912247/4424504)
Add the serialized json to a hidden field on the form and submit it.
On the server when processing the form submission, grab the data from the hidden field and do with it what you wish.
Or get it back to the server any way you wish (ajax call, whatever) the key point is to serialize the object to a json string and save it
Hope that helps...

Add data to canvas div's by clicking on rectangle

http://fiddle.jshell.net/f6rLds4g/
I have put the data in div's manual (just typing 1111, 2222, 333, 4444)
My question is is it possible to get data from JSON (which i tried but failed - I think the mistake is on me). I searched for answers on the web but failed to find it.
So if I click on the first rectangle it should give me a data showing in the divs from the JSON file instead of prompt
the JSON is:
{
"name": "1",
"text": "111111",
"version": "1.0.0",
"country": "NL"
}
So instead of just typing 1111 it should put this data in the first div from a JSON file.

Transform Javascript code to JSON and vice versa

I'm working on a small project in which I will work with dgrid dojo, the dgrid must be customized according to the profile of the person if he's administrator or simple user.
The dgrid will be filled from a REST service, and I had the idea to not only fill the dgrid but also customize it with textbox, buttons and checkbox.
For example here is the code of a dgrid with 2 columns whose fields can not be changed:
window.Grid= new StandardGrid({
sort: "abbreviation",
store: stateStore(),
columns: {
abbreviation: 'Abbreviation',
name: 'Name'
}
}, "MyGrid");
the service must verify if the person is an administrator and if so send in JSON Format the settings to add more features to dgrid, by changing "columns" with :
[ editor({
label: "Abbreviation",
field: "abbreviation",
editor: "text",
editOn: "dblclick"
}),
editor({
label: "Name",
field: "name",
editor: "text",
editOn: "dblclick"
})]
I want to send this configuration code in json format.
I want to know if this is workable and if so, how?
Thank you
Since functions aren't a valid part of JSON, you'd need to do some amount of processing to go between JSON and dgrid column plugins. For example, in the case of editor, you could just test each object for presence of the editor property, and if found, apply the editor function around it when you actually create the column structure from your JSON.
I'm not sure regarding Dojo. But if you want to convert a JS Object to JSON then simply:
var jsonString = JSON.stringify(objectGoesHere);

ExtJS manipulating propertygrid source information

I'm using a propertygrid in a similar fashion to a form, rendering a button in the propertygrid tbar to generate an AJAX request, passing the source information to a back-end function that runs a SQL script.
The process of getting the source information to the back-end is proving troublesome, however; while looking at the information in the browser debug console, the result looks like it can be iterated (sample info from console below):
[CNC: "", Cutter_ID: "", Dimension: "", ID: "71",
Internal_Finish: "Standard White", Item_No: "4", Material_ID: "N/A",
Production_Item_Type_ID: "Frame(s)", Production_Notes: "", Qty: "1",
Ready_by_Date: "31 Dec 2014", Survey_Notes: "",
Thickness_Depth: "95", Type_of_Work_ID: "Complete New"]
Selecting individual items can be done, i.e. I can get and set source['Qty'], for instance.
I cannot, however, pass the information in a manageable format; neither can I encode (or decode) the information from the source to JSON.
I would like to at least iterate through the source and append the information to a string, which is then encoded to JSON to pass to the back-end.
How does the propertygrid source work as a data source, since I clearly don't understand it correctly? What method can I employ to generate JSONable information from it?
Property grid's getSource method returns the source as an object, loop through it to append information
Sample fiddle here: https://fiddle.sencha.com/#fiddle/58o

How to generate a dynamic search form for Django?

I wrote a REST API based on Django's ORM. Here's some examples of my REST API
curl "http://example.com/api/pcserver?last_update__lt=2012-06-01 00:00:00&hostname__startswith=xen"
On server side, I uses a queryset.filter() function to process all the GET parameters, and convert the result queryset into a json, then return to user.
Now I'm having trouble convert this API into web based search form.
What I want to have about this magic search form:
dynamic add/delete query parameters using javascript, join them using AND (I don't need OR between these parameters)
provide drop down menu to select the field to be searched based on the model
support for selecting operators e.g. __lt, __gt, __regex
Are there any wheels out there so I don't have to re-invent it? Thank you:-)
#yegle
I'd suggest to use this media type: http://code.ge/media-types/collection-next-json/ it's an extension of the: http://amundsen.com/media-types/collection/format/ media type and you can achieve your goal without problems.
Look at this document:
{"collection": {
"version": 1.0,
"queries": [
{
"href": "http://service.com/my-resource",
"rel": "search",
"prompt": "Enter search string",
"data" : [
{
"name": "query",
"prompt": "Search query",
"required": true
}, {
"name": "gender",
"prompt": "Gender",
"list": {
"default": "female",
"options": [
{"value": "female", "prompt": "Female"},
{"value": "male", "prompt": "Male"}
]
}
}
]
}
]
}}
with "queries" array you can describe several search templates which easily can be transformed to the HTML form. In each query object you can define:
Search URI
Title of the "form"(i.e. prompt); and
Any number of query parameters with different requirements.
As you see from example above there are two query parameters - "query" and "gender", of which "query" parameter can be converted to HTML.input and "gender" parameter to HTML.select.
I hope I correctly understood your question and goal.
I am not sure about django tools.However,you can use MYSQL's full text boolean search to achieve most of the above requirements.Refer the below link and revert back with your comments!
http://dev.mysql.com/doc/refman//5.5/en/fulltext-boolean.html
Have a look at django-filter
Or, in some hacking way, use Django Admin. You could customize a ModelAdmin for the target model and use ChangeList to deal w/ querystring. Take built-in User for example:
from django.contrib.admin.options.IncorrectLookupParameters
from django.contrib.admin.views.main import ChangeList
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
self = UserAdmin(User, None) # or taken from admin.site._registry if the model admin is registered
cl = ChangeList(request, User, self.list_display,
self.list_display_links, self.list_filter, self.date_hierarchy,
self.search_fields, self.list_select_related,
self.list_per_page, self.list_max_show_all, self.list_editable,
self)
# then cl.get_query_set would process querystring and generate the queryset
try:
cl.get_query_set(request)
except IncorrectLookupParameters:
'encounter an invalid lookup'

Categories

Resources