Yii2 Easy Ajax
Ajax gadgets to speed-up coding
Ajax gadgets to speed-up coding
Remember to add a property data-yea="1"
in a link pointing to your yii2
controller
action (example)
public function actionNotify()
{
Yii::$app->response->format = Response::FORMAT_JSON;
return [
\letsjump\easyAjax\EasyAjax::notifySuccess('This is a Success Notify!')
];
}
public function actionNotify()
{
Yii::$app->response->format = Response::FORMAT_JSON;
return [
\letsjump\easyAjax\EasyAjax::notifyInfo('This is an Info Notify!')
];
}
public function actionNotify()
{
Yii::$app->response->format = Response::FORMAT_JSON;
return [
\letsjump\easyAjax\EasyAjax::notifyWarning('This is a Warning Notify!')
];
}
A danger notify with a personalized timer setting
public function actionNotify()
{
Yii::$app->response->format = Response::FORMAT_JSON;
return [
\letsjump\easyAjax\EasyAjax::notifyDanger('This is a Danger Notify!', null, ['timer' => 15000])
];
}
Remember to add a property data-yea="1"
in a link pointing to your yii2
controller
action (example)
public function actionConfirm()
{
Yii::$app->response->format = Response::FORMAT_JSON;
return [
EasyAjax::confirm('This will fire a growl. Ok?', Url::to(['site/notify']))
];
}
This will redirect to an ajax action. For example, this will fire another growl using site/notify action
public function actionAjaxRedirect()
{
Yii::$app->response->format = Response::FORMAT_JSON;
return [
EasyAjax::redirectAjax(Url::to(['site/notify']))
];
}
This will redirect to a landing page by window.location.href
public function actionJavascriptRedirect()
{
Yii::$app->response->format = Response::FORMAT_JSON;
return [
EasyAjax::redirectJavascript(Url::to(['site/landing']))
];
}
Last click time is never clicked
public function actionContentReplace()
{
Yii::$app->response->format = Response::FORMAT_JSON;
return [
EasyAjax::contentReplace(['#time' => date('d/m/Y H:i:s')])
];
}
public function actionValidateForm()
{
Yii::$app->response->format = Response::FORMAT_JSON;
$contactForm = new ContactForm();
return [
EasyAjax::formValidation(['#contact-form' => ActiveForm::validate($contactForm)])
];
}
Last click time is 21/11/2024 07:31:15
public function actionPjaxReload()
{
Yii::$app->response->format = Response::FORMAT_JSON;
return [
EasyAjax::reloadPjax(['#test0'])
];
}
Remember to add a property data-yea="1"
in a link pointing to your yii2
controller
action (example)
public function actionBasicModal()
{
Yii::$app->response->format = Response::FORMAT_JSON;
return [
EasyAjax::modalBasic('This is the modal content', 'Modal title'),
];
}
public function actionBasicModalNoFooter()
{
Yii::$app->response->format = Response::FORMAT_JSON;
return [
EasyAjax::modalBasic('This is the modal content', 'Modal title', Modal::SIZE_SMALL, false),
];
}
Click on each tab to get the current local date
View
<?= \yii\bootstrap\Tabs::widget([
'items' => [
[
'label' => 'Rome',
'linkOptions' => [
'data-href' => Url::to(['site/tab', 'id' => 'rome',]),
'data-yea' => 1
],
'options' => ['id' => 'rome'],
],
// ... other tabs
]
]) ?>
Controller
public function actionTab($id)
{
$date = new \DateTimeImmutable('now');
$content = [
'rome' => $date->setTimezone(new \DateTimeZone('Europe/Rome'))->format('l jS \of F h:i:s A'),
'london' => $date->setTimezone(new \DateTimeZone('Europe/London'))->format('l jS \of F h:i:s A'),
'new-york' => $date->setTimezone(new \DateTimeZone('America/New_York'))->format('l jS \of F h:i:s A'),
'calcutta' => $date->setTimezone(new \DateTimeZone('Asia/Calcutta'))->format('l jS \of F h:i:s A'),
];
if ( ! array_key_exists($id, $content)) {
throw new BadRequestHttpException('Your request is invalid');
}
Yii::$app->response->format = Response::FORMAT_JSON;
return [
EasyAjax::tab($id, '<p>' . $content[$id] . '</p>')
];
}