skeeks / cms-dadata
SkeekS CMS 使用 dadata.ru
2.0.0
2023-06-05 10:29 UTC
Requires
README
信息
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist skeeks/cms-dadata-suggest "*"
或添加
"skeeks/cms-dadata-suggest": "*"
配置应用程序
'components' => [ 'dadataSuggest' => [ 'class' => 'skeeks\cms\dadataSuggest\CmsDadataSuggestComponent', ], 'dadataSuggestApi' => [ 'class' => 'skeeks\cms\dadataSuggest\CmsDadataSuggestApi', ], 'i18n' => [ 'translations' => [ 'skeeks/dadata-suggest' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@skeeks/cms/dadataSuggest/messages', 'fileMap' => [ 'skeeks/dadata-suggest' => 'main.php', ], ] ] ] ], 'modules' => [ 'dadataSuggest' => [ 'class' => 'skeeks\cms\dadataSuggest\CmsDadataSuggestModule', ] ]
示例
首先通过 api 检测地址并保存到会话中
\Yii::$app->dadataSuggest->address
首先通过 api 检测地址并保存到会话中
if (\Yii::$app->dadataSuggest->isSavedAddress) { echo \Yii::$app->dadataSuggest->address->unrestrictedValue; } else { echo "Not saved address to session"; }
地址小部件
<?= \skeeks\cms\dadataSuggest\widgets\address\DadataGetAddressWidget::widget([ 'options' => [ 'href' => '#', 'onclick' => 'new sx.classes.ModalRegionPageReload(); return false;', 'class' => 'sx-dadata-suggestion-city', ] ]); ?>
建议小部件
<?= \skeeks\cms\dadataSuggest\widgets\suggest\DadataSuggestInputWidget::widget([ 'name' => 'address', 'id' => 'sx-global-region-input', 'addon' => 'clear', 'value' => \Yii::$app->dadataSuggest->isSavedAddress ? \Yii::$app->dadataSuggest->address->unrestrictedValue : "", 'clientOptions' => [ 'onInit' => new \yii\web\JsExpression(<<<JS function(e, data) { data.DadataSuggest.bind('onSelect', function() { $("#sx-save-region").show(); }); } JS ) ], 'options' => [ 'class' => 'form-control', 'placeholder' => 'Найти город', ] ]); ?>
在表单中使用建议小部件
<?= $form->field($model, 'post_recipient')->widget( \skeeks\cms\dadataSuggest\widgets\suggest\DadataSuggestInputWidget::className(), [ 'options' => [ 'placeholder' => $model->getAttributeLabel('post_recipient'), ], 'type' => 'NAME' ] ); ?> ### Suggest widgets with additional settings ```php $form->field($model, 'post_address')->widget( \skeeks\cms\dadataSuggest\widgets\suggest\DadataSuggestInputWidget::className(), [ 'options' => [ 'placeholder' => "Адрес (улица, дом, кв)", ], 'clientOptions' => [ 'suggestOptions' => [ 'triggerSelectOnSpace' => true, 'triggerSelectOnBlur' => true, 'constraints' => [ [ 'locations' => \Yii::$app->dadataSuggest->address->getRegionArray(), 'deletable' => false, 'label' => '' ] ], 'restrict_value' => true, ], 'onInit' => new \yii\web\JsExpression(<<<JS function(e, data) { data.DadataSuggest.bind('onSelect', function() { data.DadataSuggest.bind('afterSave', function() { $.pjax.reload({container:'#sx-cart-full'}); }); data.DadataSuggest.save(); return false; }); } JS ) ] ]); ?>
##链接
SkeekS CMS (Yii2) — 快速、简单、高效!
skeeks.com | cms.skeeks.com