我正在尝试获取在yii2的listview中选择的列表的id,将其分配给页面中的标签。列表显示良好,但如何选择一个数据,这是下面的列表视图,并将其分配给我,并回显它。
的index.php
<?php
use yii\widgets\ListView;
use yii\data\ArrayDataProvider;
use app\models\MyProfile;
use app\models\LikeDiscussion;
use yii\widgets\ActiveForm;
use common\models\Topic;
use common\models\Comment;
use common\models\Candidate;
use yii\widgets\Pjax;
use yii\helpers\Html;
use frontend\assets\AppAsset;
$this->title = 'My Yii Application';
?>
<?php
$query = Topic::find()->all();
$dataArr = array();
foreach ($query as $detail) {
$dataArr[] = array(
'id' => $detail->id,
'created' => $detail->created,
'creator' => $detail->creator,
'views' => $detail->views
);
}
$dataProvider = new ArrayDataProvider([
'allModels' => $dataArr,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'attributes' => ['views'=>SORT_ASC]
],
]);
?>
<?php Pjax::begin(['id' => 'countries']) ?>
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'comment-item'],
'itemView' => 'indexadapter',
]);
?>
<h5> <?php echo id; ?> </h5>
<div id="footer">
<footer class="row">
<p class="back-top floatright">
<a href="#top"><span></span></a>
</p>
<div class="two mobile-one columns">
<a href="#" class="postfix button expand">GO</a>
</div>
</div>
</div>
</footer>
</div>
这是indexadapter.php页面
<?php
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
//var_dump($model);exit;
?>
<div class="gravatar">
<img src="images/gravatar.png" alt="">
</div>
<h5><?php echo $model['topic']; ?>
</h5>
<p>
<h5>
<span class="commentdate">
<?php echo $model['created']. "by" .$model['creator']; ?>
</span>
</h5>
</p>
<hr>
答案 0 :(得分:0)
我刚编辑了indexadapter类。创建了一个href链接
<?php
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
//var_dump($model);exit;
?>
<div class="gravatar" >
<img src="images/gravatar.png" alt="" >
</div>
<h5><?php echo $model['topic']; ?>
</h5>
<a href="<?php echo $model['topic']; ?>">link</a>
<p>
<h5>
<span class="commentdate">
<?php echo $model['created']. "by" .$model['creator']; ?>
</span>
</h5>
</p>
<hr>