簡體   English   中英

如何在yii2中按登錄用戶更新,查看,刪除?

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

在此輸入圖像描述

我必須根據登錄用戶更改視圖,更新,刪除的ID,但是當我點擊查看時我會獲得index的ID。我想根據id更改某個公司..請幫助我,我是yii2的新手.. 。提前致謝。 的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;
            }
          ],
      ],
]); ?>

對於icon和model-> id,我認為你需要這個

   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;
            }
          ],
      ],
  ]);

抱歉添加“回答”(不要添加評論,要求聲譽> = 50)

你能添加索引視圖渲染文件嗎?

在我看來這將是該文件中的一個錯誤,例如對我有用的方法:

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);
               },
            ],
        ],
    ],
]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM