We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Rendering Grid from addon settings page

Developer Preview

Bjørn Børresen's avatar
Bjørn Børresen
629 posts
9 years ago
Bjørn Børresen's avatar Bjørn Børresen

Trying to use the Grid field in my addon settings page: https://docs.expressionengine.com/latest/development/services/table.html

However, using this code in my template to render the grid html doesn’t work:

<?php $this->view('_shared/table', $grid); ?>

It outputs a PHP error:

Fatal error: Call to undefined method EllisLab\ExpressionEngine\Service\View\View::view() in views/filename.php

I see the native addons use $this->embed(‘_shared/table’, $table); instead of $this->view() but if I try that I’ll just get “View file not found”.

       
Bjørn Børresen's avatar
Bjørn Børresen
629 posts
9 years ago
Bjørn Børresen's avatar Bjørn Børresen

Managed to get it working by rendering the grid in the mcp.file.php instead of the view, like this:

$vars['grid_html'] = ee('View')->make('_shared/table')->render($grid->viewData());

But according to the docs the code in my previous post should work?

       
Bjørn Børresen's avatar
Bjørn Børresen
629 posts
9 years ago
Bjørn Børresen's avatar Bjørn Børresen

One more question - how do I modify the “+” button at the bottom of the grid (Add new row) .. it has hover title “add_new_row” and clicking it doesn’t do anything.

       
Kevin Cupp's avatar
Kevin Cupp
791 posts
9 years ago
Kevin Cupp's avatar Kevin Cupp

You should be using $this->embed(), sorry, I’ll update the docs for that. Since you’re using it in an add-on, you have to specify the namespace. Try this:

$this->embed('ee:_shared/table', $table);

I’ll update the docs for that, too.

As for the add_new_row, try loading the content_lang file, or specify a lang key of “add_new_row” yourself and make it anything you want.

As for the JS seemingly not working, did you call loadAssets() on the Grid object?

       
Bjørn Børresen's avatar
Bjørn Børresen
629 posts
9 years ago
Bjørn Børresen's avatar Bjørn Børresen
As for the JS seemingly not working, did you call loadAssets() on the Grid object?

I’ve called loadAssets(). I think the JS is working fine, as the remove row option works (although it doesn’t do anything else than remove the row visually).

”+” does nothing though.

How to I hook onto the “+” and “remove row” to make sure it does what I want them to do?

Also, it just occurred to me that it might be better to use Table if Grid handles the storing of data. Does it? I’m already storing the data in my own table (converting an existing addon).

Thanks!

       
Bjørn Børresen's avatar
Bjørn Børresen
629 posts
9 years ago
Bjørn Børresen's avatar Bjørn Børresen

Did the above make sense, Kevin?

       
Kevin Cupp's avatar
Kevin Cupp
791 posts
9 years ago
Kevin Cupp's avatar Kevin Cupp
”+” does nothing though.

Hard to say. Are you using the shared form view to make this? If so, do you have the grid setting set on your fieldset definition? If not, have you followed the style guide in implementing Grid? It needs to live in a fieldset with a class of grid-publish.

How to I hook onto the “+” and “remove row” to make sure it does what I want them to do?

You can write your own click handlers for them if you want via jQuery.

Also, it just occurred to me that it might be better to use Table if Grid handles the storing of data. Does it? I’m already storing the data in my own table (converting an existing addon).

No, storing and populating the data is up to you, GridInput just provides the UI.

       
Bjørn Børresen's avatar
Bjørn Børresen
629 posts
9 years ago
Bjørn Børresen's avatar Bjørn Børresen
Hard to say. Are you using the shared form view to make this? If so, do you have the grid setting set on your fieldset definition?

Fieldset definition? This is just used in the control panel file (mcp) of my addon.

You can write your own click handlers for them if you want via jQuery.

Yeah, is that the correct way of doing it? I’m confused as the docs have a section for using Grid in the CP file but no further info on how to hook onto the “Delete row” / Add new row functionality.

       
Kevin Cupp's avatar
Kevin Cupp
791 posts
9 years ago
Kevin Cupp's avatar Kevin Cupp
Fieldset definition? This is just used in the control panel file (mcp) of my addon.

I’m not sure what you’re trying to say. How are you generating the markup that surrounds your Grid input?

Yeah, is that the correct way of doing it? I’m confused as the docs have a section for using Grid in the CP file but no further info on how to hook onto the “Delete row” / Add new row functionality.

We wouldn’t document it because it should be unnecessary to do. If you get the Grid JS working as it should, then these buttons work as intended. I was answering the question from the point of view of if you’re not happy with what those buttons do, then you can create your own click handler, but we wouldn’t want to encourage that because it may introduce inconsistency in the UI.

       
Bjørn Børresen's avatar
Bjørn Børresen
629 posts
9 years ago
Bjørn Børresen's avatar Bjørn Børresen
I’m not sure what you’re trying to say. How are you generating the markup that surrounds your Grid input?

Here’s the code: http://pastebin.com/raw/d9CHhNEK

Basically I just use:

$vars['grid_html'] = ee('View')->make('_shared/table')->render($grid->viewData());
       
Kevin Cupp's avatar
Kevin Cupp
791 posts
9 years ago
Kevin Cupp's avatar Kevin Cupp

Ok, and in your view file where you’re rendering your $grid_html variable, what code is around it? Did you follow the style guide section I linked to?

       
Bjørn Børresen's avatar
Bjørn Børresen
629 posts
9 years ago
Bjørn Børresen's avatar Bjørn Børresen

Nope, I’ve been following this:

https://docs.expressionengine.com/latest/development/services/table.html

       
Bjørn Børresen's avatar
Bjørn Børresen
629 posts
9 years ago
Bjørn Børresen's avatar Bjørn Børresen

Ok, so wrapping it in the code from the styleguide makes the “+” actually do something, which is good, but I get a JS error: http://i.imgur.com/OcB9cJw.jpg

The docs could use a complete example for this .. right now the Grid section under the CP/Table service makes no mention of the required code around it.

       
Kevin Cupp's avatar
Kevin Cupp
791 posts
9 years ago
Kevin Cupp's avatar Kevin Cupp

I’ll add a note to the docs follow the style guide when including a Grid arbitrarily in a view file, thanks for pointing that out.

       
Kevin Cupp's avatar
Kevin Cupp
791 posts
9 years ago
Kevin Cupp's avatar Kevin Cupp

Hard to explain the JS error, could you also share your view file code? I’ll see if I can reproduce.

       
1 2

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.