Add a button for websites

I just installed the latest version and am needing to add a button to records that when clicked, goes to the URL contained in the record.

The table has a field “web” that contains the URL.

This worked great in the old version with the “$crud->add_action” function, but I cannot figure out how to make this work with the new “$crud->setActionButton”

I looked at the example in the documentation, but nothing I have tried does anything but put a non-functioning button on the record.

Please help.

I have the same question.

Hello @JDodd and welcome to our forums :hugs:

Can you please give a more specific example of what you are trying to do? I mean if you can add the code that you were using on the older versions that worked for you and what you have tried that doesn’t work should help.

Regards
Johnny

Johnny, thanks for your reply, I really appreciate all your hard work.
What worked in the old version.
Note: I had to replace the actual url prefix with “SITE” to keep from being locked out of the forum.

Controller
$crud->add_action(‘SITE png’, ‘demo/action_more’,‘ui-icon-plus’,array($this,‘google_map’));
public function google_map($primary_key , $row)
{

//With the below code you will have the lat and the lon as GET parameters to your next request.

return “SITE/maps?q=”.$row->lat.",".$row->lng;
}

What I have tried that didn’t work would fill an encyclopedia :slight_smile:

I did get it working, but is extremely kludgy. I have a function in the controller that calls a function in Model that cerates a new database connection, queries the table for the value in map for the record number in $row and returns the result back to the controller function that is the callback.

Apparently $row was an object in the old version, but is just a variable in the new one.

Hello @JDodd and first of all happy new year :partying_face:

Ok, now I am getting the frustration that you had. I will try to update the documentation as this is a very common question that I have and it is not clear at the documentation. so your code will look similar to this (but there is a hint):

$crud->columns(['customerName','phone','your_field_name']);
$crud->setActionButton('your_field_name', 'fa fa-plus', function ($row) {
    return "SITE/maps?q=" . $row->lat . "," . $row->lng;
});

However, this will not work as the lat and lng will not be available for your datagrid. In short, for performance and security reasons I’ve removed the * from the SELECT of all the columns that I had before from 2.8.0 and later. Now you simply have to specify the columns as invisible columns. So your example will look like this:

$crud->columns(['customerName','phone','your_field_name', 'lat', 'lng']);
// By using the fieldTypeColumn you specify that the `invisible` 
// field type will only be on the datagrid columns
$crud->fieldTypeColumn('lat', 'invisible');
$crud->fieldTypeColumn('lng', 'invisible');

$crud->setActionButton('your_field_name', 'fa fa-plus', function ($row) {
    return "SITE/maps?q=" . $row->lat . "," . $row->lng;
});

I hope this will work for you.

Happy New Year to you Johnny.

I don’t seem to have the same library as you. In the description it says v 2.0.0.
When I call “fieldTypeColumn” it throws an error. I searched in the library and there is no function by that name.
I downloaded the package v 2.0.1 from your site.
I tried commenting out those lines, but then it throws the same error I originally had when first trying to move to new version.
See attached screen shots.
Thanks again for your time.

I also am still not able to log into this forum from office it says cannot login from that ip.

Hello @JDodd , sorry my mistake, I thought that you were referring to Grocery CRUD Enterprise edition. I will check the same for the community edition and let you know.

Regards
Johnny

So @JDodd I’ve investigated the issue and on community edition the first argument is the value but the $row is on the second parameter. So your example will look like this:

$crud->setActionButton('Avatar', 'fa fa-plus', function ($value, $row) {
     return "SITE/maps?q=" . $row->lat . "," . $row->lng;
});

I will try to also update the documentation.

Let me know if that worked for you.

Regards
Johnny

1 Like

Thank You!
It works now.
Does the enterprise version have a live search function, where result matches are displayed as you enter the search string?

Hello @JDodd ,

The closest that we have for that is the setRelation. You can see an example at the datagrid here: setRelation | Grocery CRUD

Just scroll right to see the “City” quick search.

We also have the fieldType ‘dropdown_search’ that does pretty much the same.

If you are looking about an ajax search implementation then this is not available yet in Grocery CRUD Enterprise.

Regards
Johnny

The Ajax search would be a very nice addition.
Thanks again for your help.