I have some problem with my code, I do not understand about send multiple parameters from jquery to controller
this is my route
Route::get('/listcdix/{id}/detail/{asnumber}',
['as' => 'remindHelper', 'uses' => 'ListcdixController#detail']);
bellow is my jquery
otable.on("click", ".btnview",
function () {
var custcode = otable.rows($(this).closest("tr")).data()[0].CustCode;
var asnumber = otable.rows($(this).closest("tr")).data()[0].ASNumber;
window.location = "{{route('remindHelper',['id'=>"+custcode+",'asnumber'=>"+asnumber+"])}}";
});
but when I click this button cannot get value custcode and asnumber
this is my URL like this
http://127.0.0.1:3232/listcdix/+custcode+/detail/+asnumber+
please check my picture
thank you for attention
window.location = "{{route('remindHelper',['id'=>custcode,'asnumber'=>asnumber])}}";
try this one hope it works
You are inside a blade directive ({}), so the page is expecting you will be adding a PHP segment while inside those curly braces. You are mixing and matching between PHP and Javascript and it produces the literal +custcode+ instead of the value behind the variable. Same thing happens if you try to add it without removing the quotes - it translates it as a constant and will produce an 'undefined constant' error. I.e. you are not getting down to Javascript to get to the variable.
Use one or the other, JS or PHP. So if all Javascript, something like:
APP_URL + "/listcdix/"+ custcode +"/detail/" + asnumber;
Where APP_URL is a pre-defined variable for your url prefix. You can cheat a little on that definition if you want and use the Laravel url() method to help you. Something like:
var APP_URL = {!! json_encode(url('/')) !!};
from a blade page before the JS needs to access it.
You can use this something like that
window.location = "{{route('remindHelper',['id'=>"custcode",'asnumber'=>"asnumber"])}}";
because when use using curly braces {{}} in laravel. You do not need to concat this with ++. So that's why you can use this as simple format. i have checked this code on my end as well so this will be 100% work for you.
Thank you.
Unfortunately you can not combine js variable with php code as far as I know and you are using the route function and because your route needs argument to process, you need to pass variables inside the "route" function. as far as I know there is no way and you have to give the route in complete string way like this:
let route = "/listcdix/" + custcode + "/detail/" + asnumber
Related
This may be really basic, but help would be appreciated.
I have the following script that can be added to a custom built web application and embeds within it another page in an iframe.
$(function() {
$('#customframe').after('<iframe src="https://www.myurl.com" style="width:100%;height:200px"></iframe>');
});
I need to include a query parameter to the iframe like below
$(function() {
$('#customframe').after('<iframe src="https://www.myurl.com/page.html?id=ThisRecordId" style="width:100%;height:200px"></iframe>');
});
ThisRecordId variable is already available in the DOM.
If I go to the browser console and just type ThisRecordId it displays the value I need for the iFrame.
I am just struggling to get it to work.
Thanks in advance.
Use string interpolation for string literals,
use backticks instead of quotes and enclose variable in ${ThisRecordId} template
More about template literals here.
Example:
let ThisRecordId = 'myidnumber'
$(function() {
$('#customframe').after(`<iframe src="https://www.myurl.com/page.html?id=${ThisRecordId}" style="width:100%;height:200px"></iframe>`);
});
So im my view (made with using Blade templates) I have link with href that calls Controller directly, that looks like:
<a id="#{{user.id}}" href="{{action('Controller#method')}}>update</a>
and it works fine, but now I need to pass some additional data, which is taken from vue.js array, which also should be put in curved braces, like:
<a id="#{{user.id}}" href="{{action('Controller#method', ['user_id' => #{{user.id}}])}}>update</a>
and here I start getting messages about unexpected { symbol. I've tried removing # symbol, and putting this variable outside href quotes, but it didn't work. Is there any possibility to solve this?
UPD: I also tried putting this link in vue method, like
$.getJSON('{{action("Controller#method", ["user_id" => '+vm.user.id+'])}}')
but in this case literally "vm.user.id", not variable gets passed in link
UPD2:
I'm now trying to pass data not in link, like $.getJSON('{{action("Controller#method")}}', {user_id: vm.user.id})
and I'm getting links like update?user_id=123, but I need format like update/123
I'm really not familiar with vue.js, but in general PHP would execute on server, and javaScript on client. So first line
<a id="#{{user.id}}" href="{{action('Controller#method', ['user_id' => #{{user.id}}])}}>update</a>
has this part #{{user.id}} PHP can't understand, as it's javaScript notation, so you get some kind of syntax error.
And for second line
$.getJSON('{{action("Controller#method", ["user_id" => '+vm.user.id+'])}}')
I think that this could not get executed either since this doesn't load Laravel, and to PHP {{action("Controller#method")}} has no particular meaning without Laravel loaded.
You could do something like this. Hardcoding part of URL that will render from server, and adding rest of the URL when it goes to client.
For example you can have server generate URL like this:
<a id='url' href="http://example.com/controller/method/">update</a>
And then have some kind of javascript function on client that would append vm.user.id to it when page loads
$(document).ready(function(){
$('#url').attr('href', $('#url').attr('href') + vm.user.id);
});
I think this would result in having an URL like this
<a id='url' href="http://example.com/controller/method/123">update</a>
I need to call a controller function from javascript on my gsp.
I have read different solutions from hundreds of places but none worked.
The problem which I found closest to mine was this.
But I am not repeating the same mistake as this and thus the solution didn't help.
I have a tag like this which calls the javascript function
<g:select name="poNumber" noSelection="['':'Select PO Number']" from="${com.rerq.PurchaseOrder.list()}"
onchange="getProject(this.value)" />
And the javascript function looks like this
function getProject(poNumber){
var projectName = document.getElementById("projectName");
var newData = ${remoteFunction(controller: 'sow', action: 'getProject', params: ['poNumber':poNumber])};
}
And the function I need to call is
def getProject(String poNumber) {
String projectName = Sow.find("from Sow as s where s.poNumber=?", [poNumber])
return projectName
}
The controller function might have mistakes as I am completely new to groovy and grails. But my understanding is that the control isn't reaching here so this should not be the cause of any problem.
I am getting below exception
No signature of method: remoteFunction() is applicable for argument types: (java.util.LinkedHashMap) values: [[controller:sow, action:getProject, params:[poNumber:null]]]
I tried using remoteFunction() in g:select itself but it threw another exception which said
Attribute value quotes not closed ...
even though they were.
Any help is greatly appreciated.
To use remoteFunction with Grails 3 you need to add the ajax-tags plugin: org.grails.plugins:ajax-tags:1.0.0
Actually you can have your gsp recognize some Grails functions inside your js if the script is inside the gsp and anything you need for your js is created on the server side. In your case it seems you want to do an ajax call so you could have the following.
project.gsp (Consider that you already loaded jQuery)
<g:select name="poNumber" noSelection="['':'Select PO Number']" from="${com.impetus.rerq.PurchaseOrder.list()}"
onchange="getProject(this.value)" />
And in the same file you have
<script type="text/javascript">
function getProject(poNumber){
jQuery("#yourTarget").load("${createLink(action: 'getProject')}",{poNumber: poNUmber},function(response, status, xhr ){
if ( status == "error" ) {
alert("No data loaded.")
}
});
}
</script>
As you see a gstring in load("${}",... is used because it will be parsed in the server and at client side your actual js it will parse to load("yourcontrollerName/getProject",..... Why not code the url directly in the js? Because by using createLink() it is less likely to make reference mistakes.
EDIT
If you want to do this but using an external js file, you would need a way to pass the url, to the js, and use a simple load function. So something like this will be helpful
<g:select name="poNumber" noSelection="['':'Select PO Number']" from="${com.impetus.rerq.PurchaseOrder.list()}"
onchange="getProject(this.value, \'${createLink(action:'getProject')}\')" />
Once on the server onchange="getProject(this.value,\'${createLink(action:'getProject')}\')"would be parsed to onchange="getProject(this.value,'yourController/getProject')". Be wary that I might have messed up the ""s and ''s so verify your html output.
And you would need a js function that accepts the uri
function getProject(value, targetUri){
....
}
What you need to review is when is your function needed, on the server o in the client;if the functions are inside a gsp or not; And if not available, how could you pass data to them.
You cannot access grails's controller from javascript. I haven't tested it but this might work.
<g:createLink controller="sow" action="getProject", params="['poNumber':poNumber]"/>
Also, if you use Google Chrome's developer's tool, you will see how your javascript code is displayed. Make sure it is in right syntax.
I need someone to enlighten me.
I've seen this:
<script src="http://host.com/file.js?no_forms=1"></script>
What does that means? A GET parameter passed to a javascript file?
Into which conditions this can be done?
What kind of approach is this?
Any help would be appreciated.
?no_forms=1 is just a query string parameters. i will just tell the common usage of such things.
Used for to avoid caching (get new updated version of JS)
Some sort of redirection
Even some application usage ideas.
Sometimes the parameter is used simply to prevent client side caching.
It could also be that requesting file.js is actually rewritten as a dynamic call (say script.php?file=file.js&no_forms=1) that is fetching the correct file and using the extra parameter somehow.
You can use this
<script src="http://host.com/file.js" no_forms="1"></script>
And
function $_GET(q,s) {
s = s || window.location.search;
var re = new RegExp('&'+q+'=([^&]*)','i');
return (s=s.replace(/^\?/,'&').match(re)) ? s=s[1] : s='';
}
And get result
var noForms = $_GET('no_forms');
Essentially I'm trying to do the opposite of what django-ds-utils does, which is get URL argument out of a resolved URL, for instance, take the following URL definition:
url(r'^reset_pwd/(?P<code>[A-Za-z0-9]+)/$', ResetPasswordView.as_view(), name='my.registration.views.reset_pwd'),
On that page, within JS, I would like to retrieve <code> into a kwargs array. Is this something that exists, or is there a standard approach to this?
AFAIK there is no standard approach. You can:
put JS into template and render view variable
pass variable in JS globals, setting it's value in template text
parse window.location
...etc.
I personally prefer the second way, e.g. put this code in template and then use _reset_pw_code global in javascript file:
<script> var _reset_pw_code = "{{ reset_code|escapejs }}" </script>
Recently JavaScript added a new API to do this it is currently still experimental.
const pattern = new URLPattern('reset_pwd/:code/', 'https://example.com');
console.log(pattern.exec('https://example.com/reset_pwd/thECode/').pathname.groups); // { code: 'thECode' }
https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API