I want to compare a set of days in an array with all the dates in the calendar.
This is my code what I have written. How should I pass the array to dayrender function?
Method for getting the dates
function GetFoodDetails() {
$.ajax({
url: 'Food/getFoodDetails',
data: {},
dataType: 'json',
contentType: "application/json; charset=utf-8",
type: 'GET',
success: function (result) {
myArray = new Array(result.length);
for (var index = 0; index < result.length; index++) {
debugger;
// myArray[index] = Date(result[index].Date.split('(')[1].split(')')[0].toString());
var date = new Date(parseInt(result[index].Date.substr(6)));
myArray[index] = date;
} //end of loop
},
error: function (response) {
alert('eror');
console.log(response);
}
});
}
in Fullcalendar plugin
dayRender: function (date, cell) {
myArray.forEach(function (item) {
if (date._d.getDate() == item.getDate() && date._d.getMonth() == item.getMonth())
{
$(cell).toggleClass('selected');
}
});
}
Well, jquery Data Method can help you here:
Add, $('body').data( "date_global", myArray); to store your date array.
You can try this technique like:
function GetFoodDetails() {
$.ajax({
url: 'Food/getFoodDetails',
data: {},
dataType: 'json',
contentType: "application/json; charset=utf-8",
type: 'GET',
success: function (result) {
myArray = new Array(result.length);
for (var index = 0; index < result.length; index++) {
debugger;
// myArray[index] = Date(result[index].Date.split('(')[1].split(')')[0].toString());
var date = new Date(parseInt(result[index].Date.substr(6)));
myArray[index] = date;
} //end of loop
$('body').data( "date_global", myArray);
},
error: function (response) {
alert('eror');
console.log(response);
}
});
}
For, fetching this array anywhere in the code write: $('body').data( "date_global" )
dayRender: function (date, cell) {
console.log($('body').data( "date_global" ));
myArray.forEach(function (item) {
if (date._d.getDate() == item.getDate() && date._d.getMonth() == item.getMonth())
{
$(cell).toggleClass('selected');
}
});
}
Related
The following code is not storing values in the Array
var checkListIdForEmail= new Array();
var checkListNameforEmail;
function getCheckListIdAndName() {
$.ajax({
type: "GET",
url: 'URL/' + 12464,
dataType: 'json',
contentType: false,
processData: false,
success: function (result) {
for (var i=0; i< result.length;i++) {
$('#checkListIdForEmail').val(result.checklistDetailId[i]);
}
// alert("Success");
},
error: function (error) {
alert("Errror while getting header values");
}
});
}
Can anyone please let me know what needs to store all data in an array..
Thank You
I would suggest on your success callback, do this instead.
success: function (result) {
checkListIdForEmail = result;
},
since result is already an array
Maybe this is what you want to store to the checkListIdForEmail array:
for (var i=0; i< result.length;i++) {
checkListIdForEmail[i] = result[i].checklistDetailId;
}
$('#checkListIdForEmail').val(checkListIdForEmail);
I am trying to use bootstrap typeahead.
I am having trouble handling which item has been selected.
Below is the code I have so for
var bindto = $(".search.typeahead");
bindto.typeahead({
source: function (query, response) {
var map = {};
var advicesearchList = [];
return $.ajax({
url: "/Advice/AutoComplete",
type: "GET",
cache: false,
data: { querystring: query },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$.each(data, function (i, result) {
map[result.Title] = result;
advicesearchList.push(result.Title);
});
response(advicesearchList);
}
});
}
}).on("typeahead:selected", function (e, o) {
console.log("item selected");
});
What would be the correct way to detect what the user has selected from one of the items in the list
thanks
Try using the afterSelected option:
var bindto = $(".search.typeahead");
bindto.typeahead({
source: function (query, response) {
//your code...
}, afterSelect: function (data) {
//print the data to developer tool's console
console.log(data);
}
});
Docs
I want to retrieve unique column data from the local storage, but i dont know how to do it. I have stored whole data from server in local storage.
Retrived data img
$("#btnlogin").click(function(){
window.e = $("#mob").val();
window.p = $("#key").val();
$.ajax({
url: "URL",
type: "GET",
datatype: "json",
data: { type:'login', phone: e, name: p },
ContentType: "application/json",
success: function(response)
{
alert(JSON.stringify(response));
window.dataToStore = JSON.stringify(response);
window.localStorage.setItem('someData', dataToStore);
},
error: function(err)
{
alert(JSON.stringify(err));
}
})
});
$("#x").click(function()
{
var localData = JSON.parse(localStorage.getItem('someData'));
alert(""+localData);
});
You should iterate the given data and store each value in a single row
Then you will be able to access to each value with the data key.
$("#btnlogin").click(function(){
window.e = $("#mob").val();
window.p = $("#key").val();
$.ajax({
url: "URL",
type: "GET",
datatype: "json",
data: { type:'login', phone: e, name: p },
ContentType: "application/json",
success: function(response)
{
var data = JSON.parse(response);
$.each(data, function(index, val) {
window.localStorage.setItem(index, JSON.stringify(val));
});
},
error: function(err)
{
alert(JSON.stringify(err));
}
})
});
$("#x").click(function()
{
var localData = [];
for (var i = 0; i < localStorage.length; i++) {
var row = JSON.parse(localStorage.getItem(localStorage.key(i)));
localData.push(row);
}
console.log(localData);
});
I have a set of cascading-option dropdown boxes that I'm trying to populate via javascript & jquery. In the $(document).ready section I'm trying to implement the jquery .done() function to ensure that each prior function is completed before the next run starts running, but it doesn't seem to be working:
$(document).ready(function () {
$.when(getSchoolYears()).done(function () {
$.when(getDistricts()).done(function () {
$.when(getAreas()).done(function () {
$.when(getSchoolTypes()).done(function () {
$.when(getSchoolLevels()).done(function () {
$.when(getGrades()).done(function () {
$.when(getEvents()).done(function () {
$.when(getResolutions()).done(function () {
$.when(getSchools()).done(function () {
loadCharts();
})
})
})
})
})
})
})
})
})
})
Focusing on the first two functions, getSchoolYears() & getDistricts(), I put consle.log statements into these functions, and the message for getDistricts() gets logged before the message for getSchoolYears()
function getSchoolYears() {
var data = [];
var param = {};
$.ajax({
type: "POST",
contentType: "application/json; charset=UTF-8",
url: "../WebServices/myLocations.asmx/get_SchoolYears",
data: JSON.stringify(param),
dataType: "json",
success: function (msg) {
var data = [];
data.push(msg.d);
$('#ddSchoolYear li').remove();
for (var i = 0; i <= data.length; i++) {
$('#ddSchoolYear').append(data[i]);
}
$('#hidSchoolYear').val(ddSubmitted('#ddSchoolYear input', '#lblYear'));
console.log('finished');
}
});
}
function getDistricts() {
console.log($('#hidSchoolYear').val());
var param = { "endYear": $('#hidSchoolYear').val() };
return $.ajax({
type: "POST",
contentType: "application/json; charset=UTF-8",
url: "../WebServices/myLocations.asmx/get_Districts",
data: JSON.stringify(param),
dataType: "json",
success: function (msg) {
var data = [];
data.push(msg.d);
$('#ddDistrict li').remove();
for (var i = 0; i <= data.length; i++) {
$('#ddDistrict').append(data[i]);
}
}
});
}
Am I not understanding how the .done() function works? Is there a way to ensure that one functions completes before the next one begins?
I have a JSON from a PHP file and I want to send that output to another PHP file. This is my JavaScript:
var data_1;
$.ajax({
type:"POST",
url:"sd.php",
success:function(result){
data_1 = result;
test(data_1);
var st = JSON.stringify(data_1);
$.post('get.php',{q:st},function(data){
window.location = "get.php";
});
}
});
And my PHP file to store the JSON:
<?php
$obj = json_decode($_POST['q']);
echo $obj;
?>
But it outputs nothing. What should I do? Please help.
You may try this i wrote for you, but its not tested
/**
* Created by vladimirnikolic on 3/24/14.
*/
$('#submit').click(function(e){
e.preventDefault();
var form_data = $('#your_form').serializeArray();
var submit_data = serializedFormToDTO(form_data);
$.ajax({
url: 'sd.php',
type: 'POST',
dataType: 'json',
data: submit_data
})
.done(function(xhr) {
$.post("get.php", {
q: submit_data
},
function (data) {
// handle data here
// console.log(data);
}, 'json');
}
)
.fail(function(xhr) {
var response_text = $.parseJSON(xhr.responseText)
console.log(response_text);
})
});
function serializedFormToDTO (data, json) {
json = (typeof json !== 'undefined') ? json : false;
if ((json !== true) && (json !== false) ) {
console.log('invalid value of second parameter (should be true/false for json output)');
return false;
}
var result = {};
for (var i = data.length - 1; i >= 0; i--) {
result[data[i].name] = data[i].value;
}
if (json === true) {
result = JSON.stringify(result);
}
return result;
}
$.ajax({
url: 'sd.php',
type: 'POST',
data: JSON.stringify(data_1), // data_1 is a javascript object here
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function(returned_data) {
alert(returned_data);
}
});