Friday 25 September 2015

Prevent Form Submission When User Presses the Enter key / Move to next textbox when user presses enter key


 When we filling out the web forms, if we hit the Enter key accidentally, then the form will submit, it makes us frustrating every time.

To prevent this you can use the code below. Instead of submitting, it actually focus / moves to next input boxes when we press Enter key.

$('body').on('keydown', 'input, select, textarea', function(e) {
    var self = $(this)
            , form = self.parents('form:eq(0)')
            , focusable
            , next
            ;
    if (e.keyCode == 13) {
        focusable = form.find('input,a,select,textarea').filter(':visible');
        next = focusable.eq(focusable.index(this) + 1);
        //  console.log(next.attr('type'));
        if (next.length && (next.attr('type') != 'button')) {
            next.focus();
        } else {
            form.submit();
        }
        return false;
    }
});


Wednesday 22 April 2015

Wordpress admin panel custom landing page after login

Redirect to different landing page after successful login on wordpress admin panel,

/*To make non-admin users redirect to specified landing page*/

function your_login_redirect()
{

if(!is_super_admin()){

            // pick where to redirect to, in the example: Posts page
            return admin_url( 'edit.php' );
        } else {
            return admin_url();
        }
   
}
add_filter( 'login_redirect', 'your_login_redirect');



Wordpress Disable Admin Panel Menus Based on Role

In wordpress admin panel, if you want to hide certain menu's for non-admins , then add the below code in your current theme's functions.php file.


function remove_menus () {
global $menu;

        $restricted = array(__('Dashboard'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'),  __('Plugins'));
        end ($menu);
        while (prev($menu)){
            $value = explode(' ',$menu[key($menu)][0]);
            if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
        }
}
      /* To hide menus for non -admin users*/
if(!is_super_admin()){
add_action('admin_menu', 'remove_menus');   
}


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