I have a variable amount of tabs with custom forms on them I am trying to post the data from.
my solution was to make an array containing another array for each tab. then post that multidimensional array to my controller.
$("#savetabs").click(function()
{
$("body").addClass("loading");
var $alltabdata = [];
$("#customtabs-holder").children().each(function() {
$tablename = $(this).attr('id');
$thistabdata = [];
$inputs = $(this).find("input");
$.each($inputs, function( index, value ) {
$thistabdata[$(this).attr("name")] = $(this).val();
});
$selects = $(this).find("select");
$.each($selects, function( index, value ) {
$thistabdata[$(this).attr("name")] = $(this).val();
});
$alltabdata[$tablename] = $thistabdata;
});
console.log($alltabdata);
posting = PostTabData($client_id, $alltabdata);
posting.done(function( data ) {
$("body").removeClass("loading");
console.log(data);
alert(data);
});
posting.fail(function() {
$("body").removeClass("loading");
alert( "Failed to post tab info" );
});
});
function PostTabData($client_id, $tabdata) {
console.log($tabdata);
debugger;
return $.post("/CustomTables/Contents/Save",
{
client_id: $client_id,
alltabdata: $tabdata,
});
}
the console.log before the post displays the correct info, But the tab data doesn't end up in my controller and when i check chromes developer tools under the network option it shows the sent data as only consisting of the client_id field
Change your variables to objects:
from [] to {}
In javascript exist only Array with numeric keys. Associative Arrays not exists in javascript.
Check this snippet to fill the difference:
//array
var arr=[];
arr["name"]="Example name";
console.log("Here array log:");
console.log(arr);
//object
var obj={};
obj["name"]="Example name";
console.log("Here object log:");
console.log(obj);
Related
I need to print in console value of key temp but no things displayed
var database=firebase.database();
var ratingRef = firebase.database().ref("sensors/temp/");
ratingRef.orderByValue().on("value", function(data) {
data.forEach(function(data) {
console.log( data.val());
});
});
There's no need for using orderByValue. You have direct reference to the temp child so the data (which is a DataSnapshot) contain the value of temp only.
var ratingRef = firebase.database().ref("sensors/temp");
ratingRef.on("value", function(data) {
console.log(`Temp: ${data.val()}`)
});
I'm trying to query a JSON file in my own server using $.getJSON and then cycling inside the objects. No problem so far, then i have an ID which is the name of the object i want to return, but can't seem to get it right:
var id = 301;
var url = "path/to/file.json";
$.getJSON( url, function( json ) {
var items = [];
items = json;
for (var i = 0; i < items.length; i++) {
var item = items[i];
console.log(item);
}
});
This prints the following in the console:
Now let's say i want return only the object == to id so then i can refer to it like item.banos, item.dorms etc.
My first approach was something like
console.log(json.Object.key('301'));
Which didn't work
Any help would be very much appreciated.
It seems like your response is wrapped in an array with one element.
You can access object properties dynamically via square brackets:
var id = 301;
var url = "path/to/file.json";
$.getJSON(url, function(json) {
console.log(json[0][id].banos);
});
As you have the name of the property in the object you want to retrieve you can use bracket notation. you can also simplify your code because of this:
var id = 301;
//$.getJSON("path/to/file.json", function(json) {
// response data from AJAX request:
var json = {
'301': {
banos: 2
},
'302': {
banos: 3
},
'303': {
banos: 4
},
'304': {
banos: 5
},
};
var item = json[id];
console.log(item);
//});
$.each(items,function(n,value){
if(n==301)
alert(value);
});
In my fiddle http://jsfiddle.net/UGYzW/319/ data is from local source at the moment and is simply called 'data'. How would I pull back data from external values? Please use either http://echo.jsontest.com/key/value/one/two and pull back the value 'mralexgray' from the first login item. Do we need to push out the items to a new array or not?
Finally is it better to use JSONP, JSON or .AJAX?
var data =[
{'label':'Core','value':1},
{'label':' Selectors','value':2},
{'label':'Events' ,'value':3}];
var nameArray = data.map(function(item){
return {value: item.value, label: item.label};
});
$("#meta-area").autocomplete({
source:nameArray,
select: function(e, ui) {
e.preventDefault() // <--- Prevent the value from being inserted.
$("#meta_search_ids").val(ui.item.label);
$(this).val(ui.item.value);
}
});
//alert("this loaded");
Since you created your data variable as an Array, after your ajax response push all the variables to an array. Later on same script will work with the map function
jsfiddle example here
$.ajax({
url:'https://api.github.com/users/mralexgray/repos',
success:function(data) {
var dataArray = [];
for(var i=0;i<data.length;i++){
dataArray.push(data[i]);
console.log(data[i]);
}
var nameArray = dataArray.map(function(item){
return {value: item.owner, label: item.name};
});
$("#meta-area").autocomplete({
source:nameArray,
select: function(e, ui) {
e.preventDefault();
$("#meta_search_ids").val(ui.item.label);
$(this).val(ui.item.value);
}
});
}
});
I'm having a little problem. I'm new in JS and I can't figure it out.
I'm trying to get a list of friends from Facebook. To do that I'm using the user posts and I want to show only the duplicates. My first answer is how to show all the friends from a loop and get only the duplicates (I want only one or two objects) and second is how to pass a var from a function to another function because I want to POST all the infos to a php file.
This is what I did:
function getInfo() {
FB.api(
'/me',
'GET',
{fields: 'first_name'},
function(response) {
friends();
var myname = response.first_name;
$.post(
"file.php",
{ myname:myname,friendsname:friendsname },
function(data) { $("body").append(data); }
);
}
With this function I'm getting my first name.
function friends() {
FB.api(
'/me/posts?fields=likes{id,name}',
{limit:50},
function(response) {
if (response.data) {
$.each(response.data,function(index, posts) {
var posturi = posts.likes;
$.each(posturi.data, function(useri, lista) {
var friendsname = lista.name;
});
});
}
}
);
}
And this is the list with my friends. If I check console.log(friendsname) it gives me all the friends with duplicates. But if I write an innerHTML in that $.each it returns me only 1 object. And how can I pass friendsname from friends() to getInfo() so I can post to that .php file?
That's because you're overwriting the value of innerHTML in every loop of $.each.
Try this:
var posturi = posts.likes;
var names = posturi.data.map(function(item) { return item.name; }).join(', ');
names will contain a comma separated list of names.
I have a form with different groups of checks boxes and trying to pass all the selected values into an array then pass that data into a ajax request.
$('#accessoriesOptions input').each(function(index, value){
if($(this).attr('checked') ){
var newItem =[];
var obj = {};
obj[$(this).attr('value')] = $(this).attr('name'); //I have an hash table with the key-value
wizard.searchArray.push(obj);
}
})
$.ajax({
data : wizard.searchArray
})
I get a wizard.searchArray like :
[0] = {'acc_1' : 'vase'},
[1] = {'acc_3' : 'ceramic'}
I need to create a key-value as I use the key to work out which part of the filtering to use.
The problem
When I do the ajax request, from firebug I see the request as :
/wizard-demo/?undefined=undefined&undefined=undefined
In this case just push add the properties to the obj and use it directly, that's the pair that'll get serialized property when used as the data property, like this:
var obj = {};
$('#accessoriesOptions input').each(function(index, value){
if(this.checked){
obj[this.value] = this.name;
}
})
$.ajax({
data : obj
});
Though this is backwards from a normal <form> submission, if that's what you want it's this instead:
obj[this.name] = this.value;
If you wanted to send the entire form, there's a much shorter/built-in .serialize() method for this:
$.ajax({
data : $("#accessoriesForm").serialize()
});