NOTICE ¶
You may want to check first the latest behavior driven version of this extension,ajax-crud-behavior.This is an outdated extension.
Overview ¶
I always thought that the navigation in the view files that the default gii CRUD generator creates is too complex. This CRUD gii template generates a single Administration page with a CGridView.Update/Create forms and model details view are rendered in a Fancybox Window and deletions go through a confirmation dialog (jUI).The form works with client validation(given that your model validation rules are included in the rules supported by the current Yii client validation).You can switch to Ajax Validation simply by setting the relevant option in CActiveForm instantiation in _ajax_form file,and uncommenting the performAjaxValidation call in ReturnForm action of the controller.
Requirements ¶
Developed and tested with Yii 1.8,may work with other versions too.
Installation ¶
- Hide index.php from your requests,if you have'nt done so yet.You can find detailed instructions on how to do this here.(Paragraph 6) Also,in urlManager configuration in config/main.php file set
'urlFormat'=>'path',
'showScriptName'=>false,
- Unzip the downloaded file.
- Copy the gii folder to your application's protected folder.
- Copy the js_plugins folder to the root folder of your application,(same level as protected).
- In config/main.php file,in gii configuration, add the path of your gii folder like so:
'modules'=>array(
......
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'1',
// If removed, Gii defaults to localhost only. Edit carefully to taste.
'ipFilters'=>array('127.0.0.1','::1'),
'generatorPaths' => array(
'application.gii' //Ajax Crud template path
),
),
.......
)
Usage ¶
- Create the table for your model in the database.Use id as your primary key column name.
- Click Model Generator.Type the table name in the Table Name field.
- Preview if you want,and then Generate.
Your Model class has been generated.
- Click Crud Generator
- In the Model Class field type the name of the class that was generated in the previous step.
- Click Code template and select ajax_crud,if it's not already selected.
- Preview if you want,and then Generate.
This is it.Navigate to the Administration page:go to [application root]/[controller ID]/.You should see the administration page and you are ready to perform CRUD operations on the model.
RESOURCES ¶
- jQuery Form Plugin
- Fancybox
- Fancybox2 //Read the licence for fancybox 2.
- jQuery UI
Spiros "drumaddict" Kabasakalis,November 5th 2011.
CHANGE LOG ¶
- 1.3.December 11th 2011.
Form submission is now handled by jQuery Form Plugin (see resources). This has the advantage that you can include a file field in your form,and it will be submitted without a page refresh,although this is not technically ajax,(as the XHR object cannot handle this task). For more information see online jQuery Form Plugin documentation.
'afterAjaxUpdate' callback of CGridView now does not duplicate the code declared in the script tag. It just calls a js function declared in the script tag so unnecesssary code repetition has been eliminated.
Fancybox 2 is now used for the form rendering.Please see the licence for fancybox2 online.(resources).
1.2.Deprecated
1.1.November 9th 2011.
Made some changes so that the application root folder is not hardcoded in URLs,in generated views and controller. Now generated code is valid in local and production enviroment as well,without the need to manually change the URLs. Thanks to Asgaroth for pointing this out to me. Cheers!
Thanks!
Veeeeeeryyy nice!! thanks a lot!!!
Thanks
Looks great!
Thanks
I tested everything in drumaddict's Lab. Excellent extensions (Fobera!)
..
Very nice idea, and I appreciate you shared this with us.
however there are a couple of basic errors you could fix to improve this extension. firts of all you "hardcode" (when generating) the app base URL, making the generated code unportable across different servers. :
//use : Yii::app()->baseUrl.'/css' //instead of '/myapp/css'
Secondly you ajax_admin views have A LOT of javascript, and its also duplicated, in the afterAjaxUpdate CGridView attribute and then again after that.
Try creating global javascript objects, or append a javascript object to the jQuery object so that you can do:
//this: 'afterAjaxUpdate' => 'js:AjaxCrud.gridAfterAjaxUpdate' //Instead of 'afterAjaxUpdate' => 'js:A HELL BUNCH OF JAVASCRIPT CODE'
Also, when generating the controller, either change the name:
public function actionAjax_Admin() //to public function actionIndex() //Or override the class attribute $defaultAction public $defaultAction = "ajax_admin"; // so that the generated code works easily without manual modification.
Nice idea though!
and thanks...
@Asgaroth.
Hi Asgaroth,
Declaring a javascript object would make sense only if I used the same js code many times.
Thanks for appreciating my extension.
@drumaddict
thanks for your response:
I was talking about the ajax_admin.php view generated files.
in the CGridView columns configuration for CButtonColumns generates code that looks like
'imageUrl' => 'myapp/css/image.. //instead of 'imageUrl' => Yii::app()->baseUrl.'/css/image..
Same for the javascript
$.ajax({ type: "POST", url: "/myapp/bebe/returnView", //instead of $.ajax({ type: "POST", url: '.Yii::app()->baseUrl.'"/bebe/returnView",
Any way, you should not have that much javascript in a view. and im prretty sure the javascript is almost the same, even if not, you can always create a plugin and funcions that receive the urls, because the only possible change from a model to another is the URL call. which can, in the end, be just the parameter of a javascript function in a external javascript file. reusing code, and archieving organization.
Its just my opinion, I just like things as organised as possible :)
@Asgaroth Apologies!
If someone does the code generation on the server there's no problem,but -yeah most of us develop locally,so the generator will hard code the local application root url.Will try to fix it as soon as possible,when I find free time.Thanks for your comments!
Changes proposed
Hi
I agree with Asgaroth, I was actually just changing your extension to made all these changes proposed by him.
As soon as I have something done, I'll let you know and post it in the extensions forum topic.
Anyway, this is a great extension.
Thanks for sharing your work.
PS. You should provide a link for the forum topic here, in the extension page.
"fancybox" can't be displayed
i always success generate code, but when my table name using underscore "_" like "test_one", the modal window "fancybox" can't be displayed, that because generate code produce code "test-one-....". so i replace all "test-one-..." with the "testOne-..." by modif template code
$this->class2id($this->modelClass) // just with $this->modelClass
primary keys
Hi, thanks for this extension.
Couple problems I noticed within a few minutes of install:
In the generator, you have 'id' hard coded as the primary key for the model. You should change this to getPrimaryKey().
This happens in 'index.php' and '_ajax_form.php'
You'll still have a problem with compound PK models though. I have some models that use these, I will look into a possible fix. But the above is a quick fix that you could easily do.
The indentation is very different from the normal Yii files. In a plain text editor, it makes your code very hard to read.
it would be nice...
Ajax looks really fast, much thanks !
But When we have to delete 100 records it takes very long time.
You should add a column with checkboxes for batch deletion.
@saegeek
You are right,I thought of that.Do you know how to do that?'Cause I don't have time to work this out.Will appreciate some contribution....UPDATE.
I recently figured out how to add batch(mass) delete functionality in CGridView with a checkbox column.I am using it right now in my CMS,when I find time,I might update this extension to take advantage of mass deletes.
Taking the form out of the fancybox in the main body
I like this extension even though it could do better with a little bit more of documentation. For instance I would like to get rid of the fancybox and instead having the form (for editing/view/etc.) appearing embedded in the grid page at the bottom or top of the grid .. where should I go to modify the plugin to do this?
I guess in /views/mytablewhatever/index.php where it says $('.fan_update') in the related section UPDATE or VIEW or CREATE on the success action... but the semantic there look bit odd to me (not much familiar with Jquery though)
any help appreciated
Incorrect links at module
When using at a module for example admin module, links would be incorrect and view, update, delete wouldn't work.
the problem is that generating URL doesn't consider the module name:
url: "<?php echo Yii::app()->request->baseUrl;?>/CONTROLLER/returnForm",
@morteza toloo
I have not considered modules in this extension,you are on your own with this.I suggest you take a look at ajax-crud-behavior ,a behavior driven version of this extension.I cannot guarantee that it will work in modules though,I did not consider modules because I never use them.
@drumaddict
@drumaddict - custom Validation rules not working
Custom Validation rules and validation like 'unique'are not working . What to do?
Delete is not working
I really love this extension.
But delete is not working it says "Error 404:The system is unable to find the requested action "view"."
Also I need Batch delete .
Could you please help me out ?
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.