1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| 一、解决父级添加不上: 修改<project>/vendor/mdmsoft/yii2-admin/models/Menu.php中 原来: public function rules() { return [ [['name'], 'required'], [['parent_name'], 'in', 'range' => static::find()->select(['name'])->column(), 'message' => 'Menu "{value}" not found.'], [['parent', 'route', 'data', 'order'], 'default'], [['parent'], 'filterParent', 'when' => function() { return !$this->isNewRecord; }], [['order'], 'integer'], [['route'], 'in', 'range' => static::getSavedRoutes(), 'message' => 'Route "{value}" not found.'] ]; } 修改后: public function rules() { return [ [['name'], 'required'], [['parent_name'], 'in', 'range' => static::find()->select(['name'])->column(), 'message' => 'Menu "{value}" not found.'], [['parent', 'route', 'data', 'order'], 'default'],
[['parent_name'], 'filterParent'], [['order'], 'integer'], [['route'], 'in', 'range' => static::getSavedRoutes(), 'message' => 'Route "{value}" not found.'] ]; } 原来: public function filterParent() { $parent = $this->parent; $db = static::getDb(); $query = (new Query)->select(['parent']) ->from(static::tableName()) ->where('[[id]]=:id'); while ($parent) { if ($this->id == $parent) { $this->addError('parent_name', 'Loop detected.'); return; } $parent = $query->params([':id' => $parent])->scalar($db); } } 修改后: public function filterParent() { $value = $this->parent_name; $parent = self::findOne(['name' => $value]); if ($parent) { $id = $this->id; $parent_id = $parent->id; while ($parent) { if ($parent->id == $id) { $this->addError('parent_name', 'Loop detected.');
return; } $parent = $parent->menuParent; } $this->parent = $parent_id; } } 二、解决意图提示样式 下载 yiisoft/yii2-jui php composer.phar require --prefer-dist yiisoft/yii2-jui
现在已经可以上试试效果,完全没有问题,如果不想用form中原来的书写方式可以按下面的办法; 修改 <project>/vendor/mdmsoft/yii2-admin/views/menu/_form.php中parent_name和route表单: 修改之前: <?= $form->field($model, 'parent_name')->textInput(['id' => 'parent_name']) ?> <?= $form->field($model, 'route')->textInput(['id' => 'route']) ?> 修改之后: <?= $form->field($model, 'parent_name')->widget('yii\jui\AutoComplete',[ 'options'=>['class'=>'form-control'], 'clientOptions'=>[ 'source'=> Menu::find()->select(['name'])->column() ] ]) ?>
<?= $form->field($model, 'route')->widget('yii\jui\AutoComplete',[ 'options'=>['class'=>'form-control'], 'clientOptions'=>[ 'source'=> Menu::getSavedRoutes() ] ]) ?> 三、在 <project>/vendor/mdmsoft/yii2-admin/views/role/_form.php 同样会有样式问题,按照二的步骤修改即可 使用到的时候再改就可以。 修改后的: <?= $form->field($model, 'ruleName')->widget('yii\jui\AutoComplete', [ 'options' => [ 'class' => 'form-control', ], 'clientOptions' => [ 'source' => array_keys(Yii::$app->authManager->getRules()), ] ]) ?>
|