0

中,jui 资产位于以下路径中:web\assets\135efca3\在其中找到themesui文件夹-确实,我不知道为什么 Yii2 会制作不同的路径段或假路径-。我认为assetManager 的getAssetPath()方法应该返回该路径,但我不知道如何!

actions()我在控制器的方法中尝试了以下调试代码:

public function actions()
{
   echo Yii::$app->assetManager->getAssetPath(Yii::$app->assetManager->bundles = [
          'yii\jui\JuiAsset'], 'themes');
          die(); //for debugging
} 

但是,它只是打印/themes.

换句话说,我可以问,我怎么能提供的第一个参数(对象)getAssetPath()?因为,我认为这是这里的问题。

编辑

我创建了以下助手 -根据关于路径的 arogachev 回答- 以获取主题列表。

<?php
namespace common\libs;
use yii;
use yii\web\Controller;

    class JuithemeHelpers
    {
      public static function getThemesList()
      {
        $themesPath =  dirname(Yii::$app->basePath).DIRECTORY_SEPARATOR."vendor".DIRECTORY_SEPARATOR."bower".DIRECTORY_SEPARATOR."jquery-ui".DIRECTORY_SEPARATOR."themes";
        $output = [];
        foreach (scandir($themesPath) as $item){
          if (is_dir($themesPath.DIRECTORY_SEPARATOR.$item) && ($item != '.' && $item !='..')) $output[] = $item;
        }
        return $output;
      }
    }

然后在视图中我做了以下内容:

...
<?= $form->field($model, 'birthdate')->widget(DatePicker::className(), ['clientOptions' => ['dateFormat' => 'yy-mm-dd', 'changeYear' => true, 'yearRange' => sprintf('%s:%s', date('Y')-100,date('Y')-16)],]) ?>
    
      <select onchange="changeTheme(this.value)">
      <?php foreach (JuithemeHelpers::getThemesList() as $item): ?>
              <option value="<?= $item ?>"><?= $item ?></option>
        <?php       endforeach; ?>
      </select>
    
    
    <?= $form->field($model, 'gender_id')->dropDownList($model->getGenderList(), ['prompt' => 'Please Select one...']) ?>


  ...
......
<script>
    function changeTheme(n){  
      s = document.getElementsByTagName('link');
      o = ''
      re = /\/themes\/(.*)\/jquery-ui.css/gi;
      for (i = 0; i < s.length; i++){
        if (s[i].href.match(re)){
          
                o = s[i].href.replace(re.exec(s[i].href)[1],n);
                s[i].href=o;
        }       
      }
    }  
      </script>

我认为现在是学习如何将所有这些打包到一个小部件中的时候了。

4

1 回答 1

1

jQuery UI(默认情况下带有框架)主题文件夹位于/vendor/bower/jquery-ui/themes文件夹中。

您可以通过检查yii\jui\JuiAsset$sourcePath的ans$css属性来查看它。

您可以使用 alias 代替编写完整路径@bower

Yii::getAlias('@bower/jquery-ui/themes');

对于列表,您可以使用例如此方法

$themesPath = Yii::getAlias('@bower/jquery-ui/themes');
$results = scandir($themesPath);

foreach ($results as $result) {
    if ($result === '.' || $result === '..' || !is_dir($themesPath . '/' . $result)) {
        continue;
    }

    echo $result . "<br/>";
}
于 2015-01-23T17:59:24.570 回答