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 Pagination Set Default Value


To set yii2 data provider default pagination, the code is
    
 $dataProvider = new ActiveDataProvider([
         'query' => $query,
         'pagination'=> ['defaultPageSize' => 50]

     ]) 


    ( or )


  $dataProvider->pagination = ['defaultPageSize' => 50];



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



Monday, 17 March 2014

Yii CGridView Update onkeyup

By default Yii filters the table in cgridview, when a user entering data on search textfield and press Enter key or clicking oustside the grid.
I had a strange situation, i want to apply filter on keyup in cgridview for all pages,

Below is the code to apply filter on keyup in  yii cgridview.

    $(function() {
        $('body').on('keyup', '.filters > td > input', function() {
           
            var focusedId = $(document.activeElement).attr('id');
            var grid_id = $('#' + focusedId + '.form').closest("div").attr('id');
           
            $('#' + grid_id).yiiGridView('update', {
                data: $(this).serialize(),
                complete: function(jqXHR, status) {
                   
                    if (status == 'success') {
                        var tmpStr = $('#' + focusedId + '.form').val();
                        $('#' + focusedId + '.form').focus();
                        $('#' + focusedId + '.form').val('');
                        $('#' + focusedId + '.form').val(tmpStr);
                    }
                   
                }
            });
            return false;
        });

    });

Tuesday, 17 December 2013

Get date of seventh day from today using javascript

Here, i posted the code below to get the date of seventh day from today using javascript.

Code:

 step:1
 var date = new Date();   //to get current day's date with GMT
//i.e, the variable date outputs as Tue Dec 17 2013 00:48:41 GMT+0530 (India Standard Time)

 step:2 
 date.setDate(date.getDate() + 7);
//Now the variable date holds the timestamp of the seventh'day from today i.e, 1387826522433

  step:3
 var dateMsg = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
alert(dateMsg); 
 //just checking with a alert , 
 alert(dateMsg); 
//Now variable  dateMsg outputs with format as 2013-12-24,

Finally ,we displayed the date of the seventh day from today using javascript.
 
 alert(dateMsg);