duanchen7401 2014-07-19 09:56
浏览 37
已采纳

YII:如何在不调用窗口小部件的情况下注册yiiwheels窗口小部件资产

I use Yiiwheels. I want to change a combo box to select 2 widget, but the pertinent assets are not loaded yet. The ugly solution is creating a dummy select2 element in a hidden span, so assets are loaded automatically. but is there any better way to do that, I mean a function to load assets of a widget?

  • 写回答

1条回答 默认 最新

  • doz22551 2014-07-19 17:10
    关注

    The function you are looking for is the registerClientScript() function in WhSelect2 widget class in Yiiwheels.

    While creating an empty widget is the easiest way to register the script you can alternatively call it directly with something like this

    <?php Yii::import('yiiwheels.widgets.select2.WhSelect2');
        $w = new WhSelect2(); 
        $w->registerClientScript();
    ?>
    

    The registerClientScript() function is called in init()call during widget creation, the following is operations executed by this function See WhSelect2 on github for the full code

       /**
        * Registers required client script for bootstrap select2. It is not used through bootstrap->registerPlugin
        * in order to attach events if any
        */
        public function registerClientScript()
        {
            /* publish assets dir */
            $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
            $assetsUrl = $this->getAssetsUrl($path);
    
            /* @var $cs CClientScript */
            $cs = Yii::app()->getClientScript();
    
            $cs->registerCssFile($assetsUrl . '/css/select2.css');
            $cs->registerScriptFile($assetsUrl . '/js/select2.js');
    
    
            if ($this->language) {
                $cs->registerScriptFile(
                    $assetsUrl . '/js/locale/select2_locale_' . $this->language . '.js',
                    CClientScript::POS_END
                );
            }
    
            /* initialize plugin */
            $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
    
            $this->getApi()->registerPlugin('select2', $selector, $this->pluginOptions, CClientScript::POS_READY);
            $this->getApi()->registerEvents($selector, $this->events, CClientScript::POS_READY);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集