Retrieve Unique Column From LocalStorage - javascript

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);
});

Related

How to set values in multi Select from ajax response?

function getNamefromDb(){
var dataString = "metodo=getNames";
$.ajax({
type : "GET",
url : "NameList.do",
data : dataString,
dataType: "json",
async: false,
contentType: "application/json; charset=utf-8",
success : function(data) {
if (data.success == false) {
swal({
title: data.message,
html: true,
});
console.log("Fail"+data.message);
} else {
console.log("Success");
$.each(JSON.parse(data.message), function(key, value) {
$('#selectBoxId').append($("<option/>", {
value: key,
text: value
}));
});
$("#selectBoxId").multiselect('rebuild');
}
}
});
}
<select id="selectBoxId" multiple="multiple" onclick="javascript:getNamefromDb();">
Here I am receiving the json response from the ajax call. And then I had to set those values to the select box. Is it possible?
But anyhow the function getNamefromDb() is not called. Please tell what's wrong in it.
You can do like this:
let dropdown = document.getElementById('selectBoxId');
if (data.success){
const data = JSON.parse(request.responseText);
let option;
for (let i = 0; i < data.length; i++) {
option = document.createElement('option');
option.text = data[i].value;
option.value = data[i].key;
dropdown.add(option);
}}
$('#selectBoxId').change(function() {
var dataString = "{metodo:getNames,value:$(this).val()};
$.ajax({
type: "GET",
url: "NameList.do",
data: dataString,
dataType: "json"
async: false,
success: function(response) {
data = response;
return response;
},
if (data.success == false) {
swal({
title: data.message,
html: true,
});
console.log("Fail" + data.message);
} else {
console.log("Success");
$.each(JSON.parse(data.message), function(key, value) {
$('#selectBoxId').append($("<option/>", {
value: key,
text: value
}));
});
$("#selectBoxId").multiselect('rebuild');
}
}
});
}

How to apply filter on Hyperlink column in sharepoint list using rest (with CAML)?

I want to get items in which siteUrl contains value from the variable 'project_url'
var project_url = _spPageContextInfo.webServerRelativeUrl;
var queryText = "<Where><Contains><FieldRef Name='SiteUrl' /><Value Type='URL'>" + project_url + "</Value></Contains></Where>";
var viewXml = '<View><Query>' + queryText + '</Query></View>';
var webserviceUrl = _spPageContextInfo.siteAbsoluteUrl + "/_api/web/Lists(guid'eb0ba220-73e8-428f-b848-92b606aa74d8')/items?$select=ID,Title,MembersId,AccessTYpe,OrganizationalUnitId,SiteUrl";
var queryPayload = {
'query' : {
'__metadata': { 'type': 'SP.CamlQuery' },
'ViewXml' : viewXml,
}
};
$.ajax({
url: webserviceUrl,
method: "POST",
data: JSON.stringify(queryPayload),
headers: {
"X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
"Accept": "application/json; odata=verbose",
"content-type": "application/json; odata=verbose"
},
success: function (data) {
console.log(data);
},
error: function (data) {
console.log("AllTasks Error -", data);
}
});
I'm getting this error:
'"{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"An entry without a type name was found, but no expected type was specified. To allow entries without type information, the expected type must also be specified when the model is specified."}}}"'
You need to hit the GetItems endpoint.
Also, change the CAML query to use BeginsWith instead of Contains.
Modify your code as below:
var project_url = _spPageContextInfo.webAbsoluteUrl;
var queryText = "<Where><BeginsWith><FieldRef Name='SiteUrl' /><Value Type='URL'>" + project_url + "</Value></BeginsWith></Where>";
var viewXml = '<View><Query>' + queryText + '</Query></View>';
var webserviceUrl = _spPageContextInfo.siteAbsoluteUrl + "/_api/web/Lists(guid'eb0ba220-73e8-428f-b848-92b606aa74d8')/GetItems";
var queryPayload = {
'query' : {
'__metadata': { 'type': 'SP.CamlQuery' },
'ViewXml' : viewXml,
}
};
$.ajax({
url: webserviceUrl,
method: "POST",
data: JSON.stringify(queryPayload),
headers: {
"X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
"Accept": "application/json; odata=verbose",
"content-type": "application/json; odata=verbose"
},
success: function (data) {
console.log(data);
},
error: function (data) {
console.log("AllTasks Error -", data);
}
});

How do i split ajax return values using jquery

I want to split the ajax returned values using jQuery.
Here is my code:
var url = "/StudentProgress/GetStudProgDet/";
$.ajax({
url: url,
data: { currentAcadYr: iAcademicYearText, currentSem: iSemesterText },
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "GET",
success: function (data) {
var result = $(data).text().split(':');
var ProgAcadYearCode = result[0].ProgAcadYearCode;
var productSize = result[1];
// alert(data.ProgAcadYearCode);
//$("#ToProgressAcademicYearId option").filter(function () {
// return this.text == testsem;
//}).attr('selected', true);
},
error: function (reponse) {
alert("error : " + reponse);
}
});
I got a result like this:
data = {
success: true,
progAcadYearCode: 20172018,
progAcadYearId: 17,
progressSemId: 47,
progressSemNo: 2
}
How do I extract the desired values from the JSON using jQuery?
Based on data what you shown,you have to directly fetch it's properties like below:-
success: function (data) {
console.log(data.success);
console.log(data.progAcadYearCode); //and so on
},

Looping through two JSON arrays Ajax

I'm trying to get data from a Json file using the id from a previous previous ajax call looping through the the second array based on the id gotten from the first
I have tried
$(document).on('click', '.stories', function(e) {
e.preventDefault();
var request = $.ajax({
url: 'includes/functions.php?job=front_title',
cache: false,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'get'
});
request.done(function (output) {
if (output.result === 'success') {
var n = output.data[0].title_count;
$('.blog').empty();
for (var i=0; i<n; i++) {
var storytitle = output.data[i].story_view;
var id = output.data[i].titleID;
var request2 = $.ajax({
url: 'includes/functions.php?job=story_episodes',
cache: false,
data: 'id=' + id,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'get'
});
request2.done(function (output2) {
if (output2.result === 'success') {
var n2 = output2.data[0].episode_count;
for (var i=0; i<n2; i++) {
var titles = output2.data[i].title;
console.log(storytitle + " " + titles);
}
}
else {
console.log('faileds');
}
});
}
} else {
console.log('failed');
}
});
});
The storyTitle has a single value and loops through all the titles when i check my console.
I tried debugging and found out the second for-loop was only executed once, after executing request2.done, it goes back to the first for-loop after the first has finish all its loop, it executes the second for-loop.
I don't know where am missing it.I need help with this.
Finally solved the problem...Changed my code to...
$(document).on('click', '.stories', function(e) {
e.preventDefault();
var request = $.ajax({
url: 'includes/functions.php?job=front_title',
cache: false,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'get'
});
request.done(function (output) {
if (output.result === 'success') {
var n = output.data[0].title_count;
var jsonArray = $(jQuery.parseJSON(JSON.stringify(output.data))).each(function() {
var id = this.titleID;
var CLASS = this.story_view;
var request2 = $.ajax({
url: 'includes/functions.php?job=story_episodes',
cache: false,
data: 'id=' + id,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'get'
});
request2.done(function (output2) {
if (output2.result === 'success') {
var jsonArray2 = $(jQuery.parseJSON(JSON.stringify(output2.data))).each(function() {
var id2 = this.id;
console.log(id + " " + id2);
})
}
})
})
} else {
console.log('failed');
}
});
})
And it worked fine....thanks to Swapnil Godambe

Passing array to function - array is empty inside function

I tried almost everything, I have two codes:
First one, pokaz_ofe, calls correct function - "generuj_oferte" in ajaxQuery() function, but I cant access postTab array in ajaxQuery(). Without delete postData it was using old array from another call (other that in "pokaz_ofe" function).
function ajaxQuery(postData, func_var) {
$.ajax({
type: "POST",
url: "ajax/pokaz_lst.php",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(postData),
success: func_var
})
postData = [];
delete postData;
}
function pokaz_ofe(id,tab) {
if(!tab) {
var postTab = [
{ "id": id }
]
}
else {
var postTab = [
{ "id": id },
{ "listaId": tab}
]
}
ajaxQuery(postTab, generuj_oferte);
}
function generuj_oferte(res) {
$(document).ready(function(){
$('#modal-oferta').html( res );
});
}
And this is the different code which works, but I want it more flexible:
function pokaz_ofe(id,tab) {
if(!tab) {
var postData = [
{ "id": id }
]
}
else {
var postData = [
{ "id": id },
{ "listaId": tab}
]
}
$.ajax({
type: "POST",
url: "ajax/pokaz_ofe.php",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(postData),
success: generuj_oferte
})
}
It was rly stupid one mistake... now it is:
function ajaxQuery(filename, postData, func_var) {
$.ajax({
type: "POST",
url: "ajax/" + filename + ".php",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(postData),
success: func_var
})
postData = [];
delete postData;
}
It was all about filename.

Categories