0

I have a GridView::widget which is populated with this method:

StabilimentiController.php

public static function getGridColumns($isMine = true, $readonly = false) {
 $c = [
            'denominazione',
            [
                'attribute'      => 'provincia',
                'value'          => 'indirizzo.provincia',
                'headerOptions'  => ['class' => 'table-col-provincia'],
                'contentOptions' => ['class' => 'table-col-provincia'],
            ],
            [
                'attribute'      => 'created_by',
                'value'          => 'azienda.ragioneSociale',
                'headerOptions'  => ['class' => 'table-col-createdBy'],
                'contentOptions' => ['class' => 'table-col-createdBy'],
            ]
    ];

return $c;
}

I have some problems in creating a searchable inputBox for the field created_by.

Created_by in my model is defined as integer, it's a code to identify univocally the company (azienda).

In my Stabilimenti.php model I have this method to catch it and then with the .notation inside the getGridColumns method I can reach the ragioneSociale field that is the one I'm interested in.

/**
     * @return \yii\db\ActiveQuery
*/
    public function getAzienda() {
        return $this->hasOne(Azienda::className(), ['id' => 'azienda_id'])->inverseOf('stabilimenti');
    }

The ouput is what I want, the only problem is that the searchable field in my GridView take as input the int code and not the ragioneSociale string, and is not very userFriendly.

In my StabilimentiSearch.php

public function rules(){
    return [
           [['id', 'azienda_id', 'created_by', 'updated_by'], 'integer'],
           [['denominazione', 'indirizzo', 'citta', 'localita', 'provincia', 'cap', 'produttore', 'utente', 'azienda' ], 'safe'],
             array('ragioneSociale', 'safe', 'on'=>'search'),
        ];

I put ragioneSociale as safe and with the search option, but I've some difficulties in bind it correctly to the inputBox.

Pickeroll
  • 757
  • 2
  • 7
  • 18

1 Answers1

1

See #29231013. If you want drop down, try this:

            [
                'attribute'      => 'created_by',
                'value'          => 'azienda.ragioneSociale',
                'headerOptions'  => ['class' => 'table-col-createdBy'],
                'contentOptions' => ['class' => 'table-col-createdBy'],
                'filter'         => ArrayHelper::map(Azienda::find()->asArray()->all(), 'id', 'ragioneSociale')
            ]

If you want inputBox and text search, see https://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview. Add property and filter condition to search model:


class StabilimentiSearch extends Stabilimenti
{
    public $ragioneSociale;

    public function rules()
    {
        return [
            // ... more stuff here
            [['ragioneSociale'], 'safe'],
            // ... more stuff here
        ];
    }

    public function search($params)
    {
        // create ActiveQuery
        $query = Tour::find();

        $query->joinWith(['azienda']);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        // No search? Then return data Provider
        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }
        // Some filters
        $query->andFilterWhere([
            //... other searched attributes here
        ])
        ->andFilterWhere(['like', 'azienda.ragioneSociale', $this->ragioneSociale]);

        return $dataProvider;
    }
}

And use this property as attribute in GridView:

            [
                'attribute'      => 'ragioneSociale',
                'value'          => 'azienda.ragioneSociale',
                'headerOptions'  => ['class' => 'table-col-createdBy'],
                'contentOptions' => ['class' => 'table-col-createdBy'],
            ]
Anton Rybalko
  • 1,041
  • 13
  • 21