K
K
Kurper2019-08-05 15:53:53
Yii
Kurper, 2019-08-05 15:53:53

How can you get the GridView attributes for your widget?

Hello everyone,
Maybe someone faced such a task, wrote my own widget, I want to use the attributes of the GridView table in it, but now it works manually, that is, I pass these attributes by writing them ...
something like this:
MyWidget:: widget([
attributes => ['foo', 'bar' ...]
]);
Is it possible to get these attributes from the GridView? I want to automatically take all the columns (attributes) that are currently active.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2019-08-05
@Kurper

If your widget is inherited from the grid, then yes. You can get the attributes property.
The second solution is if these two widgets are on the same page. Then you can create a $attributes variable where to add common variables and add it to both widgets.
But I am more than sure that you do not need to do this. First of all, widgets are created to solve logical operations and the widget is considered independent. That is, it is enough to pass parameters to configure the widget. For example, the VKontakte comments widget. Sometimes widgets are used in a completely wrong way, making them not helpers, but an additional problem and a vulnerable part of the site.
Sometimes it's better to copy-paste than to guess for two days on coffee grounds where it came from. Don't create problems for yourself out of the blue. Especially if these two widgets are independent of each other. The copy-paste logic must be implemented not in these widgets, but outside ... For example, as I wrote above. Or a helper...
It's worth just looking at how it's implemented in Yii2. Three widgets GridView, ListView, DetailView. These widgets use the same attributes of the same model. But there no one did it the way you want.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question