I am making a filter in order to drill down into a report. I have two fields that are dependent on the selection of another field (which is a multiselect list). When I make my selection(s) from that multiselect field, it looks like the parameter is being passed in as an array and ultimately not being recognized. It looks as if my parameter is being passed in as an array, which I'm thinking is the issue
Javascript Code:
function getMO()
{
var fa_code = $('#tslcFilterFA').val();
var ao_code = $('#tslcFilterAO').val();
var wq_code = $('#tslcFilterWQ').val();
var newOption = '';
$('#tslcFilterMO').empty();
$('#tslcFilterMO').append(newOption);
$('#TSLC_MO_Loading').css('display','inline');
$.ajax({
url: '../TSLC/getData.cfc',
type:"POST",
cache: false,
dataType: "text",
async:true,
data: {method: "getMO",
fa_code: fa_code,
ao_code: ao_code,
wq_code: wq_code
},
success: function(returnMsg)
{
try
{
var obj = JSON.parse(returnMsg);
$.each(obj.DATA, function(index,row) {
if (obj.DATA.length == 1)
{
var newOption = '<option selected="selected" value="' + row[0] + '">' + row[1] + '</option>';
}
else
{
if (row[2] == "1")
{
var newOption = '<option selected="selected" value="' + row[0] + '">' + row[1] + '</option>';
}
else
{
var newOption = '<option value="' + row[0] + '">' + row[1] + '</option>';
}
}
$('#tslcFilterMO').append(newOption);
});
try
{
$('#tslcFilterMO').multiselect('destroy');
}
catch(e) {}
$('#tslcFilterMO').multiselect({
selectedList: 4
}).multiselectfilter();
$('.ui-multiselect').css('width','225px');
$('#TSLC_MO_Loading').css('display','none');
}
catch(e)
{
alert('getMO Error parsing JSON');
}
},
error: function(httpRequest, textStatus, errorThrown)
{
alert("getMO status=" + textStatus + ",error=" + errorThrown);
}
});
}
I've tried to change this line:
var ao_code = $('#tslcFilterAO').val();
to this:
var ao_code = $('#tslcFilterAO').multiselect('getChecked').map(function () {return this.value;}).get();
I've also tried to wrap my ao_code variable in URLDecode() to see if it would pass the value as a string instead of an array, but neither works.
CF Code (from component):
<cffunction name="getMO" access="remote" returntype="any" returnformat="plain" hint="Get distinct Managing Orgs based on FA=ATT_IT and AO">
<cfargument name="fa_code" type="string" required="true">
<cfargument name="ao_code" required="true">
<cfargument name="wq_code" required="true">
<cfquery name="qMO" datasource="#request.dbdsn_ic4p#" username="#request.dbuser_m66266#" password="#request.dbpw_m66266#">
SELECT DISTINCT managing_org MANAGING_ORG, DECODE(managing_org,'*','*ALL*',managing_org) MANAGING_ORG_DISPLAY, DECODE(managing_org,'*',1,2) sortcol
<cfif #fa_code# NEQ "ATT_IT">
FROM HAS_TICKET_STATE_GUI_LOOKUP
WHERE client_id = '#fa_code#'
<cfelse>
FROM IT_TICKET_STATE_GUI_LOOKUP
WHERE 1=1
</cfif>
<cfif #ao_code# neq "">
AND active_org IN (<cfqueryparam value="#ao_code#" cfsqltype="cf_sql_varchar" list="true" />)
</cfif>
<cfif #wq_code# neq "">
<!--- !COM: is workaround for commas in listbox items! --->
AND work_queue IN (#replace(ListQualify(wq_code,"'",",","CHAR"),":COM!",",","all")#)
</cfif>
ORDER BY 3, 1
</cfquery>
<cfreturn serializeJson(qMO)>
</cffunction>
Change this line in your JS code
var ao_code = $('#tslcFilterAO').val();
to
var ao_code = $('#tslcFilterAO').val().join(",");
That should give you a list of string values from the multiple select field that you're expecting in your function in the CFC.
The join() method joins all elements of an array into a string. More on "join" here.
This article helped me solve my problem... https://christierney.com/2011/06/07/returning-multiple-value-elements-to-coldfusion-remote-method-via-jquery-ajax/
function getWQ()
{
var fa_code = $('#tslcFilterFA').val();
var ao_code = $('#tslcFilterAO').val();
if ($.isArray(ao_code))
var ao_code_array = ao_code.join(",");
else
var ao_code_array = ao_code;
var mo_code = $('#tslcFilterMO').val();
if ($.isArray(mo_code))
var mo_code_array = mo_code.join(",");
else
var mo_code_array = mo_code;
var newOption = '';
$('#tslcFilterWQ').empty();
$('#tslcFilterWQ').append(newOption);
$('#TSLC_WQ_Loading').css('display','inline');
$.ajax({
url: '../TSLC/cfcs/getData.cfc',
type:"POST",
cache: false,
dataType: "text",
async:true,
data: {method: "getWQ",
fa_code: fa_code,
mo_code: mo_code_array,
ao_code: ao_code_array
},
success: function(returnMsg)
{
Have you considered a combination of the jQuery serializeArray() function and the param() function? It creates a string similar to a query string which is easily parsed. You could send the whole form in.
Example: $.param($('#yourform').serializeArray(),false);
Or, perhaps, simply use the serializeArray function to send your function a JSON string?
Related
dears , i have one table to show the list of category and i need to add unlimited drop down list which will be nested from the parent
like
category A
Category B , if selected B it will add another dropdown and added it into data base and will add another dropdown list sub b1 , sub 2 and if user select sub 2 will do the same
can anyone help
$(document).ready(function(){
//$('.form-control').change(function(){
$('select.form-control').on('change', function () {
var selectedCategory = $(this).children("option:selected").val();
var counter = 1;
//alert( $(this).find('option:selected').text());
//var deptid = $('#category option:selected').attr("id");
var name = "Sub "+ $(this).children("option:selected").text();
if(selectedCategory!=''){
$.ajax({
url: "<?php echo base_url(); ?>category/add_new",
type: 'POST',
data: {'category_parent':selectedCategory,'category_name':name},
dataType: 'json',
async: false ,
cache:false,
success:function(data){
alert(data);
if($('#category'+data).length)
return;
else{
$("#groupd").after('<div id="sub'+data+'" class="form-group">' +
'<label>' +name +' : </label>' +
'<select class="form-control" name="dropdown' + counter + '" id="category'+data+'">' +
// Add your options here...
'<option>Please Select</option>'+
'<option value="'+data+'">'+name +'1</option>'+
'<option value="'+data+'">'+name +'2</option>'+
'</select></div>');
counter++;
}
},
error:function(){
alert('error');
}
});
}
});
});
</script>
I don't know why my code is giving error while making the ajax call and not responding or working at all. I ran this on an html file. I took this function - getParameterByName() from another stackoverflow answer.tweet-container tag is down the code below outside this script and an empty division.I tried some jquery also.
<script>
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
$(document).ready(function(){
console.log("working");
var query = getParameterByName("q")
// console.log("query");
var tweetList = [];
function parseTweets(){
if (tweetList == 0){
$("#tweet-container").text("No tweets currently found.")
} else {
//tweets are existing, so parse and display them
$.each(parseTweets, function(key, value){
//console.log(key)
// console.log(value.user)
// console.log(value.content)
var tweetKey = value.key;
var tweetUser = value.user;
var tweetContent = value.content;
$("#tweet-container").append(
"<div class=\"media\"><div class=\"media-body\">" + tweetContent + "</br> via " + tweetUser.username + " | " + View + "</div></div><hr/>"
)
})
}
}
$.ajax({
url:"/api/tweet/",
data:{
"q": query
},
method: "GET",
success:function(data){
//console.log(data)
tweetList = data
parseTweets()
},
error:
function(data){
console.log("error")
console.log(data)
}
})
});
</script>
strong text
Fix the quotes to resolve your syntax error:
$("#tweet-container").append("<div class=\"media\"><div class=\"media-body\">" + tweetContent + " </br> via " + tweetUser.username + " | " + "View</div></div><hr/>")
I have been trying to perform a simple task (in ASP) for a coldfusion page. I have a local variable "pt.PreCaution" that I need updated. I have tried the following code with no success.
<cfset pt.PreCaution = "">
<label>
Patient Precaution:
</label>
<br>
<cfselect name="lstPreCaution" multiple="yes"
query="GetPreCaution"
value="IsoTypeID"
display="IsoTypeName"
size="8"
required="yes"
width="100"
height="25"
label="Precaution: "
onclick="PreCautionSelected(this)"
>
</cfselect>
function PreCautionSelected(val){
var result ="";
var frm=eval('document.next');
for(i = 0 ; i < val.children.length; i++)
{
if(val.children[i].selected){
if(result.length < 1)
{
result = val.children[i].value;
}
else
{
result = result + "," + val.children[i].value;
}
}
}
$.ajax({
type: "POST",
url: "details.cfm",
data: { "#pt.PreCaution#" : result}
}).done(function(){alert(result)})
}
I modified the ajax call as follows:
function PreCautionSelected(val){
var result ="";
var frm=eval('document.next');
for(i = 0 ; i < val.children.length; i++)
{
if(val.children[i].selected){
if(result.length < 1)
{
result = val.children[i].value;
}
else
{
result = result + "," + val.children[i].value;
}
}
}
$.ajax({
type: "POST",
url: "FileUpdater.cfc?method=setPrecautionType",
data: { lstPrecaution : result}
}).done(function(){})
}
<cfcomponent>
<cffunction name="setPrecautionType" access="remote" returntype="Any" >
<cfargument name="lstPrecaution" type="any" required="true" >
<cfset session.lstPreCaution = #arguments.lstPrecaution#>
<cfreturn />
</cffunction>
</cfcomponent>
I added the following CFC file. This file updates the session variables.
CFC File
Thanks for the input. The Ajax function is getting called and the complete data is being extracted. The only problem is the ColdFusion variable is not getting updated on the server side.
I fixed this problem by written the data to the session data. The problem is the old CF5 frame work that the program is written in.
You have only one mistake.select box not have onclick functionality so you have onchange function means working fine
I have searched a lot through this website and found some similar posts, but they could not help. I have 2 dropdowns. The first is populated through an inline query. The second needs to be populated through the first's selection. I know cfselect and cfajaxproxy are 2 of the simplest things, but I want to use them on Railo which doesn't support them (checked and returned disappointed).
The ajax code goes like this:
$.ajax({
type: 'POST',
url: 'admin/getModelsForManufs.cfc?method=getModels&returnFormat=JSON',
data: {manuid:selected},
dataType: "text",
success: function(res) {
var newoptions = "";
for(var i=0; i<res.length; i++) {
newoptions += "<option value=\"" + res[i].ID + "\">" + res[i].NAME + "</option>";
}
$("#model").append(newoptions);
},
error: function(x) {
alert(x.responseText);
}
});
Where manuid is the first selection, and model is the html field ID for second dropdown to be populated. The cfc has the following:
<cffunction name="getModels" access="remote" returnType="array">
<cfargument name="manuid" type="numeric" required="true" default="#url.manuid#">
<cfset var data="">
<cfset var result=[]>
<cfset var i=0>
<cfquery name="data" datasource="#THIS.dsn#">
select modelId, modelName
from tablename
where manufacturerid = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.manuid#">
</cfquery>
<cfloop index="i" from="1" to="#data.RecordCount#">
<cfset model = {}>
<cfset model.NAME = data.modelName[i]>
<cfset model.ID = data.modelId[i]>
<cfset arrayAppend(result, model)>
</cfloop>
<cfreturn result>
</cffunction>
In Firebug, the data is in the format:
[{"ID":84.0,"NAME":"name 1"},{"ID":1.0,"NAME":"name 2"}]
which looks correct and there are many more records returned. Have tried most of the tricks but still scratching my head as to why the second dropdown doesn't populate. Any help is highly appreciated. Thanks in advance.
Try this code
$.ajax({
type: 'POST',
url: 'admin/getModelsForManufs.cfc?method=getModels&returnFormat=JSON',
data: {manuid:selected},
dataType: "text",
success: function(res) {
var res2 = JSON.parse(res);
var newoptions = "";
for(var i=0; i<res2.length; i++) {
newoptions += "<option value=\"" + res2[i].ID + "\">" + res2[i].NAME + "</option>";
}
$("#model").append(newoptions);
},
error: function(x) {
alert(x.responseText);
}
});
i have to items on my html
a Input text (company) field and a select (Company_List)
when user types in the text field i want jQuery to use /Home/SearchSynonym/ to get the names and display it in select (the SearchSynonym takes the value user types and do a wildcard search and returns ID and NAME )
can some one help , am new ti jQuery
Leb
You need to clarify as to in which format does SearchSynonym return the IDs and NAMEs? Is it in JSON? If it is in JSON, then try the following:
var companyList = $("#Company_List");
$("#company").change( function(){
$.getJSON("/Home/SearchSynonym/",{ query: $(this).val() }, function(response){
var responseList = "";
$.each(result, function(index, item){
responseList += "<option value='" + item.id + "'>" + item.name + "</option>";
});
companyList.html(responseList);
});
});
This would work if your 'company' text field bears the id="company" in the tag decleration, your 'Company_List' dropdown bears the id="Company_List" in the tag decleration and your server end receives the parameter "query" for pulling records.
You can use setTimeout. Try this:
var companyList = $("#Company_List");
$("#company").change( function(){
setTimeout( function() {
$.getJSON("/Home/SearchSynonym/",{ query: $(this).val() }, function(response){
var responseList = "";
$.each(result, function(index, item){
responseList += "<option value='" + item.id + "'>" + item.name + "</option>";
});
companyList.html(responseList);
});
}, 2000);
});
The value of 2000 indicates a 2 second delay.