items is a variable i used in below function...
items value is
[{"daLevel":"DA0","daName":"Da Name 0"},{"daLevel":"DA1","daName":"Da Name 1"},{"daLevel":"DA2","daName":"Da Name 2"},{"daLevel":"DA3","daName":"Da Name 3"},{"daLevel":"DA4","daName":"Da Name 4"},{"daLevel":"DA5","daName":"Da Name 5"},{"daLevel":"DA6","daName":"Da Name 6"},{"daLevel":"DA7","daName":"Da Name 7"},{"daLevel":"DA8","daName":"Da Name 8"},{"daLevel":"DA9","daName":"Da Name 9"},{"daLevel":"DA10","daName":"Da Name 10"}]
I need to display daName values in a select box as dropdown.
I am not able to get daName values from items var.
Any suggestion would be of gr8 help.
Thanks in advance
function notifyDa(excessId) {
alert("notified");
var html = "<table><tr><td align='center' colspan='2'> Excess Notification </td></tr><tr><td>Select DA Holder</td><td><select id='daList'>";
var ctx = '${contextPath}';
var queryUrl = ctx + "/excessList.htm?getDaList=true";
$.ajax({
url : queryUrl,
type : "POST",
dataType : "text",
success : function(result) {
alert(result);
**var items = result;
alert("items *** "+items);
alert("items[0] *** "+items.daName[0]);**
$('#notifyDiv').empty();
$('#notifyDiv').html(html);
$("#pop").click(function() {
$("#notifyDiv").fadeIn(1000);
if (!$("#notifyDiv").is(':visible')) {
return;
}
});
$("#notifyDiv").css({
left : ($(window).width() - $('#notifyDiv').width()) / 2,
top : ($(window).width() - $('#notifyDiv').width()) / 7,
position : 'absolute'
});
},
error : function() {
}
});
}
You can try this
result = jQuery.parseJSON(result); // as dataType is Text
items[0].daName;
You need to change your Datatype to JSON, then you can use jQuery .each() function for parsing the JSON you are getting. Something like this:
$(items).each(function(index,element){
alert(element.daName);
});
This will iterate through all the objects and gives you the required fields.
Related
I need to get a new file name using an ajax call so I can use it in another function. This is the original call for the new filename I am trying to retrieve from the setup_file function. The file array has all the data I need including the original file name.
var newfilename = setup_file(file);
But when I try to return the data I need from said function, it doesn't work.
function setup_file(file) {
var newfilename;
var newtitle = jQuery("#choosetitle").val();
var aspect = jQuery("#chooseaspect").val();
var uploadlanguage = jQuery("#uploadlanguage").val();
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
type : 'POST',
async: false,
datatype : 'JSON',
data : {action : 'process_uploads',fileinfo: file,filetitle : newtitle, aspect : aspect, uploadlanguage : uploadlanguage},
success : function(data){
var json = JSON.parse(data);
var newfilename = json['filename'];
alert(newfilename);
//this alerts fine.
}
});
return newfilename;
//thisreturnsnothing, however if I change it to return 'Whatever'; it does return Whatever.
}
You created, same variable again in the success block, that cause the outer variable empty. Change the
var newfilename = json['filename'];
to
newfilename=json['filename']
In the success block.
I am using this jQuery plugin MagicSearch
How can I programatically select a value (by ID for example)?
This is what I have. After a careful analysis of their callback functions, I have devised a method to extract the ID and Name from my search list.
Please find the code to my attempt:
<script>
var idList="";
var nameList = "";
$(function () {
var dataSource = "[{\"id\":1,\"name\":\"test\"},
{\"id\":2,\"name\":\"example\"},{\"id\":3,\"name\":\"image\"},
{\"id\":4,\"name\":\"election\"},{\"id\":5,\"name\":\"IPL\"},
{\"id\":6,\"name\":\"Fashion\"},{\"id\":7,\"name\":\"Movies\"},
{\"id\":8,\"name\":\"Pollution\"},{\"id\":9,\"name\":\"Modi\"},
{\"id\":10,\"name\":\"Rahul\"},{\"id\":11,\"name\":\"Cricket\"},
{\"id\":12,\"name\":\"Market\"},{\"id\":13,\"name\":\"TestKeyword\"},
{\"id\":14,\"name\":\"testkeyword1\"},{\"id\":15,\"name\":\"testkeyword2\"},
{\"id\":16,\"name\":\"testkeyword3\"}]";
$('#basic').magicsearch({
dataSource: dataSource,
fields: ['name'],
id: 'id',
format: '%name%',
hidden: true,
multiple: true,
focusShow: true,
isClear: true,
multiField: 'name',
multiStyle: {
space: 5,
width: 200
},
success: function ($input, data) {
idList = idList + data.id + ',';
nameList = nameList + data.name + ',';
// alert(data.id);
return true;
},
afterDelete: function ($input, data) {
idList = idList.replace(data.id + ',', "");
nameList = nameList.replace(data.name + ',',"");
//alert(data.id);
return true;
}
});
});
Link to the fiddle: https://jsfiddle.net/yn0cmgjt/5/
Excuse me for the bad interface. I had a difficult time getting the CDN link to this plugin and somehow it is not rendering the correct way. But play around with this and I hope it will suffice to your needs.
The two lists (idList and nameList) that I have created are the id and name for each element concatenated in a single comma separated string. Once you have this string, you can extract out the required information.
I'm calling an ajax for giphy, with this code:
$.ajax({
url: queryURL,
method: "GET"
}). then(function(response) {
console.log(response);
when I look at the console log there's an object with the first property being data. Each index of data is another object, inside this object are two properties i'm trying to pull, rating and url. I want to be able to list the rating and url not just of a specific index, but every index in that data array. What would be the best way to do that? Currently I've tried a for loop
for (var i = 0; i<response.data.length;i++){
var dataIndex = response.data[i];
}
then <creating a variable something like>
var imgURL = response.data[dataIndex].url
but its not working.
Here's the entire code
function displayTopicGif() {
var topic = $(this).attr("data-name");
// query url
var queryURL = "https://api.giphy.com/v1/gifs/search?q=" + topic + "&limit=20&rating=r&api_key=";
$.ajax({
url: queryURL,
method: "GET"
}).then(function (response) {
console.log(response);
// for loop to create a variable for the index of the objects data
for (var i = 0; i < response.data.length; i++) {
var dataIndex = response.data[i];
}
// where the gif's will be dumped
var topicDiv = $("<div class='topic'>");
// rating of gif
var rating = response.data[0].rating;
console.log(rating);
// Creating an element to have the rating displayed
var pOne = $("<p>").text("Rating: " + rating);
// add to the rating element
topicDiv.append(pOne);
// retrieve the IMG of the gif
var imgURL = response.data[0].url;
var image = $("<img>").attr("src", imgURL);
topicDiv.append(image);
// put gif into topic-view div
$("#topic-view").prepend(topicDiv);
});
}
You can check that something is an object using $.isPlainObject and then read through its properties via:
for (key in object) {
var value = object[key];
//log
}
Or you can get the keys using Object.getOwnPropertyNames();. See this sample excerpt from MDN:
const object1 = {
a: 1,
b: 2,
c: 3
};
console.log(Object.getOwnPropertyNames(object1));
// expected output: Array ["a", "b", "c"]
Why it returns only the last number of my database? I wanted to return all value inside the div. Help me.
Ajax
$.ajax({
url : 'Test.php',
method : 'post',
data : {id: id},
success : function(response)
{
var x = $.parseJSON(response);
for (var a in b)
{
$('#allValue').html(b[a]);
}
}
});
PHP
if(isset($_POST['id']))
{
$query = mysqli_query($conn, "SELECT * FROM tbl_reservation");
while ($row = mysqli_fetch_array ($query))
{
$result[] = array($row['ID_reservation']);
}
echo json_encode($result);
}
By doing
$("#element").html("TEST");
The HTML method removes any existing content and replaces it with "TEST". If you want to append text you could first get the HTML value, but better yet:
var x = $.parseJSON(response);
var text = "";
for (var a in b)
{
text += b[a];
}
$('#allValue').html(text);
I'm using autocomplete with materialize and i'm retrieving the data with ajax call, it works fine but when i want to call ajax only after entering caracters(using onkeyup event), the drop down list will not be showing correctly !!!!
Before i forget please help me to show a "NOT FOUND" in the drop down list if no data founded (because my else condition doesn't work). here is my code and thanks a lot in advance :
$(document).ready(function() {
var contents = $('#autocomplete-input')[0];
contents.onkeyup = function (e) {
$.ajax({
type: 'GET',
url: Routing.generate('crm_search_lead', {"search":
$(this).val()}),
success: function (response) {
var contacts = {};
if (true === response.success) {
var result = response.result;
for (var i = 0; i < result.length; i++) {
var lastName = result[i].lastName ?
result[i].lastName : '';
var firstName = result[i].firstName ?
result[i].firstName : '';
contacts[lastName + " " + firstName] = null;
}
$('input.autocomplete').autocomplete({
data: contacts,
minLength: 2,
});
} else {
$('input.autocomplete').autocomplete({
data: {
"NOT FOUND": null
}
});
}
}
});
}
});
Hi people :) i resolve it by changing onkeyup() with focus() and it's totally logical because with onkeyup() the droplist will appear and disappear very quickly on every key entered.