есть урл:
Код: Выделить всё
https://api.vk.com/method/database.getCountries?need_all=1&count=1000
Не могу сообразить как их загнать в dropdown
Может спать пора, может мозг менять, так или иначе, буду благодарен за ссылочку или код.
Код: Выделить всё
https://api.vk.com/method/database.getCountries?need_all=1&count=1000
Код: Выделить всё
$('#idCountry').change(function() {....}
Если стран 1000 - без автокомплита не обойтисьAlex@ писал(а):Блин я без виджета делал:
у меня сначала в форме изначально загружается в activedropDownList список стран,
список регионов и список городов пока пустой, после того как юзер выбирает из списка стран
на jQuery прописано событие $('#idCountry').change(function() {....} , которое посылает get запрос и возвращает результат в json формате и очищает список регионов (если он был не пуст) и заполняю теми регионами которые принадлежат выбранной стране, так же и с регионами.
Код: Выделить всё
echo Select2::widget(
[
'name' => 'country',
'value' => $model->country,
'options' => ['multiple' => true],
'pluginOptions' =>[
'tags'=>['111','222','333']
]
]);
Код: Выделить всё
<script type="text/javascript">var select2_e3a3cca2 = {"tags":["111","222","333"],"width":"resolve"};
</script>
Код: Выделить всё
var TvojSelect = TvojSelect;
$.each(select2_e3a3cca2, function(i){
TvojSelect.append('<option value="' + i + '">' + this + '</option>');
});
Код: Выделить всё
return Html::activeDropDownList($model, 'group_id',
ArrayHelper::map(Groups::find()->all(), 'id', 'title'),
[
'prompt' => 'Нет',
]
);
Код: Выделить всё
[
'attribute' => 'group_id',
'value' => function ($model, $key, $index, $column) {
// var_dump($model); var_dump($key); exit;
return Html::activeDropDownList($model, 'group_id',
ArrayHelper::map(Groups::find()->all(), 'id', 'title'),
[
'prompt' => 'Нет',
'data-id' => $model->id,
'id' => "word-group_id-$model->id",
'onchange' => "
$.ajax({
url: \"/words/default/ajax\",
type: \"post\",
data: { word_id: $key, group_id : $(\"#word-group_id-$model->id\").val()},
success: function(response) {
тут заменяешь селект ответом от контроллера
$('#word-group_id-$model->id').replaceWith(response);
}
});"
]
);
},
'format' => 'raw',
'filter' => ArrayHelper::map(Groups::find()->all(), 'id', 'title')
],
Код: Выделить всё
$lang = 0;
$headerOptions = array(
'http' => array(
'method' => "GET",
'header' => "Accept-language: en\r\n" .
"Cookie: remixlang=$lang\r\n"
)
);
$methodUrl = 'http://api.vk.com/method/database.getCountries?v=5.5&need_all=1&count=1000';
$streamContext = stream_context_create($headerOptions);
$json = file_get_contents($methodUrl, false, $streamContext);
$arr = json_decode($json, true);
$arrforstr = [];
foreach($arr['response']['items'] as $one)
{
$arrforstr[$one['id']] = $one['title'];
}
return $this->render('add', [
'arrforstr' => $arrforstr
]);
Код: Выделить всё
use kartik\select2\Select2;
echo Select2::widget(
[
'id' => 'country',
'name' => 'country',
'data' => $arrforstr,
'options' => ['multiple' => false, 'placeholder' => 'Введите страну ...'],
'pluginOptions' => [
'allowClear' => true,
]
]);
Код: Выделить всё
$id = 1; //берем из выбранной страны, но не пойму как
$q = "моск"; //берем из второго виджета, но не пойму как
$url = 'http://api.vk.com/method/database.getCities?v=5.5&country_id='.$id.'&need_all=1&count=1000&q='.$q;
Код: Выделить всё
/*******
* View
******/
// The controller action that will render the list
$url = \yii\helpers\Url::to(['city-list']);
// Script to initialize the selection based on the value of the select2 element
$initScript = <<< SCRIPT
function (element, callback) {
var id=\$(element).val();
if (id !== "") {
\$.ajax("{$url}?id=" + id, {
dataType: "json"
}).done(function(data) { callback(data.results);});
}
}
SCRIPT;
// The widget
echo $form->field($model, 'city')->widget(Select2::classname(), [
'options' => ['placeholder' => 'Search for a city ...'],
'pluginOptions' => [
'allowClear' => true,
'minimumInputLength' => 3,
'ajax' => [
'url' => $url,
'dataType' => 'json',
'data' => new JsExpression('function(term,page) { return {search:term}; }'),
'results' => new JsExpression('function(data,page) { return {results:data.results}; }'),
],
'initSelection' => new JsExpression($initScript)
],
]);
/*************
* Controller
************/
public function actionCitylist($search = null, $id = null) {
$out = ['more' => false];
if (!is_null($search)) {
$query = new Query;
$query->select('id, name AS text')
->from('city')
->where('name LIKE "%' . $search .'%"')
->limit(20);
$command = $query->createCommand();
$data = $command->queryAll();
$out['results'] = array_values($data);
}
elseif ($id > 0) {
$out['results'] = ['id' => $id, 'text' => City::find($id)->name];
}
else {
$out['results'] = ['id' => 0, 'text' => 'No matching records found'];
}
echo Json::encode($out);
}
Код: Выделить всё
$query->select('id, name AS text')
->from('city')
->where('name LIKE "%' . $search .'%"') //!!!! НИКОГДА ТАК НЕ ДЕЛАЙТЕ!!! Это уязвимость вашего сайта
->limit(20);
Код: Выделить всё
$query->select('id, name AS text')
->from('city')
->where(['like','name',$search]) //!!!!
->limit(20);
Код: Выделить всё
<?php
use \yii\helpers\Json;
echo $form->field($model, 'streetid')->widget(\kartik\widgets\Select2::classname(), [
'language'=>'ru',
'options' => ['placeholder' => 'Улица ...'],
'pluginOptions' => [
'minimumInputLength' => 3,
'allowClear' => false,
'ajax' => [
'url' => \yii\helpers\Url::toRoute(['/reference/street/list']),
'dataType' => 'json',
'data' => new \yii\web\JsExpression('function(term,page) { return {q:term}; }'),
'results' => new \yii\web\JsExpression('function(data,page) { return {results:data.results}; }'),
],
/**
Вот это необходимо что при обновлении формы норм инициализировался выбранный элемент - то что в доках - initScript
По сути мы должны вернуть json - объект со стартовыми id и текст
**/
'initSelection' => new \yii\web\JsExpression('function(element,callback){
return callback('.
($model->isNewRecord
? '{"id":null,"text":""}'
:Json::encode(["id"=>$model->streetid,"text"=>$model->name."[".$model->city->name."]"])
).')
}')
],
]);?>
Код: Выделить всё
public function actionList($q){
if(Yii::$app->request->isAjax){
$model=new StreetSearch();
Yii::$app->response->format=Response::FORMAT_JSON;
return $model->ajsearch($q);
}
return false;
}
Код: Выделить всё
public function ajsearch($q){
$query = self::find()->active(true,'{{%street}}')->joinWith('city');
$query->select(['{{%street}}.street_id','{{%street}}.name','{{%street}}.city_id','{{%city}}.name as gorod'])->active();
if($q!='*'){
$query->andFilterWhere(['like', '{{%street}}.name', $q]);
}
$query->orderBy('{{%street}}.name');
$res=$query->all();
$result=[];
if(!empty($res)){
foreach($res as $row){
/**@var Street $row **/
$result[]=['id'=>$row->street_id,'text'=>$row->name.($row->city_id?'['.$row->city->name.']':'')];
}
}
$out=['more'=>false,'results'=>$result];
return $out;
}
+1denisOgr писал(а):А вы не хотите раз спарсить себе города и выбирать у себя с БД? Или там хорошая динамика изменений в этих данных?
Зачем дергать удаленный сервер каждый раз при выводе?
ПС Паша ушел. Контакт часто тормозит. Вам нужны иногда эти тормоза? А если API поменяют (я так понимаю это неофициальное API (могу ошибаться)) - вам нужно переделывать каждый dropdown?
База страны и города = 85 мб. И перебирать ее у себя не быстрее чем юзать api. Тем более уже просто злость берет. Надо сделать и освободить мозг.denisOgr писал(а):А вы не хотите раз спарсить себе города и выбирать у себя с БД?
Код: Выделить всё
<?php
//виджет Select2 с странами
echo Select2::widget(
[
'id' => 'country',
'name' => 'country',
'data' => $arrforstr,
'options' => ['multiple' => false, 'placeholder' => 'Введите страну ...'],
'pluginOptions' => [
'allowClear' => true,
]
]);
//вставляем jquery будь он не ладен
$this->registerJs("
jQuery('#country').change(function(data){ //реагируем на событие change
var data_id = data.added['id']; //берем id страны, которая выбрана
jQuery.ajax({
url: '".Url::to(['ajaxsity'])."', //урл запроса
data: 'id=' + data_id + '&q=', //данные для запроса
success: function(data){
jQuery('#forsity').append(data); //из контролера вернулся renderpartial запихалим его в заранее подготовленный див с id forsity
}});
jQuery('#sity').select2();
});
");
?>
Ну как минимум страны выдрать - это один раз одним запросом их ксатит всего 234wokster писал(а):База страны и города = 85 мб. И перебирать ее у себя не быстрее чем юзать api. Тем более уже просто злость берет. Надо сделать и освободить мозг.denisOgr писал(а):А вы не хотите раз спарсить себе города и выбирать у себя с БД?
Код: Выделить всё
public function actionYourform()
{
if(!Yii::$app->request->isPost){
$countries=$this->getVkCountries();
}
$model new Model();
if(Yii::$app->request->isPost){
/// bla-bla
}
return $this->render('yourview',['countries'=>$countries, $model=>$model]);
}
public function actionVkcity()
{
$lang = 0;
$headerOptions = array(
'http' => array(
'method' => "GET",
'header' => "Accept-language: en\r\n" .
"Cookie: remixlang=$lang\r\n"
)
);
$id = Yii::$app->request->get('countryid', null);
$q = Yii::$app->request->get('q', null);
if ($q && $id) {
$methodUrl
= 'http://api.vk.com/method/database.getCities?v=5.5&country_id=' . $id . '&need_all=1&count=1000&q='
. $q;
$streamContext = stream_context_create($headerOptions);
$json = file_get_contents($methodUrl, false, $streamContext);
$arr = json_decode($json, true);
$result = [];
foreach ($arr['response']['items'] as $one) {
$result[] = ['id'=>$one['id'],'text'=>$one['title']];
}
Yii::$app->response->format = 'json';
return $out=['more'=>false,'results'=>$result];
}else{
//корявый запрос
}
}
function getVkCountries(){
//можно закешить, читать из файла результат и т.п.
$lang = 0;
$headerOptions = array(
'http' => array(
'method' => "GET",
'header' => "Accept-language: en\r\n" .
"Cookie: remixlang=$lang\r\n"
)
);
$methodUrl = 'http://api.vk.com/method/database.getCountries?v=5.5&need_all=1&count=1000';
$streamContext = stream_context_create($headerOptions);
$json = file_get_contents($methodUrl, false, $streamContext);
$arr = json_decode($json, true);
$arrforstr = [];
foreach ($arr['response']['items'] as $one) {
$arrforstr[$one['id']] = $one['title'];
}
return arrforstr;
}
Код: Выделить всё
<?php
//виджет Select2 с странами
echo Select2::widget(
[
'id' => 'country',
'name' => 'country',
'data' => $countries,
'options' => ['multiple' => false, 'placeholder' => 'Введите страну ...'],
'pluginOptions' => [
'allowClear' => true,
],
'pluginEvents'=>[
'change'=>'function(e){
$("#city").val(null).trigger("change"); //Сбрасываем выбор города при смене страны
}',
],
]);
//виджет Select2 с городами
echo Select2::widget(
[
'id' => 'city',
'name' => 'city',
'options' => ['multiple' => false, 'placeholder' => 'Введите город ...'],
'pluginOptions' => [
'allowClear' => true,
'ajax' => [
'url' => \yii\helpers\Url::toRoute(['/controller/vkcity']),
'dataType' => 'json',
'data' => new \yii\web\JsExpression('function(term,page) { var countryid=$("#country").val(); return {q:term,countryid:countryid}; }'),
'results' => new \yii\web\JsExpression(
'function(data,page) { return {results:data.results}; }'
),
],
]
]);