Yii 1.1: ajax-crud-behavior

Single Page CRUD operations on ActiveRecord models with controller behavior inherited AJAX actions
19 followers

AjaxCrudBehavior

Single Page CRUD operations on ActiveRecord models with controller behavior inherited AJAX actions

© 2013 Spiros Kabasakalis

The MIT License (MIT)

AjaxCrudBehavior

LIVE DEMO

GITHUB REPOSITORY

Overview

Create, update and view details of records in a fancybox pop up. Single and bulk deletions with modal dialog prompt (noty plugin). This is actually a complete rewrite of my ajaxcrudgiitemplate extension. Instead of gii generated files,I wrote a reusable behavior that adds controller actions and eliminates repetition of code across different controllers.Javascript has been moved to a js file resulting in cleaner code.

Requirements

Yii 1.1.12 or above,may work with older versions too.

Setup.

  • 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,
  • Copy js_plugins folder in webroot folder.I don't like publishing assets.I register css and jss straight from a webroot subfolder.
  • Copy css folder with icons to webroot.
  • Copy behaviors folder containing AjaxCrudBehavior.php class file to protected folder.
  • Copy BootPager.php and BaseController.php to components folder.
  • Prepare the database table for your ActiveRecord model,say Product.(example product.sql provided).
  • Create the model ActiveRecord class file that corresponds to product table,(example Product.php). The only requirement is that it includes a search function that returns a CActiveDataProvider instance to populate CGridView:

    public function search($pagination)
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $criteria=new CDbCriteria;
     
        //example,fill in with your model property names.
        $criteria->compare('id',$this->id,true);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('description',$this->description,true);
        $criteria->compare('price',$this->price,true);
     
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
             'pagination' => array(
             'pageSize' =>$pagination,
                       ),
        ));
    }
  • Write a controller for the model.The only requirement for the controller is that includes the code below and extends from BaseController.

class [ MODEL CLASS NAME ] Controller extends BaseController
{
public function init(){
    $this->ajaxCrudBehavior->register_Js_Css();
    parent::init();
}
 
    public function behaviors()
       {
           return array(
                   'ajaxCrudBehavior' => array('class' => 'application.behaviors.AjaxCrudBehavior',
                   'modelClassName' =>'[MODEL CLASS NAME(ex.Product)]',
                   'form_alias_path' =>'[FORM PATH ALIAS (ex.application.views.product._form)]',
                   'view_alias_path' =>'[VIEW PATH ALIAS (ex.application.views.product._view)]' ,
                   'pagination'=>'10'      //page size for CGridView pagination
               )
           );
       }
...
Your controller code
...
}

Templates for the form and view files are provided,you will only need to modify property names and input fields for your specific model and copy to view folder of your controller.

  • Last,you need to configure property columns for the CGridView in admingrid.php and copy it to controller's view folder. Again,a template is provided-it's straightforward.
  • Make sure you include jquery before any other scripts.Either uncomment the relevant line in register_Js_Css() function of AjaxCrudBehavior or register it somewhwere else in your code.
  • Example files use bootstrap styled markup.Uncomment the relevant line that registers bootstrap.css in register_Js_Css() function of AjaxCrudBehavior if it's not already registered somewhere else in your application.
  • Navigate to /[controllerID]/admingrid to render the administration page.
  • Example files for model,controller,form and view files are provided.

Resources

Total 7 comments

#16384 report it
Chris Backhouse at 2014/02/17 08:03am
Great extension but ...

Nice extension with code cleverly centralized into one behavior.

BUT it only works on a basic one Model form. You cannot have sub-forms for other models or tabbed interface with sub-models.

I've forked the project at: https://github.com/chrisb34/yii-ajax-crud-behavior to enable multiple forms from different models/controllers

#13230 report it
drumaddict at 2013/05/16 11:59am
@Subin Manuvel

I can't help you if you don't provide further debugging info.Watch for errors in your browser's javascript console.Make sure you followed all steps carefully.If you wish to contact again,please not here,go to my Github and get my email address.Thanks!

#13227 report it
Subin Manuvel at 2013/05/16 07:00am
Form,View,Update and Delete are not working

It shows this error,how can i solve this error product/returnDetailsView

#12335 report it
drumaddict at 2013/03/14 06:56am
@marios

Glad you worked it out.In my setup instructions above I mention "you will only need to modify property names and input fields for your specific model ".It's obvious that when using your own models you must modify the form templates.Cheers!

#12333 report it
marios at 2013/03/14 06:32am
Problem Solved

The debbuging info showed that it was an internal server error which after a closer look and searchnig it caused by the line 22 in _form.php file. That line echoed:

echo $model->name;

but since i didn't have any fitting attribute in my model it trigerred an error. I didn't even notice that line of code at the beginning as i was focused on rendering the model attributes in the form fields. I suggest that line to be modified as

echo $model->id;

or to be commented.

**Thanks again Spyros for this awesome and really helpful extension!! Cheers up!! **

#12320 report it
drumaddict at 2013/03/13 02:30pm
@marios

The fact that the spinner shows ,tells me that javascript is applied on the buttons. I can only guess that some server error happens,but since I have zero debugging info,I cannot help you.Check your javascript console (firebug for firefox etc) and look for javascript errors and server response.Send it over,in my gmail,and maybe I can help you.

#12315 report it
marios at 2013/03/13 12:57pm
Congrats for the extension upgrade!! It seems awesome but i can' t get it work yet!!

Although we are both native speakers of Greek language, i'll exibit my problem here so as to help any other members of the community facing the same problem. I've installed your extension following carefully your instructions. I've embedded the given example (Product) in my application and works perfectly. But i have problems while trying to administrate my own models. In fact, i'm getting the results (rows) on the grid and i'm in position to delete any row of the model (using both unique and massive deletion). BUT, when pushing the add button, or the view and update icon neither fancy box is rendered nor any action is performed. The only thing i'm getting is the loading image for a while. I' ve done all the proper modifications needed in the model, controller, and the view files. I' ve triple checked the modifications so as to eliminate the possibility of having any mistake in that files. Any ideas of what is going on??

Leave a comment

Please to leave your comment.

Create extension