yii2 widget实现筛选记录

测试控制器代码:

namespace frontend\controllers;


use yii\web\Controller;

class TestlistController extends Controller
{

    public function actionIndex(){
        $cityd = 12;
        return $this->render('index',['cityid'=>$cityd]);
    }
}

view中引用代码


use frontend\components\widget\Filter;
?>
<link type="text/css" href="<?php echo DOMAIN; ?>/css/list_fang.css" rel="stylesheet" />
<div id="header">


</div>

<?=Filter::widget(['cityid'=>320200]) ?>

在frontend/components中新建ListWidget.php,内容如下

<?php

/*
 * 列表页筛选
 */
namespace frontend\components;

use yii\base\Widget;


class ListWidget extends Widget
{
    public function init()
    {
       	ob_start();
		ob_implicit_flush(false);
		ob_clean();
    }

    public function run()
    {

		$content=ob_get_clean();

		echo $this->renderContent();

    }
    protected function renderContent()
    {
    }
    
    
}


在frontend/components/widget中新建Filter.php,内容如下


<?php
/*
 * 列表页筛选项
 *
 */

namespace frontend\components\widget;
use common\components\AddressUtils;
use frontend\components\ListWidget;

class Filter  extends  ListWidget
{
    public $cityid;

    public function init()
    {
        parent::init();
    }

    protected function renderContent()
    {
        $areaList = AddressUtils::getCityList($this->cityid);
        return  $this->render('Filter',['areaList'=>$areaList]);
    }


}

在frontend/components/widget/views中新建Filter.php视图,内容如下

<?php

$arr['k'] = Yii::$app->request->get('k','');
$arr['areaid'] = Yii::$app->request->get('areaid','0');



?>
<div id="selection">

    <dl class="secitem">
        <dt>区域:</dt>
        <dd>
            <a href="<?php echo Yii::$app->urlManager->createUrl(['/site/index','areaid'=>0]); ?>"<?php if(!$arr['areaid']):?> class="select"<?php endif;?>>全<?php echo $cityname; ?></a>
            <?php foreach ($areaList as $key => $val): ?>
                <a href="<?php echo Yii::$app->urlManager->createUrl(['/site/index','areaid'=>$key]); ?>"<?php if($arr['areaid']==$key):?> class="select"<?php endif;?>><?php echo $val.$key; ?></a>
            <?php endforeach; ?>
        </dd>
    </dl>


    <div id="SearchForm">
        <input type="text" value="<?php echo $arr['k']; ?><?php echo $cityid ?>" class="keyword" id="keyword1" name="keyword">
        <span class="sbtn"><input type="submit" id="searchbtn1" class="atcate" value="搜本类"></span>
        <span class="sbtn"><input type="button" class="global" value="搜全站" οnclick="b_query_all()"></span>

效果如下:


页面ui没做,只实现功能部分,url地址为http://www.cf.com/site/index?areaid=320205

只做了一个字段areaid的效果,如有多个,只要在frontend/components/widget/views,中添加就可以

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值