基于ID的YII2渲染网站

发布于 2025-01-24 23:27:40 字数 1769 浏览 2 评论 0原文

希望有人可以帮助我。我在单击GridView后试图渲染一个站点,并且可以正常工作,但无法接缝以使其正确渲染,并且我不确定我在控制器中的功能是否正确,因为我需要根据类别ID渲染网站,并使用日期渲染该网站。对于另一个桌子。 我的网址是 /web/index.php?r=site%2FSubCategory?id=1

我的视图

<?= GridView::widget([
                    'dataProvider' => $dataProvider,
                    'rowOptions'   => function ($model,) {
                        return ['data-id' => $model->id];
                    },
                    'columns' => [
                        [
                            'header' => 'Titel',
                            'attribute' => 'name',                            
                        ],
                        [
                            'header' => 'Beskrivelse',
                            'attribute' => 'description'
                        ],
                        
                    ]
                ]) ?>      
            </div>
                    
        </div>

    </div>
</div>
<?php
$this->registerJs("

    $('td').click(function (e) {
        var id = $(this).closest('tr').data('id');
        if(e.target == this)
            location.href = '" . Url::to(['site/subcategory']) . "?id=' + id;
    });

");?

控制器函数

public function actionSubcategory($id)
    {
        
        $category = new Category();
        $category->getCategorybyid($id);
        $Subcategory = new Subcategory();
        $dataProvider = new ActiveDataProvider([
            'query' => $Subcategory->getSubcategory(),
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        return $this->render('subcategory', [
            'dataProvider' => $dataProvider]);

    }

hope there is someone that can help me. im trying to render a site after i clicked on my gridview and it works fine but can´t seam to get it render properly and im not sure if my function in controller is correct becoures i need to render the site based on category id with date for another table.
my url is
/web/index.php?r=site%2Fsubcategory?id=1

my view

<?= GridView::widget([
                    'dataProvider' => $dataProvider,
                    'rowOptions'   => function ($model,) {
                        return ['data-id' => $model->id];
                    },
                    'columns' => [
                        [
                            'header' => 'Titel',
                            'attribute' => 'name',                            
                        ],
                        [
                            'header' => 'Beskrivelse',
                            'attribute' => 'description'
                        ],
                        
                    ]
                ]) ?>      
            </div>
                    
        </div>

    </div>
</div>
<?php
$this->registerJs("

    $('td').click(function (e) {
        var id = $(this).closest('tr').data('id');
        if(e.target == this)
            location.href = '" . Url::to(['site/subcategory']) . "?id=' + id;
    });

");?

Controller function

public function actionSubcategory($id)
    {
        
        $category = new Category();
        $category->getCategorybyid($id);
        $Subcategory = new Subcategory();
        $dataProvider = new ActiveDataProvider([
            'query' => $Subcategory->getSubcategory(),
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        return $this->render('subcategory', [
            'dataProvider' => $dataProvider]);

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文