doutong2132 2018-04-04 12:56
浏览 38

我想从gridview复制数据并放在另一个页面Yii2上?

I've selected id from the button. Every time I select id to be copied to another page. Like a Cart Shoping, where you can see the selected products.

Here's what I've done

in Controller

public function actionClone($id)
{
    $model = $this->findModel($id);

    $newModel = new Masa();
    $newModel->attributes = $model->attributes;        
    $newModel->save(false);
    $newModel->save();

}

public function actionTabel()
{
    $searchModel = new MasaSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    foreach( $model->models as $id) {

        $newModel = new Masa();

        $newModel->attributes = $id->attributes; 
        $newModel->save();
     }

     return $this->render('tabel',[
         'models'=>$newModel,
     ]);
}

in view

 'clone' => function ($url, $model) {
                return Html::a('<span class="glyphicon glyphicon-floppy-open">Clonare</span>',
                Yii::$app->urlManager->createUrl(['masa/clone', 'id' => $model->id]),
                [
                    'title' => Yii::t('yii', 'Clonare'),
                    'url' => Url::to(["/masa/clone", 'id' => $model->id]),
                ]
                ) . "</li>";
               },

I have two attributes id and name,for simplicity.

  • 写回答

1条回答 默认 最新

  • dtf24224 2018-04-04 15:09
    关注

    try to change method to GET , and now the data will be state in the url , and when you go back or return, the data not lose .

    Html::a('<span class="glyphicon glyphicon-floppy-open">Clonare</span>',
        Yii::$app->urlManager->createUrl(['masa/clone', 'id' => $model->id]),
        [
            'title' => Yii::t('yii', 'Clonare'),
            'url' => Url::to(["/masa/clone", 'id' => $model->id]),
        ]
    )
    

    If you don't like to show ID you can encrypt

    $decrypt = \Yii::$app->security->decryptByKey($_GET['id'], \Yii::$app->request->cookieValidationKey);
    $encrypt=\Yii::$app->security->encryptByKey($id, \Yii::$app->request->cookieValidationKey);
    

    If you want to show a new view, you must create one in the same route as the other "masa" views

    New view masa/view.php

    <div class="post-view">
    
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">Informaction of the Masa</h3>
            </div>
            <div class="box-body">
                <?= DetailView::widget([
                    'model' => $model,
                    'attributes' => [
                        [
                            'attribute' => 'MassaId',
                            'value' => $model->id,
                        ]
                       //....
                    ]
                ])
                ?>
            </div>
        </div>
    
    </div>
    

    Controller

    public function actionClone($id)
    {
        $model = $this->findModel($id);
    
        $newModel = new Masa();
        $newModel->attributes = $model->attributes;        
        $newModel->save(false);
        $newModel->save();
    
        return $this->render('view', [
                'model' => $model,
            ]);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序