Is there anyway to update an exising array defined inside HTML? - javascript

There is a static array which should be dynamically updated based upon click event.
I am able to get the dynamic value in an array format using Ajax but finding difficulties in setting it to the existing variable.
Ajax returns the below array
echo json_encode($arry);
// ["2","1","1","0","1","0"]
$.post( "ajaxcall.php", { ids: id })
.done(function( returnedArray){
//returnedArray looks like ["2","1","1","0","1","0"]
datasets: [{
//data: [10,12,33,50,12,34]
data: returnedArray
}]
},
But after the click event, returnedArray does not get interpreted to place the value.
Bacialy the result should look like below:
data: ["2","1","1","0","1","0"]

Use the assignment operator instead of :
datasets = [{
//data: [10,12,33,50,12,34]
data: returnedArray
}]

Related

finding nested object value in java script

JSON file from URL
{
"1": [
{
"actions": ["OUTPUT:2" ],
"idle_timeout": 0,
"cookie": 0,
"packet_count": 2,
"hard_timeout": 0
}
}
JavaScript
function myFunction() {
//alert("INTo function");
$.ajax({
url: "http://127.0.0.1:3000/flow",
cache: false,
success: function(data) {
$("#flow").append(data["1"].actions.OUTPUT[i]);
$("#flow").append(data["1"].idle_timeout);
$("#flow").append(data["1"].cookie);
$("#flow").append(data["1"].packet_count);
$("#flow").append(data["1"].hard_timeout);
}
});
}
This is the JavaScript code which I have used it, to find values of the object inside the nested JSON response coming from a URL.
Your sample does not look like valid json. Since the value of "1" is an array, you should try to access it via the index. e.g.$("#flow").append(data["1"][0].idle_timeout)
The code used to find the values inside the nested JSON object is actually incorrect.
You must use data["1"][0].actions[0].OUTPUT to retrieve the value 2.

jQuery JSON converting time values to numeric values

I'm having a problem with a JSON conversion by jQuery AJAX.
Ajax Setup
$.ajax({
url: 'ajax/get_available_schedule.php',
type: 'GET',
dataType: 'json',
data: {field: field, day: day, hour: inicio},
})
I have this response from server
{"success":true,"ranges":[["22:00","22:30"],["22:30","23:00"],["01:30","02:00"],["02:00","02:30"]],"close":"03:30"}
But at the moment I do a 'console.log' of the response in the Ajax success, it will convert to this:
close: "03:30"
ranges: Array(2)
0: (2) [5400, 9000]
1: (2) [79200, 82800]
success: true
I don't know why the 'ranges' array are converted to numbers, I want that be likely the response, an array of 2 strings
I try to Google the problem but I don't find anything, or I don't know how to search it.
I know I've seen a duplicate for this, but I can't find it at the moment, so here goes.
var data = { ranges: [ "A", "B", "C" ] };
//log the data element to the console
console.log( data );
//set the anotherVariable to point to the ranges array
//this does NOT create a separate element
//both variables point to the same array in memory
var anotherVariable = data.ranges;
//this changes both variables, because as said before, they point to the
//same array
anotherVariable[1] = "weee";
/*
If you look at your actual browser, you will see that the consoled
data has ranges of "A", "weee", "C". This is because your browser
is showing you the value of the element, up to date. Not only of
when you logged it.
*/
The problem was the disabled_hours var in timepicker library option declared later in the code.
It changes the value of disabled_hours, and I can't looking in the console until after it is changed.
$.ajax({
url: 'ajax/get_available_schedule.php',
type: 'GET',
dataType: 'json',
data: {field: field, day: day, hour: inicio},
})
.done(function(dataSCHEDULE) {
console.log(dataSCHEDULE);
// debugger;
if (dataSCHEDULE.success) {
disabled_hours = dataSCHEDULE.ranges;
console.log(disabled_hours);
close_hour = dataSCHEDULE.close;
}
else {
console.warn('API ERROR -> Getting available hours');
}
})
.fail(function(error) {
console.error('API ERROR -> Getting available hours');
console.log(error);
});

Meteor : Iterate through array of Objects inside an Object

I'm currently trying to iterate through an array of Objects inside an Object.
I have my Collection "Proposals", and this is the corresponding Schema :
Schemas.ProposalsSchema = new SimpleSchema({
'content': {
type: String,
max: 140
},
'parties': {
type: [Object],
autoform : {
type: "select-multiple"
}
},
'parties.$._id': {
type: Object,
optional: true
},
'parties.$._id._str': {
type: String
},
'parties.$.name': {
type: String
}
});
I would like to iterate through the array of parties inside one of my template. I tried this :
{{#each proposals}}
<p>{{content}}</p>
<p>{{#each parties}} {{this.name}} {{/each}}</p>
{{/each}}
The content is displayed, but not the name of the different parties. Here's my template helper :
Template.proposalsIndex.helpers({
proposals: () => Proposals.find().fetch()
});
Do you know what do i do wrong?
Thank you in advance.
The code you posted looks ok.
Things you need to check are:
Is the "parties" field populated in a database? (you can run Proposals.find().fetch() in a meteor shell to check it).
If not, find out why it is not populated.
Is the "parties" field published to client? (you can run Proposals.find().fetch() in a browser console and see the results).
If not, check your publication.

In Select2, how do formatSelection and formatResult work?

I'm using Select2 (http://ivaynberg.github.io/select2/) to have an input field of a form (let's say its id is topics) to be in tagging mode, with the list of existing tags (allowing the user to choose some of these tags, or to create new ones) being provided by an array of remote data.
The array (list.json) is correctly got from my server. It has id and text fields, since Select2 seems to need these fields. It thus looks like this:
[ { id: 'tag1', text: 'tag1' }, { id: 'tag2', text: 'tag2' }, { id: 'tag3', text: 'tag3' } ]
The script in the HTML file looks like this:
$("#topics").select2({
ajax: {
url: "/mypath/list.json",
dataType: 'json',
results: function (data, page) {
return {results: data};
},
}
});
But the input field is showing "searching", which means it's not able to use the array for tagging support.
In the script with Select2, I know I have to define formatSelection and formatInput, but I'm not getting how they should work in my case, although I have read the Select2 documentation... Thank you for your help!
You need to add the function like explained here. In your example:
function format(state) {
return state.text;
}

dhtmlxgrid - load ( url ) for changing data set

I am using dhtmlxgrid and calling the function
mygrid.load ( url, "json" ) .
The url points to a file which has the data shown below.
The data returned by the url is of format
data = {
rows: [{
id: 1001,
data: ["200", "The Rainmaker", "John Grisham", "101", "0" ]
}, {
id: 1002,
data: ["1002", "A Time to Kill", "John", "90", "110" ]
}]
}
This mygrid.load is called repeatedly in loop every few seconds.
The first time, the data is correctly loaded in the mygrid. The next times also when the data is changed, it's correctly updated in the grid.
The problem is that the new records and deletes are not seen on the grid. Any idea how this method can be done in this method.
I am trying to find something equivalent of mygrid.updateFromXML - but for json with insert/deletes.
http://docs.dhtmlx.com/doku.php?id=dhtmlxgrid:api_method_dhtmlxgridobject_updatefromxml ( but for json )
To reload data you need to use something like next
grid.clearAll(); //clear old data first
grid.load(url, "json");

Categories

Resources