简体   繁体   中英

How to Update,View,Delete as per login user in yii2?

在此输入图像描述

I have to change the the id of view,update,delete as per login user but when I click on View I get ID of index.I wanna change a particular company as per id..Please help me I am new in yii2...Thanks in advance. actionIndex()

$searchModel = new VendorsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->pagination->pageSize = 10;
        $user = \Yii::$app->user->identity;
        $userid = \Yii::$app->user->identity->id;

         $query1 = new \yii\db\Query;
            $query1->select('*')->from('vendors')->where(['ven_contact_person_id' => $userid,'deleted' => 'N']);
            $query1->createCommand();

            $dataProvider1 = new ActiveDataProvider([
                'query' => $query1,
                'pagination' => false,
            ]);



        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'dataProvider1' => $dataProvider1,
        ]);

index.php

    <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'dataProvider' => $dataProvider1,
            'filterModel' => $searchModel,
            'columns' => [
            'ven_id',
            'ven_company_name',
            'ven_website',
             'ven_contact_no',
             'ven_email_id:email',
          ['class' => 'yii\grid\ActionColumn',
              'header' => 'Action',
              'template' => '{view} {edit} {delete}',
              'buttons' => [
                 'view' => function ($url, $model) {
                   return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url);
                 },
                 'edit' => function ($url, $model) {
                   return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url);
                 },
                 'delete' => function ($url, $model) {
                   return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url);
                 },
              ],
              'urlCreator' => function ($action, $dataProvider, $key, $index) {

                if ($action === 'view') {
                     return Url::to(['vendors/view', 'id' =>$dataProvider['ven_id']]);
                }
                if ($action === 'edit') {
                    return Url::to(['/vendors/update', 'id' =>$dataProvider['ven_id']]);
                }
                if ($action === 'delete') {
                   return Url::to(['/vendors/delete', 'id' =>$dataProvider['ven_id']]);
                }                    
                return $url;
            }
          ],
      ],
]); ?>

For icon and model->id i think you need this

   GridView::widget([
      'dataProvider' => $dataProvider,
      'filterModel' => $searchModel,
      'columns' => [
          'name',
          ['class' => 'yii\grid\ActionColumn',
              'header' => 'Action',
              'template' => '{view} {edit} {delete}',
              'buttons' => [
                 'view' => function ($url, $model) {
                   return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url);
                 },
                 'edit' => function ($url, $model) {
                   return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url);
                 },
                 'delete' => function ($url, $model) {
                    return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url,[
                    'title' => Yii::t('yii', 'Delete'),
                    'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                    'data-method' => 'post',
                ]);
                 },
              ],
              'urlCreator' => function ($action, $model, $key, $index) {

                if ($action === 'view') {
                    $url = Url::to(['/yourController/view', 'id' =>$model->id]);
                }
                if ($action === 'edit') {
                    $url = Url::to(['/yourController/edit', 'id' =>$model->id]);
                }
                if ($action === 'delete') {
                    $url = Url::to(['/yourController/delete', 'id' =>$model->id]);
                }                    
                return $url;
            }
          ],
      ],
  ]);

sorry for adding a "answer" (coulden't add an comment it requires reputation >= 50)

could you add the index view rendered file ?

in my opinion this will be a mistake in that file for example the method that works for me:

GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        'name',
        ['class' => 'yii\grid\ActionColumn',
            'header' => 'Action',
            'template' => '{view} {edit} {delete}',
            'buttons' => [
               'view' => function ($url, $model) {
                 return Html::a('View', $url);
               },
               'edit' => function ($url, $model) {
                 return Html::a('Edit', $url);
               },
               'delete' => function ($url, $model) {
                 return Html::a('Delete', $url);
               },
            ],
        ],
    ],
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM