Skip to content

Commit 208f164

Browse files
committed
first commit
0 parents  commit 208f164

File tree

4 files changed

+227
-0
lines changed

4 files changed

+227
-0
lines changed

.gitattributes

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
# Custom for Visual Studio
5+
*.cs diff=csharp
6+
*.sln merge=union
7+
*.csproj merge=union
8+
*.vbproj merge=union
9+
*.fsproj merge=union
10+
*.dbproj merge=union
11+
12+
# Standard to msysgit
13+
*.doc diff=astextplain
14+
*.DOC diff=astextplain
15+
*.docx diff=astextplain
16+
*.DOCX diff=astextplain
17+
*.dot diff=astextplain
18+
*.DOT diff=astextplain
19+
*.pdf diff=astextplain
20+
*.PDF diff=astextplain
21+
*.rtf diff=astextplain
22+
*.RTF diff=astextplain

AjaxSubmitButton.php

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
namespace demogorgorn\ajax;
3+
4+
use yii\base\Widget;
5+
use yii\helpers\Html;
6+
use yii\helpers\Json;
7+
8+
/**
9+
* AjaxSubmitButton renders an ajax button which is very similar to ajaxSubmitButton from Yii1.
10+
*
11+
* Example:
12+
*
13+
* ```php
14+
* <?= Html::beginForm(); ?>
15+
* <?= Select2::widget([
16+
* 'name' => 'country_code',
17+
* 'data' => Country::getAllCountries(),
18+
* 'options' => [
19+
* 'id' => 'country_select',
20+
* 'multiple' => false,
21+
* 'placeholder' => 'Select country...',
22+
* 'class' => 'uk-width-medium-7-10'
23+
* ]
24+
* ]);?>
25+
*
26+
* <?php AjaxSubmitButton::begin([
27+
* 'label'=>'Проверить',
28+
* 'ajaxOptions'=>
29+
* [
30+
* 'type'=>'POST',
31+
* 'url'=>'country/getinfo',
32+
* 'cache' => false,
33+
* 'success' => new \yii\web\JsExpression('function(html){
34+
* $("#output").html(html);
35+
* }'),
36+
* ],
37+
* 'options' => ['type' => 'submit'],
38+
* ]);
39+
* AjaxSubmitButton::end();?>
40+
*
41+
* <?= Html::endForm(); ?>
42+
* ```
43+
*
44+
* @author Oleg Martemjanov <[email protected]>
45+
*/
46+
47+
class AjaxSubmitButton extends Widget
48+
{
49+
public $ajaxOptions = [];
50+
51+
/**
52+
* @var array the HTML attributes for the widget container tag.
53+
*/
54+
public $options = [];
55+
56+
/**
57+
* @var string the tag to use to render the button
58+
*/
59+
public $tagName = 'button';
60+
/**
61+
* @var string the button label
62+
*/
63+
public $label = 'Button';
64+
/**
65+
* @var boolean whether the label should be HTML-encoded.
66+
*/
67+
public $encodeLabel = true;
68+
69+
/**
70+
* Initializes the widget.
71+
*/
72+
public function init()
73+
{
74+
parent::init();
75+
if (!isset($this->options['id'])) {
76+
$this->options['id'] = $this->getId();
77+
}
78+
}
79+
80+
public function run()
81+
{
82+
parent::run();
83+
84+
echo Html::tag($this->tagName, $this->encodeLabel ? Html::encode($this->label) : $this->label, $this->options);
85+
86+
if (!empty($this->ajaxOptions))
87+
$this->registerAjaxScript();
88+
}
89+
90+
protected function registerAjaxScript()
91+
{
92+
$view = $this->getView();
93+
94+
if(!isset($this->ajaxOptions['data']) && isset($this->ajaxOptions['type']))
95+
$this->ajaxOptions['data'] = new \yii\web\JsExpression('$(this).parents("form").serialize()');
96+
97+
$this->ajaxOptions= Json::encode($this->ajaxOptions);
98+
$view->registerJs("$( '#".$this->options['id']."' ).click(function() {
99+
$.ajax(". $this->ajaxOptions .");
100+
return false;
101+
});");
102+
}
103+
104+
}

README.md

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
AjaxSubmitButton for Yii 2
2+
=====================================
3+
4+
This is the AjaxSubmitButton widget that renders an ajax button which is very similar to ajaxSubmitButton from Yii1 for Yii 2.
5+
6+
Example of usage of the widget with form and other custom widget (in this case Select2).
7+
8+
The view:
9+
```php
10+
use demogorgorn\ajax\AjaxSubmitButton;
11+
12+
<?php echo Html::beginForm('', 'post', ['class'=>'uk-width-medium-1-1 uk-form uk-form-horizontal']); ?>
13+
14+
<?= Select2::widget([
15+
'name' => 'country_code',
16+
'data' => Country::getAllCountries(),
17+
'options' => [
18+
'id' => 'country_select',
19+
'multiple' => false,
20+
'placeholder' => 'Choose...',
21+
'class' => 'uk-width-medium-7-10']
22+
]);
23+
?>
24+
25+
<?php AjaxSubmitButton::begin([
26+
'label' => 'Check',
27+
'ajaxOptions' => [
28+
'type'=>'POST',
29+
'url'=>'country/getinfo',
30+
/*'cache' => false,*/
31+
'success' => new \yii\web\JsExpression('function(html){
32+
$("#output").html(html);
33+
}'),
34+
],
35+
'options' => ['class' => 'customclass', 'type' => 'submit'],
36+
]);
37+
AjaxSubmitButton::end();
38+
?>
39+
40+
<?php echo Html::endForm(); ?>
41+
42+
<div id="output"></div>
43+
```
44+
45+
Please note: that #output is a div element which will be updated.
46+
47+
In controller:
48+
```php
49+
public function actionGetinfo()
50+
{
51+
if(!isset($_POST['country_code']) || empty($_POST['country_code']))
52+
return;
53+
54+
$code = $_POST['country_code'];
55+
56+
return $this->renderAjax('resultwidget', ['code' => $code]);
57+
}
58+
```
59+
60+
Installation
61+
------------
62+
63+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
64+
65+
Either run
66+
67+
```
68+
php composer.phar require demogorgorn/yii2-ajaxSubmitButton "*"
69+
```
70+
71+
or add
72+
73+
```
74+
"demogorgorn/yii2-ajaxSubmitButton": "*"
75+
```
76+
77+
to the require section of your `composer.json` file and run `composer update`.
78+

composer.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "demogorgorn/yii2-ajax-submit-button",
3+
"description": "AjaxSubmitButton renders an ajax button which is very similar to ajaxSubmitButton from Yii1.",
4+
"keywords": ["ajax", "submit", "button", "extension", "widget"],
5+
"homepage": "https://github.com/demogorgorn/yii2-ajax-submit-button",
6+
"type": "yii2-extension",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Oleg Martemjanov",
11+
"email": "[email protected]",
12+
"homepage" : "http://foreign.by"
13+
}
14+
],
15+
"require": {
16+
"yiisoft/yii2": "*"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"demogorgorn\\ajax\\": ""
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)