public function run() { SelectAsset::register($this->view); FilterAsset::register($this->view); $values = []; foreach ($this->data as $value) { $value = strval($value); $values[$value] = $value; } if (!$this->default) { $this->default = $this->multiple ? array_keys($values) : key($values); } $selected = $this->selected($this->default); // Setup options $options = ['id' => $this->name, 'name' => $this->name . '[]', 'style' => 'width: 300px;', 'class' => 'selectpicker']; $extra = ['title' => 'Not selected']; if ($this->multiple) { $extra['multiple'] = 'multiple'; } if ($this->placeholder) { $extra['title'] = strval($this->placeholder); } $options = array_merge($options, $extra); if (!$this->method) { $this->method = 'get'; } // Render echo Html::beginForm(Url::canonical(), $this->method, ['data-pjax' => '1', 'id' => $this->name]); echo Html::beginTag('select', $options, ['data-pjax' => '1']); echo Html::renderSelectOptions($selected, $values); echo Html::endTag("select"); echo Html::endForm(); parent::run(); }
public function run() { $id = $this->getId(); echo Html::beginForm(Url::canonical(), 'get', ['data-pjax' => '1', 'id' => $this->name]); echo Html::beginTag('div'); echo Html::beginTag("select", ['id' => $id, 'multiple' => 'multiple', 'class' => 'invisible', 'style' => 'width: ' . $this->width . 'px;', 'name' => $this->name . '[]']); echo Html::renderSelectOptions($this->selectedValues, $this->_allValues); echo Html::endTag("select"); echo Html::submitButton('OK', ['style' => 'margin: 0 5px;']); echo Html::endTag('div'); echo Html::endForm(); echo "<script type='text/javascript'>"; echo "\$('#{$id}').removeClass('invisible');"; echo "\$('#{$id}').multipleSelect()"; echo "</script>"; }
/** * Рендерит html населенных пунктов для dropdown * @param int $id идентификатор района * @throws \yii\base\ExitException */ public function actionNps($id) { $all = Np::find()->published()->where(["rajon_id" => $id])->orderBy(["title" => SORT_ASC])->all(); $o = ["prompt" => ""]; echo Html::renderSelectOptions(null, ArrayHelper::map($all, "id", "title"), $o); Yii::$app->end(); }
public function run() { $parent_id = Yii::$app->request->get('parent_id'); $modelClass = $this->model; if ($parent_id > 0) { return Html::renderSelectOptions('district', $modelClass::getRegion($parent_id)); } else { return []; } }
public function init() { if (!$this->model) { throw new InvalidParamException('model不能为null!'); } if (empty($this->province) || empty($this->city)) { throw new InvalidParamException('province和city不能为空!'); } $cityId = Html::getInputId($this->model, $this->city['attribute']); if (empty($this->city['options']['prompt'])) { $this->city['options']['prompt'] = '选择城市'; } $cityDefault = Html::renderSelectOptions('city', ['' => $this->city['options']['prompt']]); if (!empty($this->district)) { if (empty($this->district['options']['prompt'])) { $this->district['options']['prompt'] = '选择县/区'; } $districtId = Html::getInputId($this->model, $this->district['attribute']); $districtDefault = Html::renderSelectOptions('district', ['' => $this->district['options']['prompt']]); $this->city['options'] = ArrayHelper::merge($this->city['options'], ['onchange' => "\r\n if(\$(this).val()!=''){\r\n \$.get('{$this->url}?parent_id='+\$(this).val(),function(data){\r\n \$('#{$districtId}').html('{$districtDefault}'+data);\r\n })\r\n }else{\r\n \$('#{$districtId}').html('{$districtDefault}');\r\n }\r\n "]); } $this->province['options'] = ArrayHelper::merge($this->province['options'], ['onchange' => "\r\n if(\$(this).val()!=''){\r\n \$.get('{$this->url}?parent_id='+\$(this).val(),function(data){\r\n \$('#{$cityId}').html('{$cityDefault}'+data);\r\n })\r\n }else{\r\n \$('#{$cityId}').html('{$cityDefault}');\r\n }\r\n \$('#{$districtId}').html('{$districtDefault}');\r\n "]); }
private function generateForm($fields, $data = null) { $result = ''; foreach ($fields as $field) { $value = !empty($data->{$field->name}) ? $data->{$field->name} : null; if ($field->type === 'string') { $result .= '<div class="form-group"><label>' . $field->title . '</label>' . Html::input('text', "Data[{$field->name}]", $value, ['class' => 'form-control']) . '</div>'; } elseif ($field->type === 'text') { $result .= '<div class="form-group"><label>' . $field->title . '</label>' . Html::textarea("Data[{$field->name}]", $value, ['class' => 'form-control']) . '</div>'; } elseif ($field->type === 'boolean') { $result .= '<div class="checkbox"><label>' . Html::checkbox("Data[{$field->name}]", $value, ['uncheck' => 0]) . ' ' . $field->title . '</label></div>'; } elseif ($field->type === 'select') { $options = ['' => Yii::t('easyii/customers', 'Select')]; foreach ($field->options as $option) { $options[$option] = $option; } $result .= '<div class="form-group"><label>' . $field->title . '</label><select name="Data[' . $field->name . ']" class="form-control">' . Html::renderSelectOptions($value, $options) . '</select></div>'; } elseif ($field->type === 'checkbox') { $options = ''; foreach ($field->options as $option) { $checked = $value && in_array($option, $value); $options .= '<br><label>' . Html::checkbox("Data[{$field->name}][]", $checked, ['value' => $option]) . ' ' . $option . '</label>'; } $result .= '<div class="checkbox well well-sm"><b>' . $field->title . '</b>' . $options . '</div>'; } } return $result; }
<?php foreach ($model->fields as $field) { ?> <tr> <td><?php echo Html::input('text', null, $field->name, ['class' => 'form-control field-name']); ?> </td> <td><?php echo Html::input('text', null, $field->title, ['class' => 'form-control field-title']); ?> </td> <td> <select class="form-control field-type"> <?php echo Html::renderSelectOptions($field->type, Category::$fieldTypes); ?> </select> </td> <td> <textarea class="form-control field-options" placeholder="<?php echo Yii::t('easyii/catalog', 'Type options with `comma` as delimiter'); ?> " <?php echo !$field->options ? 'style="display: none;"' : ''; ?> ><?php echo is_array($field->options) ? implode(',', $field->options) : ''; ?> </textarea> </td>
public function actionGolferSearch($id, $target, $term = '') { $model = $this->findCompetition($id); $golfers = Golfer::find()->all(); $availables = []; foreach ($golfers as $golfer) { $availables[$golfer->id] = $golfer->name; } $registrations = Registration::find()->where(['competition_id' => $id, 'status' => array(Registration::STATUS_PENDING, Registration::STATUS_REGISTERED)])->all(); $registereds = []; foreach ($registrations as $registration) { $registereds[$registration->golfer_id] = $availables[$registration->golfer_id]; unset($availables[$registration->golfer_id]); } $result = []; if (!empty($term)) { foreach (${$target} as $golfer) { if (strpos($golfer, $term) !== false) { $id = Golfer::findOne(['name' => $golfer]); $result[$id->id] = $golfer; } } } else { $result = ${$target}; } return Html::renderSelectOptions('', $result); }
/** * Action method for auto completion sections by the given parent category. * @param integer $id of the category to be used to auto complete sections. * @return string|Response action method execution result. */ public function actionSections($id) { $sections = Section::findAll(['category_id' => $id]); $items = array_merge(['(choose section)'], ArrayHelper::map($sections, 'id', 'title')); return Html::renderSelectOptions(null, $items); }
/** * @param $id * @param string $target * @param string $term * @return string */ public function actionRoleSearch($id, $target, $term = '') { $result = ['Roles' => [], 'Permission' => [], 'Routes' => []]; $authManager = Yii::$app->authManager; if ($target == 'available') { $children = array_keys($authManager->getChildren($id)); $children[] = $id; foreach ($authManager->getRoles() as $name => $role) { if (in_array($name, $children)) { continue; } if (empty($term) or strpos($name, $term) !== false) { $result['Roles'][$name] = $name; } } foreach ($authManager->getPermissions() as $name => $role) { if (in_array($name, $children)) { continue; } if (empty($term) or strpos($name, $term) !== false) { $result[$name[0] === '/' ? 'Routes' : 'Permission'][$name] = $name; } } } else { foreach ($authManager->getChildren($id) as $name => $child) { if (empty($term) or strpos($name, $term) !== false) { if ($child->type == Item::TYPE_ROLE) { $result['Roles'][$name] = $name; } else { $result[$name[0] === '/' ? 'Routes' : 'Permission'][$name] = $name; } } } } return Html::renderSelectOptions('', array_filter($result)); }
/** * 给用户分配角色 * @param $id * @return string */ public function actionAssignrole($id) { $auth = Yii::$app->authManager; $model = TAdmUser::findOne($id); if (Yii::$app->request->isPost) { $action = Yii::$app->request->get('action'); $roles = Yii::$app->request->post('roles'); if ($action == 'assign') { foreach ($roles as $rolename) { $role = $auth->getRole($rolename); $auth->assign($role, $id); } } else { foreach ($roles as $rolename) { $role = $auth->getRole($rolename); $auth->revoke($role, $id); } } //所有角色 $allroles = ArrayHelper::map($auth->getRoles(), 'name', 'name'); //所有已选择的角色 $selectedroles = ArrayHelper::map($auth->getRolesByUser($id), 'name', 'name'); $res = [Html::renderSelectOptions('', array_diff($allroles, $selectedroles)), Html::renderSelectOptions('', $selectedroles)]; Yii::$app->response->format = Response::FORMAT_JSON; return $res; } //获取已有角色 $assignedroles = ArrayHelper::map($auth->getRolesByUser($id), 'name', 'name'); //获取所有角色 $allroles = ArrayHelper::map($auth->getRoles(), 'name', 'name'); //未被选择的角色 $roles = array_diff($allroles, $assignedroles); return $this->render('assignrole', ['roles' => $roles, 'assignedroles' => $assignedroles, 'model' => $model, 'id' => $id]); }
/** * Return rendered options for attribute select * @param $entityType * @return string */ public function actionAttributes($entityType) { $attributes = Facet::getSearchableAttributes($entityType); $options = ['prompt' => '']; return Html::renderSelectOptions(null, $attributes, $options); }
/** * Get Values for Dependent DropDownList. * @author juan.gaviria@dsotogroup.com | Mariusz Soltys (https://github.com/marsoltys) */ public function actionGetDroDownDepValues() { $post = $_POST; /**@var \yii\db\ActiveRecord $model*/ $model = new $post['model'](); $query = $model::findAll([$post['varname'] => $post[$post['varname']]]); $data = ArrayHelper::map($query, 'id', $post['optionDestName']); echo Html::renderSelectOptions('', $data, $options = ['prompt' => 'Select...']); }
/** * Search Route * @param string $target * @param string $term * @param string $refresh * @return array */ public function actionRouteSearch($target, $term = '', $refresh = '0') { if ($refresh == '1') { $this->invalidate(); } $result = []; $manager = Yii::$app->getAuthManager(); $existsOptions = []; $exists = array_keys($manager->getPermissions()); $routes = $this->getAppRoutes(); if ($target == 'new') { foreach ($routes as $route) { if (in_array($route, $exists)) { continue; } if (empty($term) or strpos($route, $term) !== false) { $result[$route] = $route; } } } else { foreach ($exists as $name) { if ($name[0] !== '/') { continue; } if (empty($term) or strpos($name, $term) !== false) { $result[$name] = $name; } // extract route part from $name $r = explode('&', $name); if (empty($r[0]) || !in_array($r[0], $routes)) { $existsOptions[$name] = ['class' => 'lost']; } } } $options = $target == 'new' ? [] : ['options' => $existsOptions]; return Html::renderSelectOptions('', $result, $options); }
public function actionGetCiudadesRegion($region_id, $empty = true) { if ($empty) { $options = ['' => 'Escoge ciudad']; } $ciudades = Region::find()->where(['id' => $region_id])->one()->ciudads; $ciudades = ArrayHelper::map($ciudades, 'id', 'nombre'); asort($ciudades); $options += $ciudades; echo Html::renderSelectOptions('', $options); }
public function testRenderOptions() { $data = ['value1' => 'label1', 'group1' => ['value11' => 'label11', 'group11' => ['value111' => 'label111'], 'group12' => []], 'value2' => 'label2', 'group2' => []]; $expected = <<<EOD <option value="">please select<></option> <option value="value1" selected>label1</option> <optgroup label="group1"> <option value="value11">label11</option> <optgroup label="group11"> <option class="option" value="value111" selected>label111</option> </optgroup> <optgroup class="group" label="group12"> </optgroup> </optgroup> <option value="value2">label2</option> <optgroup label="group2"> </optgroup> EOD; $attributes = ['prompt' => 'please select<>', 'options' => ['value111' => ['class' => 'option']], 'groups' => ['group12' => ['class' => 'group']]]; $this->assertEqualsWithoutLE($expected, Html::renderSelectOptions(['value111', 'value1'], $data, $attributes)); }
/** * Search roles of user * @param integer $id * @param string $target * @param string $term * @return string */ public function actionRoleSearch($id, $target, $term = '') { $authManager = Yii::$app->authManager; $avaliable = []; $assigned = []; foreach ($authManager->getRolesByUser($id) as $role) { $type = $role->type; $assigned[$type == Item::TYPE_ROLE ? 'Roles' : 'Permissions'][$role->name] = $role->name; } foreach ($authManager->getRoles() as $role) { if (!isset($assigned['Roles'][$role->name])) { $avaliable['Roles'][$role->name] = $role->name; } } foreach ($authManager->getPermissions() as $role) { if ($role->name[0] !== '/' && !isset($assigned['Permissions'][$role->name])) { $avaliable['Permissions'][$role->name] = $role->name; } } $result = []; $var = ${$target}; if (!empty($term)) { foreach (['Roles', 'Permissions'] as $type) { if (isset($var[$type])) { foreach ($var[$type] as $role) { if (strpos($role, $term) !== false) { $result[$type][$role] = $role; } } } } } else { $result = $var; } return Html::renderSelectOptions('', $result); }
/** * Search roles of user * @param integer $id * @param string $target * @param string $term * @return string */ public function actionRoleSearch($id, $target, $term = '') { $authManager = Yii::$app->authManager; $avaliable = []; foreach ($authManager->getRoles() as $role) { $avaliable[$role->name] = $role->name; } $assigned = []; foreach ($authManager->getRolesByUser($id) as $role) { $assigned[$role->name] = $role->name; unset($avaliable[$role->name]); } $result = []; if (!empty($term)) { foreach (${$target} as $role) { if (strpos($role, $term) !== false) { $result[$role] = $role; } } } else { $result = ${$target}; } return Html::renderSelectOptions('', $result); }
public function actionGetRegionesPais($pais_id, $empty = true) { if ($empty) { $options = ['' => 'Escoge región']; } $regiones = Pais::find()->where(['id' => $pais_id])->one()->regions; $regiones = ArrayHelper::map($regiones, 'id', 'nombre'); asort($regiones); $options += $regiones; echo Html::renderSelectOptions('', $options); }