使用 Select2 在视图 Yii2 中调用函数
Posted
技术标签:
【中文标题】使用 Select2 在视图 Yii2 中调用函数【英文标题】:Call function in view Yii2 with Select2 【发布时间】:2018-11-27 00:07:21 【问题描述】:我使用高级框架 Yii2。我在前端有 RegionController,在前端有 Region 模型。鉴于我想用 Select2 调用一个公共函数 Countrylist 来显示所有国家。但是当我尝试调用这个函数时,异常是“找不到这个页面”...... 这是控制器:
<?php
namespace frontend\controllers;
use Yii;
use frontend\models\Region;
use frontend\models\Country;
use frontend\models\RegionSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
/**
* RegionController implements the CRUD actions for Region model.
*/
class RegionController extends Controller
public function actionRegionlist($q = null, $id = null)
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$out = ['results' => ['id' => '', 'text' => '']];
if (!is_null($q))
$query = new Query;
$query->select('id, name AS text')
->from('region')
->where(['like', 'name', $q])
->andWhere(['country_id' => $_GET['country']])
->limit(20);
$command = $query->createCommand();
$data = $command->queryAll();
$out['results'] = array_values($data);
elseif ($id > 0)
$out['results'][] = ['id' => $id, 'text' => Region::find($id)->name];
return $out;
public function actionCountrylist($q = null, $id = null)
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$out = ['results' => ['id' => '', 'text' => '']];
if (!is_null($q))
$data = array();
$arr = ArrayHelper::map(Country::find()->select('country.id, country.name')->filterWhere(['like', 'country.name', $q])->limit(10)->all(), 'id', 'name');
ksort($arr);
if ($arr)
$k = 0;
foreach ($arr as $id => $value)
$data[$k]['id'] = $id;
$data[$k]['text'] = $value;
$k++;
$out['results'] = array_values($data);
return $out;
这是我调用函数的视图代码:
<div class="col-md-4">
<div class="form-group">
<?php
$cityName = empty($model->city_id) ? '' : City::findOne($model->city_id)->name;
$url = \yii\helpers\Url::to(['..\..\region\countrylist']);
?>
<?=
$form->field($model, 'city_id')->widget(Select2::classname(), [
'initValueText' => $cityName,
'theme' => 'bootstrap',
'options' => [
'placeholder' => Yii::t('app', 'app.choose'),
'class' => 'form-control select2'
],
'pluginOptions' => [
'allowClear' => true,
'minimumInputLength' => 3,
'ajax' => [
'url' => $url,
'dataType' => 'json',
'data' => new JsExpression('function(params)
return q:params.term, region:$("#profile-region_id").val(); ')
],
'escapeMarkup' => new JsExpression('function (markup) return markup; '),
'templateResult' => new JsExpression('function(city) return city.text; '),
'templateSelection' => new JsExpression('function (city) return city.text; '),
],
])
?>
</div>
</div>
【问题讨论】:
生成的$url
是错误的,请仔细检查您的网址以获取正确的控制器操作。添加 $url
生成的内容。
网址是“xxxxxx.com/bg/region/countrylist?q=asd”我试过没有bg,但没有任何改变..
您的视图文件在哪里?它是区域控制器的一部分吗?试试$url = \yii\helpers\Url::to(['region\countrylist']);
或$url = \yii\helpers\Url::to([\region\countrylist']);
是的,谢谢你的工作
如果对您有用,请将答案标记为正确
【参考方案1】:
尝试根据范围改变 URL
$url = \yii\helpers\Url::to(['region\countrylist']);
或
$url = \yii\helpers\Url::to(['\region\countrylist']);
【讨论】:
以上是关于使用 Select2 在视图 Yii2 中调用函数的主要内容,如果未能解决你的问题,请参考以下文章
Yii2 - 使用 Ajax 加载为 Select2 插件设置值
yii2 - 如何在 javascript 中设置 kartik select2 值