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);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来