Skip to content

Commit

Permalink
Merge pull request #37 from drsdre/dev-custom
Browse files Browse the repository at this point in the history
Flexible forceReload and renderAjax
  • Loading branch information
johnitvn committed Nov 15, 2015
2 parents 4aa41ed + 6a26eb1 commit db971b6
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 34 deletions.
20 changes: 12 additions & 8 deletions src/CrudAsset.php
Expand Up @@ -19,20 +19,24 @@ class CrudAsset extends AssetBundle
public $css = [
'ajaxcrud.css'
];
public $js = [
//'ModalRemote.js',
//'ajaxcrud.js',
'ModalRemote.min.js',
'ajaxcrud.min.js',

];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
'yii\bootstrap\BootstrapPluginAsset',
'kartik\grid\GridViewAsset',
];



public function init() {
// In dev mode use non-minified javascripts
$this->js = YII_DEBUG ? [
'ModalRemote.js',
'ajaxcrud.js',
]:[
'ModalRemote.min.js',
'ajaxcrud.min.js',
];

parent::init();
}
}
20 changes: 12 additions & 8 deletions src/assets/ModalRemote.js
Expand Up @@ -15,15 +15,15 @@
function ModalRemote(modalId){


this.defauls = {
this.defaults = {
okLabel:"OK",
cancelLabel:"Cancel",
loadingTitle:"Loading",
loadingTitle:"Loading"
};

this.modal = $(modalId);

this.dialog - $(modalId).find('.modal-dialog');
this.dialog = $(modalId).find('.modal-dialog');

this.header = $(modalId).find('.modal-header');

Expand Down Expand Up @@ -140,7 +140,7 @@ function ModalRemote(modalId){
*/
this.displayLoading = function(){
this.setContent(this.loadingContent);
this.setTitle(this.defauls.loadingTitle);
this.setTitle(this.defaults.loadingTitle);
}

/**
Expand All @@ -159,8 +159,8 @@ function ModalRemote(modalId){
if(message!==undefined){
this.setContent(message);
}
this.addButton(okLabel===undefined?this.defauls.okLabel:okLabel,'btn btn-primary',okCallback);
this.addButton(cancelLabel===undefined?this.defauls.cancelLabel:cancelLabel,'btn btn-default pull-left',cancelCallback);
this.addButton(okLabel===undefined?this.defaults.okLabel:okLabel,'btn btn-primary',okCallback);
this.addButton(cancelLabel===undefined?this.defaults.cancelLabel:cancelLabel,'btn btn-default pull-left',cancelCallback);
}

/**
Expand Down Expand Up @@ -276,8 +276,12 @@ function ModalRemote(modalId){
function successRemoteResponse(response){

// reload datatable if response contain forceReload field
if(response.forceReload !== undefined && response.forceReload){
$.pjax.reload({container:'#crud-datatable-pjax'});
if(response.forceReload !== undefined && response.forceReload) {
if (response.forceReload == 'true') {
$.pjax.reload({container: '#crud-datatable-pjax'});
} else {
$.pjax.reload({container: response.forceReload});
}
}

// close modal if response contain forceClose field
Expand Down
2 changes: 1 addition & 1 deletion src/assets/ModalRemote.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions src/assets/ajaxcrud.js
Expand Up @@ -8,13 +8,17 @@ $(document).ready(function(){

// Create instance of Modal Remote
// This instance will be controller all business logic of modal
modal = new ModalRemote('#ajaxCrubModal');
// Backwards compatible lookup of ajaxCrubModal
if ($('#ajaxCrubModal').length>0 && $('#ajaxCrudModal').length == 0) {
modal = new ModalRemote('#ajaxCrubModal');
} else {
modal = new ModalRemote('#ajaxCrudModal');
}


// Catch click event of all button want to open modal
$(document).on('click','[role="modal-remote"]',function(event){
event.preventDefault();
modal.remote(this,null);
modal.remote(this,null);
});

// Catch click event of all button want to open modal
Expand All @@ -30,8 +34,8 @@ $(document).ready(function(){

if(selectedIds.length==0){
modal.show();
modal.setTitle('Have no selection');
modal.setContent('You must select item for do this action');
modal.setTitle('No selection');
modal.setContent('You must select item(s) to use this action');
modal.addButton("Close",'btn btn-default',function(button,event){
this.hide();
});
Expand Down
3 changes: 2 additions & 1 deletion src/assets/ajaxcrud.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions src/generators/default/controller.php
Expand Up @@ -101,7 +101,7 @@ public function actionView(<?= $actionParams ?>)
Yii::$app->response->format = Response::FORMAT_JSON;
return [
'title'=> "<?= $modelClass ?> #".<?= $actionParams ?>,
'content'=>$this->renderPartial('view', [
'content'=>$this->renderAjax('view', [
'model' => $this->findModel(<?= $actionParams ?>),
]),
'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
Expand Down Expand Up @@ -133,7 +133,7 @@ public function actionCreate()
if($request->isGet){
return [
'title'=> "Create new <?= $modelClass ?>",
'content'=>$this->renderPartial('create', [
'content'=>$this->renderAjax('create', [
'model' => $model,
]),
'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
Expand All @@ -142,7 +142,7 @@ public function actionCreate()
];
}else if($model->load($request->post()) && $model->save()){
return [
'forceReload'=>'true',
'forceReload'=>'#crud-datatable-pjax',
'title'=> "Create new <?= $modelClass ?>",
'content'=>'<span class="text-success">Create <?= $modelClass ?> success</span>',
'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
Expand All @@ -152,7 +152,7 @@ public function actionCreate()
}else{
return [
'title'=> "Create new <?= $modelClass ?>",
'content'=>$this->renderPartial('create', [
'content'=>$this->renderAjax('create', [
'model' => $model,
]),
'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
Expand Down Expand Up @@ -195,17 +195,17 @@ public function actionUpdate(<?= $actionParams ?>)
if($request->isGet){
return [
'title'=> "Update <?= $modelClass ?> #".<?= $actionParams ?>,
'content'=>$this->renderPartial('update', [
'content'=>$this->renderAjax('update', [
'model' => $model,
]),
'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
Html::button('Save',['class'=>'btn btn-primary','type'=>"submit"])
];
}else if($model->load($request->post()) && $model->save()){
return [
'forceReload'=>'true',
'forceReload'=>'#crud-datatable-pjax',
'title'=> "<?= $modelClass ?> #".<?= $actionParams ?>,
'content'=>$this->renderPartial('view', [
'content'=>$this->renderAjax('view', [
'model' => $model,
]),
'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
Expand All @@ -214,7 +214,7 @@ public function actionUpdate(<?= $actionParams ?>)
}else{
return [
'title'=> "Update <?= $modelClass ?> #".<?= $actionParams ?>,
'content'=>$this->renderPartial('update', [
'content'=>$this->renderAjax('update', [
'model' => $model,
]),
'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
Expand Down Expand Up @@ -252,7 +252,7 @@ public function actionDelete(<?= $actionParams ?>)
* Process for ajax request
*/
Yii::$app->response->format = Response::FORMAT_JSON;
return ['forceClose'=>true,'forceReload'=>true];
return ['forceClose'=>true,'forceReload'=>'#crud-datatable-pjax'];
}else{
/*
* Process for non-ajax request
Expand Down Expand Up @@ -284,7 +284,7 @@ public function actionBulkDelete()
* Process for ajax request
*/
Yii::$app->response->format = Response::FORMAT_JSON;
return ['forceClose'=>true,'forceReload'=>true];
return ['forceClose'=>true,'forceReload'=>'#crud-datatable-pjax'];
}else{
/*
* Process for non-ajax request
Expand Down
2 changes: 1 addition & 1 deletion src/generators/default/views/index.php
Expand Up @@ -74,7 +74,7 @@
</div>
</div>
<?='<?php Modal::begin([
"id"=>"ajaxCrubModal",
"id"=>"ajaxCrudModal",
"footer"=>"",// always need it for jquery plugin
])?>'."\n"?>
<?='<?php Modal::end(); ?>'?>
Expand Down

0 comments on commit db971b6

Please sign in to comment.