⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

add_action target _blank



naufal
  • profile picture
  • Member

Posted 18 March 2012 - 17:32 PM

Does anyone know, how to custom add_action using attribute _blank in a link?

Ty

KaBaDaBrA
  • profile picture
  • Member

Posted 18 March 2012 - 18:29 PM

can use jQuery to do it if there is no way on GCRUD yet...

Example:


$('#your_element_id').attr("target", "_blank");


EDIT: Just tested one of mine with the following:


$('a[title="Properties"]').attr("target", "_blank");


Works perfect - maybe they guys can add an additional option called "target" - something like the below. But until then jQuery can do sooooo much!!!!!


void add_action( string $label, string $image_url , string $link_url , string $css_class , mixed $url_callback, string $target)

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 18 March 2012 - 20:43 PM

Hmmmmmm @KaBaDaBrA is right about jquery, for now it is the only fast solution if you don't want to change the core from gCRUD . The only problem will be with the ajax thing. You have to call it in every ajax call. So the only way to it is to have a little trigger at the jquery ajax, For flexigid is at: assets/grocery_crud/themes/flexigrid/js/flexigrid.js line 33-34 :


$(this).ajaxSubmit({
url: ajax_list_info_url,
dataType: 'json',
success: function(data){
$('#total_items').html( data.total_results);
displaying_and_pages();
this_form.ajaxSubmit({
success: function(data){
$('a.target_blank_class').trigger('add_target_blank'); // THIS IS THE LINE THAT YOU WILL ADD

$('#ajax_list').html(data);
}
});
}
});



and of course the javascript:


$("body").delegate('a.target_blank_class', "add_target_blank", function(){
$(this).attr("target", "_blank");
});


Just don't forget to add the add_target_blank to the 4th argument of the add_action method. For example:


$crud->add_action('Smileys', '/images/Active2.png', 'just_a_test','your_css_class add_target_blank');


It will work fine because if the a.target_blank_class does not even exist, you will not have any problem as the jquery is clever enough to not throw an error.

I think it is a more complicated solution but if you follow it step by step you will be proud of yourself that you made it :)

Also the $target as 6th argument is a good idea but I don't want just to add arguments for every new feature that I have in mind. Perhaps I will add a new method named "callback_action" to have your own action button. It will work exactly like the callback_column with the only difference that it will appear at the buttons sections. So in your case it will be easy to create your own button with the target blank.

naufal
  • profile picture
  • Member

Posted 19 March 2012 - 15:37 PM

My knowledge in jquery is poor.

Thank you so much guys, i've learned something today.

fastturtle
  • profile picture
  • Member

Posted 09 July 2012 - 01:30 AM

I am trying to get this to work and I am having a little trouble. Where do I put the javasscript code? Do I put it in my view that contains the grocery crud table or do I put it in a javascript file?

fdias
  • profile picture
  • Member

Posted 09 July 2012 - 23:22 PM

Just put the javascript on the view. Of course you could have a js file depending on the size of your script, but still you would need to link the js file from the view.

The javascript file needs to be called from an html file (the view will generate that html), therefore you have to call if from the view (either grocerycrud view rendered by GC or even flexigrid list view/template)

Cheers

webdev23
  • profile picture
  • Member

Posted 29 October 2013 - 18:54 PM

can use jQuery to do it if there is no way on GCRUD yet...

Example:
 

$('#your_element_id').attr("target", "_blank");
EDIT: Just tested one of mine with the following:

$('a[title="Properties"]').attr("target", "_blank");
Works perfect - maybe they guys can add an additional option called "target" - something like the below. But until then jQuery can do sooooo much!!!!!

void add_action( string $label,  string $image_url , string $link_url , string $css_class ,  mixed $url_callback, string $target)

Where do you add this jQuery statement? On which page or which function or callback? Thanks.


webdev23
  • profile picture
  • Member

Posted 04 November 2013 - 13:50 PM

Where do you add this jQuery statement? On which page or which function or callback? Thanks.

Anybody?


DrPaul
  • profile picture
  • Member

Posted 05 March 2014 - 12:46 PM

Here's a quick hack that will do the job without any mods to GC - just add a call to str_replace() before you pass the output to your print function in your controller, e.g.:

 

$output->output = str_replace('class="print-icon crud-action"', 'class="print-icon crud-action" target="_blank"', $output->output); // additional line
$this->_example_output('example_template.php', $output);
 

In my case the added action has the given class, but you could use the title instead - just load up the relevant page, do a "view source" and take a look at the final output, then work out where to splice in the 'target="_blank"'

 

Edit:

 

Darn it, only works once of course!


DrPaul
  • profile picture
  • Member

Posted 30 May 2014 - 10:30 AM

I just had to come back to this, as I really wanted actions to open in a new window - the code is printing out PDF documents - but I just couldn't get the jQuery trickery to work.

 

Turns out that if you don't mind changing 3 lines of GC core code, it's easy to add the 6th arg to add_action():

 

In file Grocery_CRUD.php around line 5020 change

 

public function add_action( $label, $image_url = '', $link_url = '', $css_class = '', $url_callback = null)
to
public function add_action( $label, $image_url = '', $link_url = '', $css_class = '', $url_callback = null, $action_target = '_self')
 
Then around 6 lines later after
'css_class' => $css_class,
add this line
'action_target' => $action_target,
 

Finally in file assets/grocery_crud/themes/flexigrid/views/list.php around line 54 change

 

<a href="<?php echo $action_url; ?>" class="<?php echo $action->css_class; ?> crud-action" title="<?php echo $action->label?>">
to
<a href="<?php echo $action_url; ?>" class="<?php echo $action->css_class; ?> crud-action" title="<?php echo $action->label?>" target="<?php echo $action->action_target?>">

 

And that's it, just add the 6th arg to add_action() in your controller code where the target needs to be other than "_self" - works for me directly (first page view) and via AJAX (after a search or stepping through pages)

 

Flexigrid only of course, but a simple change to list.php for other themes should be straightforward.


Amit Shah
  • profile picture
  • Member

Posted 31 May 2014 - 03:31 AM

exactly.. thats the only place.. directly adding the change into the GC library and not using the jquery ... cuz after the 1st page load, when system reloads the stuff.. the jquery wont execute again. Unless you want it using just jquery then u need to apply this hack / patch to your library

 

/topic/2260-do-javascript-function-after-ajax-list-finished/

 

this is helpful for taking control of the additional actions that you might prefer performing. Else.. the above hack provided is best if u want to take in just simple _blank to the add button

 

Happy GCing:)


DrPaul
  • profile picture
  • Member

Posted 31 May 2014 - 16:48 PM

Here's a slightly more general-purpose version of the hack:

 

public function add_action( $label, $image_url = '', $link_url = '', $css_class = '', $url_callback = null, $extra_args = '')
 
'extra_args' => $extra_args,
 

<a href="<?php echo $action_url; ?>" class="<?php echo $action->css_class; ?> crud-action" title="<?php echo $action->label; ?>"<?php echo $action->extra_args; ?>>

 

This will let you add any number of arbitrary arguments to the add_action href, so you call it in your controller with 6th arg such as ' target="_blank"' - you could even add things like JavaScript triggers for example.


DrPaul
  • profile picture
  • Member

Posted 31 May 2014 - 16:58 PM

While we're on the subject of hacking list.php :-)

 

If you remove all of the HTML white space inside the <div class='tools'> on lines 37 to 53 or so of list.php, then all of the add_action() hrefs will be output without that white space between them and will behave better if the user resizes the browser window, in that they will stay "lined up" for as long as possible.


DrPaul
  • profile picture
  • Member

Posted 24 July 2014 - 13:06 PM

And if you do decide to open actions in a new tab, try this bit of JavaScript in the head of the view template you are using:

 

<script type='text/javascript'>
window.onunload = refreshParent;
    function refreshParent() {
        if(window.opener) {
window.opener.location.reload();
}
    }
</script>
 

This was posted as a way of refreshing a parent window when a popup window closes, but it works if a "_blank" href target is used as well.

 

So now I can add an action to flexigid, have it open in a new tab, and then when that tab is closed the original tab updates itself automatically - nice!


Adriano Gonçalves
  • profile picture
  • Member

Posted 30 September 2014 - 02:08 AM

This is my solution for that (need no changes on core):

<script type="text/javscript">
function openBlank(url)
{
    window.open(url);
}
</script>

<?Php

// ...

$url_callback_print = function($primary_key, $row)
{
    return "javascript:openBlank('" . base_url('documents/printDocument') . '/' . $primary_key . "')";
};
$crud->add_action('Print Document', base_url('images/print.png'), '', '', $url_callback_print);

// ...

robertgalp
  • profile picture
  • Member

Posted 12 May 2015 - 05:49 AM

Hope this will help you....HTML New Window

 

Robert