Download the PHP package letsjump/yii2-easy-ajax without Composer
On this page you can find all versions of the php package letsjump/yii2-easy-ajax. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download letsjump/yii2-easy-ajax
More information about letsjump/yii2-easy-ajax
Files in letsjump/yii2-easy-ajax
Package yii2-easy-ajax
Short Description EasyAjax are a bunch of Yii methods that minimize the amount of code you need to write to interact with Ajax CRUD, notifies, modals, tabs, pjax-reloads, form validations among others...
License MIT
Informations about the package yii2-easy-ajax
yii2-easy-ajax
Relax your keyboard with Yii2 EasyAjax
EasyAjax are a bunch of Yii methods that minimize the amount of code you need to write to interact with Bootstap UI and with Javascript in general.
form validation among others can now be set up and launched with only a line of code into the controller's action response.
For example, EasyAjax::modal('My modal content')
opens a modal with "My modal content" as content, while EasyAjax::reloadPjax(['#p0'])
reloads the pjax-container
identified by id="p0"
EasyAjax further provides:
- Complete and ready-to-use ajax CRUD, thanks to the integrated Gii generator
- Fully configurable options both globally and action-specific
- Customizable HTML templates
- Extensible with your own methods
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the require
section of your composer.json.
Configuration
To use this extension, add the following code to your web application configuration (config/web.php):
To use the integrated Gii generator, add the following code to your application configuration:
Please note: The integrated Gii generator allows you to generate the code for AJAX as well as the standard CRUD.
Usage
- 1. Requests
- 2. Responses
1. Requests
EasyAjax requests are simple jQuery AJAX requests that need to refer to a controller action appropriately configured:
The easiest way to perform an EasyAjax request to a controller action is adding the data-yea=1
attribute to the Html tag in charge of the onclick javascript event:
or
NOTE:
To explore any other Html attributes available and to know all the details, please refer to the guide
2. Responses
The controller actions interacting with EasyAjax should return a Json array, and they only need to contain one or more EasyAjax methods in their response array.
In the following example...
... EasyAjax will:
- Closes the bootstrap modal opened in the UI
- Reload the pjax-container
#p0
- Shows a Bootstrap Notify informing the user of the successful operation
Here are the available methods in detail:
Modals
With EasyAjax, you can fire and configure a Bootstrap Modal using the following: EasyAjax::modal(content, title, _formid, size, footer, options)
It is also available a method to remotely close a Modal which is actually open in the UI:
You can edit the modal layout and many other settings. Please refer to the guide for all the available options.
Notify
EasyAjax Notify allows to control the Bootstrap Notify plugin. The plugin assets (bootstrap-notify.js
and animate.js
) are bundled within EasyAjax.
Tip: You can disable the asset inclusion if it is already bundled in your application
To fire a Notify, you can use \letsjump\easyAjax\EasyAjax::notify(message, title, settings)
.
In the settings
parameter, you should specify the type of notification displayed (Notify::TYPE_INFO
(blue), Notify::TYPE_SUCCESS
(green), Notify::TYPE_WARNING
(yellow) or Notify::TYPE_DANGER
(red)).
To speed up your coding, I have also added some shortcut methods for the most common notification types: EasyAjax::notifyInfo(message)
, EasyAjax::notifySuccess(message)
, EasyAjax::notifyWarning(message)
, EasyAjax::notifyDanger(message)
.
Example:
Note: Actually notify doesn't refer to the Yii2 flash message session array. Please open an issue if you think it is needed.
Refer to the guide for all the available options and template manipulation.
Pjax Reload
The EasyAjax PjaxReload (EasyAjax::pjaxReload(['#myPjaxID0', '#myPjaxID1', ...])
) method allows to reload one or more Pjax containers.
Refer to the guide for detailed informations.
Form Validation
The form validation (EasyAjax::formValidation(['#my-form-id'=>ActiveForm::validate(MyModelClass)])
) method allows to display the validation results for the specified form. It will send the validation results for each form field specified by the #form-id
attribute to display its error messages.
Refer to the guide for detailed informations.
Ajax CRUD with Gii
The bundled Gii generator within EasyAjax allows you to instantly create the Ajax CRUD controller and views.
This will generate a CRUD schema which is identical to the Yii2 original one, with an added actionModal($id=null)
into the controller code, plus its relative myViewFolder/_modal.php
view in charge of creating or updating the data in an Ajax modal.
You can switch the CRUD behavior to Ajax modal by simply setting the 'modal' parameter of the integrated ActionColumn method :
BONUS: Deleting a record doesn't imply a complete page refresh, therefore the GridView pagination will not be affected.
Refer to the guide for all the available options.
Ajax Tabs
EasyAjax Tab (EasyAjax::tab('tab-id', 'content to inject')
) is a simple and very basic way to update the content of a Bootstrap Tab with an ajax response from a controller.
View
In the \yii\bootstrap\Tabs widget for each tab item, you must:
- Include a
linkOptions
parameter with a reference to the controller action Url and adata-yea=1
parameter which will fire the request. - Set the tab container ID (
'options' => ['id' => 'yourTabID']
) for a stable reference to its content.
Controller
Just include the EasyAjax method EasyAjax::tab(tab-id, content)
into the controller action response array:
Warning: tab-id must be specified without the hashtag (#).
Refer to the guide for all the available options.
Content replace
EasyAjax ContentReplace(['#container1-id'=>'New content', '#container2-id'=>'New content', ...])
replaces the content of a specific Html tag with the code sent by the EasyAjax response. It uses the jQuery.html()
standard method.
Refer to the guide for detailed informations.
Confirms
You can use the EasyAjax confirm('message', 'url')
method to fire a Javascript Window confirm() from a controller action. By clicking on the "OK" button it will fire an Ajax request to the action specified in the url
parameter.
Refer to the guide for detailed informations.
Security
See SECURITY.md
Contributing
See CONTRIBUTING.md
Credits
Yiisoft for the best PHP framework for large-scale application (my own opinion), Twitter Bootstrap, and the Bootstrap Notify plugin. The Code of Conduct is adapted from the Contributor Covenant
License
See LICENSE.md
All versions of yii2-easy-ajax with dependencies
bower-asset/remarkable-bootstrap-notify Version ~3.1.0
bower-asset/animate-css Version ~3.7.0