Filter.php 3.48 KB
<?php

namespace backend\widgets;

use Yii;
use backend\widgets\Filter\Base;
use yii\web\View;

class Filter extends Base
{
    /**
     * 实例化自身
     * @return \YHGEditor\Brand\Client
     */
	public static function self()
	{
		return new self();
	}
    
    public function __construct() {
        self::_attributes_config('submit', array('class'=>'btn btn-primary btn-sm mr5'));
        self::_attributes_config('reset', array('class'=>'btn btn-primary btn-sm mr5'));
    }
    
    /**
     * 输出
     * @return type
     */
    protected function render(){
        $view = Yii::$app->getView();
        $view->registerCssFile('/css/select2.css',array('postion'=>View::POS_END));
        $view->registerJsFile('/js/select2.min.js',array('postion'=>View::POS_END));
        
        $script  = <<<EOT
        <script type="text/javascript">
        $(function(){
            jQuery(".select-search").select2();
            jQuery('.select-search-hide').select2({
                minimumResultsForSearch: -1
            });
        });
        </script>
EOT;
        $this->html($script);
        return parent::render();
    }
    
    public function reset($name = null, $value = '重置', array $attributes = NULL){
        $filterForm = self::$filterFormId;
        $script  = <<<EOT
        <script type="text/javascript">
        $(function(){
            filterResetTimeout = null;
            filterReset = function(obj){
                clearTimeout(filterResetTimeout);
                filterResetTimeout = setTimeout(function(){
                    $(obj).parents('form:eq(0)').find('select').each(function(){
                        var text = $(this).find('option:selected').text();
                        $(this).prev('div').find('span[id^=select2-chosen-]').text(text);
                    });
                }, 50)
            };
        });
        </script>
EOT;
        $this->html($script);
        return parent::reset($name, $value, array('onclick'=>'filterReset(this)'));
    }
    
    /*
     * 关键字查询
     */
    public function keyword($name = null, $value = NULL){
        if (!$name)
            $name = 'keyword';
        
        //构造当前元素
        $this
            ->html('<div class="form-group"><label class="control-label">关键字:</label>')
            ->input_equal($name, $value,array('class'=>'form-control','placeholder'=>"输入关键字"))
            ->html('</div>');
        return $this;
    }
    
    /*
     * 直播间状态select
     */
    public function liveStatus($name = null, $selected = NULL){
        if (!$name)
            $name = 'status';
        
        //选项
        $options = Yii::$app->params['config']['room_status'];
        $options = array(''=>'ALL') + $options;
        
        //构造当前元素
        $this
            ->html('<div class="form-group"><label class="control-label"> 状态:</label>')
            ->select($name, $options, $selected, array('class'=>'select-search-hide','style'=>'width:150px'))
            ->html('</div>');
        return $this;
    }
    
    /*
     * 关键字查询
     */
    public function masterName($name = null, $value = NULL){
        if (!$name)
            $name = 'name';
        
        //构造当前元素
        $this
            ->html('<div class="form-group"><label class="control-label">名称:</label>')
            ->input_equal($name, $value,array('class'=>'form-control','placeholder'=>"搜索主播名称"))
            ->html('</div>');
        return $this;
    }
    
}