yii2 widget实现筛选记录

来源:互联网 发布:网络电视直播软件apk 编辑:程序博客网 时间:2024/04/30 05:16

测试控制器代码:

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="搜全站" onclick="b_query_all()"></span>

效果如下:


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

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

0 0
原创粉丝点击