How do I specify the method of an action (similar to _delete):
generator:
config:
list:
object_actions:
myaction: {label: Label, action: myaction, method: post}
This ignores my method setting and renders a get link:
Label
Whereas I want it to a "post" link, similar to _delete (with onclick attribute)
I guess your using Propel.
If you check the generator code, specifically on: generator > theme_name > template > template > _list_td_actions.php, there you'll find a pice of code like:
<?php else: ?>
<li class="sf_admin_action_<?php echo $params['class_suffix'] ?>">
<?php echo $this->addCredentialCondition($this->getLinkToAction($name, $params, true), $params) ?>
</li>
<?php endif; ?>
That's the code thats being executed when you define a custom object action like the one you described. Check $params and you may find a solution to your needs ( i think that probably you could define something like the onclick attribute value).
If you just want to have a confirmation message than probably the best way is:
generator:
config:
list:
object_actions:
myaction: {label: Label, action: myaction, confirm: "Are your sure?", params: {onclick: 'alert("Bu!");'} }
Additional parameters to link_to can be passed with 'params' option (notice 'onclick' in the example above).
maybe this will be useful
generator:
config:
list:
object_actions:
myaction:{ params: { onclick : "if(confirm('Are you sure?')){return true;}else{return false;}" } }
Related
I'm trying to do a very easy thing, but I'm stuck and I feel so stupid.
Basically I have a text area defined like this:
<textarea id="comment" name="comment" class="form-control" rows="7"></textarea>
Then I have a createlink tag where I would like to pass in the params section the content of the textarea, so I did something like this:
<g:createLink controller="admin"
action="book"
id="${bookingInstance?.id}"
params="jQuery('#comment').serialize()"/>
Of course it doesn't work.
Can anybody give me some advices?
thanks a lot
nibe
UPDATE:
the create link it's inside a html tag. Like this:
<a class="btn btn-primary btn-centered" onclick="getComment();" title="Press me" href=
<g:createLink
controller="admin"
action="bookAccepted"
id="${bookingInstance?.id}"/>>
Press me
</a>
The getComment() function is defined like this:
function getComment() {
window.location.href = $(this).attr('href') + '?' + $('#commet').serialize();
return false;
}
As result I got this error message:
Provided id of the wrong type for class com.Booking. Expected: class java.lang.Long, got class java.lang.String. Stacktrace follows:Message: Provided id of the wrong type for class com.Booking. Expected: class java.lang.Long, got class java.lang.String
Well you have to do this with jQuery. So something like this perhaps:
<g:link controller="admin" action="book" id="${bookingInstance?.id"} onclick="return function() { window.location.href=$(this).attr('href') + '?' + $('#comment').serialize(); return false;}">My link</g:link>
Or you can try use JS like that. Works fine for me, of course, for ajax call.
function sendTextArea(comment) {
var params = {"comment":comment, "id": ${bookingInstance?.id}};
console.log(params);
${remoteFunction(controller: 'admin', action: 'book', params:'params', update:'div')}
}
I've been Googling this to no avail. I have a multi-checkbox form element in one of my forms. Here's the code I used to create it:
$this->add(array (
'name' => 'thingyId',
'type' => 'MultiCheckbox',
'options' => array (
'value_options' => $thingyArray,
)
));
In my view script, I have this:
<?= $this->formRow($form->get('thingyId')); ?>
The form element shows up fine, but all of the checkboxes are on a single line. How do I get it so that each checkbox is on a new line?
If you view this link, you can see that the fourth argument is partial. So, you can use many ways to accomplish the task.
Method 1:
echo $this->formRow($element, null, null, 'template-file');
Now, create a template file named as template-file.phtml to render the element however you like.
//template-file.phtml
<span><?php echo $label; ?></span><br/>
<?php foreach ($element->getValueOptions() as $key => $value): ?>
<input type="checkbox" name="<?php echo $element->getName() ?>[]" value="<?php echo $value; ?>">
<span><?php echo $key; ?></span><br/>
<?php endforeach; ?>
Method 2
Create your own view helper by extending the default helper.
namespace Application\View\Helper;
class MyFormRow extends \Zend\Form\View\Helper\FormRow
{
/**
* #var string
*/
protected $partial = 'template-file';
}
Now, inform our application about our new helper in your module,
namespace Application;
class Module
{
public function getViewHelperConfig()
{
return array(
'invokables' => array(
'myFormRow' => 'Application\View\Helper\MyFormRow'
)
);
}
}
Lastly use the helper:
echo $this->myFormRow($element);
I came across this question when I was having this issue myself. The code that was being used was the following:
<?php
$oMultiCheckboxField = $oForm->get('multicheckboxelement');
echo $this->formMultiCheckbox($oMultiCheckboxField);
?>
The only additional parameter you could pass to the formMultiCheckbox view helper was whether to append or prepend the label.
How I eventually chose to solve this is with the following code:
<?php
$oMultiCheckboxField = $oForm->get('multicheckboxelement');
$oMultiCheckboxViewHelper = new \Zend\Form\View\Helper\FormMultiCheckbox();
$oMultiCheckboxViewHelper->setSeparator('<hr>');
echo $oMultiCheckboxViewHelper->render($oMultiCheckboxField);
?>
From what I recall, ZF1 had the option to set the separator (I clearly remember this at least for radio buttons). Why there isn't a clearer way to do this in ZF2 is a bit puzzling. If there are better ways to do this, I would certainly like to know about it.
I am having following action link:
<%= Html.ActionLink("Check this", "Edit", "test",
new { id = id }, new { style = "display:block" })%>
How do I include data=name as query string. Some thing like this:
link?data=name
4th parameter of Html.ActionLink can have any number of properties:
<%= Html.ActionLink("Check this", "Edit", "test",
new { id = id, data=name }, new { style = "display:block" })%>
These properties are inserted into URL based on routing, but if the property name cannot be matched into any route it is added as URL GET parameter.
So if you have standard route {controller}/{action}/{id}, you will get the URL:
test/Edit/[id]?data=[name]
from the above code.
Pass Query String By this way
#Html.ActionLink("Delete Record", "Home", "Delete", new { id=Id},null)
By above code you will get the url like(Suppose Id=1): /Home/Delete/1
and if you want to add more parameters to query string then:
#Html.ActionLink("Delete Record", "Home", "Delete", new { id=Id, Name=name},null)
By above code you will get the url like(Suppose Id=1 and Name=India) :
/Home/Delete/1?Name=India
I got tired of banging my head against a wall with the html.actionlink. Works great when you just want to route it against straightforward routing calls, but absolutely refuses to cooperate when you want to add a simple querystring at the end.
I don't an ID at then end, I want to be able to add some kind of actual Querystring with the "?".
So anywhere I needed a Querystring I switched to using the url.action inside the anchor tag.
<a href='#url.action("Action","route")?Parameter=Value' >Text for Link Name</a>
At least it works and I can stop getting headaches over something that should have been a very simple task. Someone needs to get their heads out of their butts and make the ActionLink work properly for Querystrings in the MVC routing.
I know this is kind of old question but.
In case the below code doesn't generate the <a href="/?param=value" />.
<%= Html.ActionLink("Text", "Action", "Controller", new { param=value }, null)%>
I would advice checking whether you action has at least one [Route] attribute (I used [Route("/")] for example).
Hope it helps.
I'm using Symfony 1.4 and wondering whether it's possible to achieve the following:
<span>Text</span>
... using Symfony's link_to helper?
Of course, it's possible to do this:
<span>Text</span>
But I'm wondering if there's a simpler way to do it, especially as combining i18n with the above will produce:
<span><?php echo __('Text') ?></span>
... a tag soup basically.
Thanks.
YOu can do it two ways...
Option 1
<?php echo link_to("<span>".__('Text')."</span>", $url); ?>
Option 2
<?php echo content_tag('a', "<span>".__('Text')."</span>", array('href' => url_for($url))); ?>
There's also:
<?php echo link_to(content_tag('span', __'Text', array('class' => 'span-class')), '#route', array('class' => 'link-class'));
I added the attribute class for each of the two HTML tags as options if you need to extend that way.
How do I create a link of this type:
<a href="#" onclick="document.getElementById('search').value=this.value">
using method link_to in Rails?
I couldn't figure it out from Rails docs.
You can use link_to_function (removed in Rails 4.1):
link_to_function 'My link with obtrusive JavaScript', 'alert("Oh no!")'
Or, if you absolutely need to use link_to:
link_to 'Another link with obtrusive JavaScript', '#',
:onclick => 'alert("Please no!")'
However, putting JavaScript right into your generated HTML is obtrusive, and is bad practice.
Instead, your Rails code should simply be something like this:
link_to 'Link with unobtrusive JavaScript',
'/actual/url/in/case/javascript/is/broken',
:id => 'my-link'
And assuming you're using the Prototype JS framework, JS like this in your application.js:
$('my-link').observe('click', function (event) {
alert('Hooray!');
event.stop(); // Prevent link from following through to its given href
});
Or if you're using jQuery:
$('#my-link').click(function (event) {
alert('Hooray!');
event.preventDefault(); // Prevent link from following its href
});
By using this third technique, you guarantee that the link will follow through to some other page—not just fail silently—if JavaScript is unavailable for the user. Remember, JS could be unavailable because the user has a poor internet connection (e.g., mobile device, public wifi), the user or user's sysadmin disabled it, or an unexpected JS error occurred (i.e., developer error).
To follow up on Ron's answer if using JQuery and putting it in application.js or the head section you need to wrap it in a ready() section...
$(document).ready(function() {
$('#my-link').click(function(event){
alert('Hooray!');
event.preventDefault(); // Prevent link from following its href
});
});
just use
=link_to "link", "javascript:function()"
another solution is catching onClick event and for aggregate data to js function you can
.hmtl.erb
<%= link_to "Action", 'javascript:;', class: 'my-class', data: { 'array' => %w(foo bar) } %>
.js
// handle my-class click
$('a.my-class').on('click', function () {
var link = $(this);
var array = link.data('array');
});