Showing posts with label Yii2. Show all posts
Showing posts with label Yii2. Show all posts

Wednesday, 11 October 2017

Yii2 Pjax Reload Grid view and stay on same page


Yii 2 Pjax -    In order to stay in same page when using Pjax reload() use the below code,

 $.pjax.reload({
container:'.grid-view',
url: $('.grid-view li.active a').attr('href')
});



Yii2 Query Builder Like operator


To use "LIKE" operator in Yii2 query builder, use the below code,

$sql = "SELECT *  FROM  TABLE_NAME  WHERE item_code = :item_code AND DATE_FORMAT(req_date, '%Y-%m') LIKE :period";
                $req_date = '%' . $req_date . '%';
                $command = Yii::$app->db->createCommand($sql);
                $command->bindParam('item_code', $item_code);
                $command->bindParam('period', $req_date);
                $result = $command->execute();

Yii2 Sum Column in Active Record



To get sum of column using active record,
$query = Item::find();
            $query->where(['item_code' => $item_code]);
            $cost = $query->sum('lot_qty');

Wednesday, 10 August 2016

Yii2 href post – Post data using link tag



In Yii2, you can post data using link tag in a form with  data-method post, you can also pass data as params.


Example usage:
Html::a('<i class="fa fa fa-arrow-right"></i> Confirm Details', ['controller/action'], [
                        'class' => 'btn bg-blue btn-flat',
                        'data' => [
                            'method' => 'post',
                            'params' => [
                                'confirm-user' => '1',
                                'user_id' => '10',

                            ],
                        ]
                    ]);

Yii2 from events


In Yii2, below are the form events which we can make use it on front end / client side .

  • beforeValidate,
  • afterValidate,
  •  beforeValidateAttribute,
  • afterValidateAttribute,
  • beforeSubmit,
  •  ajaxBeforeSend,
  • ajaxComplete

Example usage:

$("#FORM-ID").on("afterValidate", function (event, messages) {

    // Now you can work with messages by accessing messages variable
 var attributes = $(this).data().attributes; // to get the list of attributes
    that has been passed in attributes property

  var settings = $(this).data().settings; // to get the settings

});

Yii2 model relation with multiple conditions

In AR Model Relations you can add multiple ON condition using andOnCondition method.
public function getInvoiceItem(){

return $this->hasOne(Invoice::className(), ['order_no' => 'order_no']) 

            ->andOnCondition(['item' => $this->order_item])           

            ->andOnCondition(['user_id' => $this->user_id]);
}


Sunday, 8 March 2015

Yii2 gridview row options

In Yii2 GridView, if you want to highlight or focus on certain row, you can use rowOptions attribute in your gridview.

Below are the example code.

<?=
GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
      'rowOptions' => function ($model, $index, $widget, $grid) {

                            if ($model->is_read == 1) {
                                return ['class' => 'read'];
                            } else {
                                return [];
                            }
                         },
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
         ['class' => 'yii\grid\ActionColumn']
    ],
]);
?>

Tuesday, 10 February 2015

Yii2 Modify GridView Filters


In Yii2  the filter input fields are automatically generated by the widget component, but we can modify to our needs by using the "filterPosition" property in Gridview widget.


To remove entire filters on Gridview,

'filterPosition'=>' ',

To show the fiters  on  top of each column's header cell,

  'filterPosition'=>'header',

To show the fiters  right below of each column's header cell,

 'filterPosition'=>'body',


To show the fiters  below each column's footer cell,

 'filterPosition'=>'footer',






Yii2 Dataprovider Default Sorting


To set the default sorting for Yii2 Data provider is

    $dataProvider = new ActiveDataProvider([
         'query' => $query,
         'sort'=> ['defaultOrder' => ['id' => 'DESC']]

     ]) 


    ( or )


   $dataProvider->sort = ['defaultOrder' => ['id' => 'DESC']]; 



Thursday, 5 February 2015

yii2 get logged in user details

 
//To get whole logged user data
$user = \Yii::$app->user->identity;

//To get id of f logged user
$userId = \Yii::$app->user->identity->id