| |

PHP Advanced Datatable in Laravel >= 5.0


What is Ajax action?

by ajax action you can create, edit your data or remove it and any other action without load current page, let's say your datatable shows list of users with many filters and you want to create and edit user data without re-loading the page or re-filtering.

IDUsernameFirstnameLastnameEmailActions

Create Action

Create "action class" extend from: \Phpdt\Ldt\Actions


        class Edit extends \Phpdt\Ldt\Actions
        {

            //The path to blade template that will appears after clicking on the button (call action) - optional
            var $formview = 'your.blade.template';

            //How to dislay Edit button in datatable list
            static function row_button($id,$actionclass)
            {
                return  '<button>Edit</button>';
            }
        }

Properties and Methods

  • $formview: property to define template blade form (you must call this '@section('save_action_form_section') @show') where you want the submit and cancel buttons appears in your blade template form.
  • $initial_data: property in the action class, you need to initial this property by $this->initial_data=new \stdClass(); to use it as a variable in the blade from.
  • initialize_form(): method in the action class is used to initialize "initial_data" property and pass it to the blade template from.
  • $errors: matrix in the action class is used to validation data.
  • after_submit_form(): method in the action class is used to receive data after click on save button in the form and validation submitted data by push errors in the errors array ( $this->errors[]='* Username is required';) e.g.

Call Action

Call this action by "addaction" method from datatable object as below:


    $dt->addaction(action_key,'action_class');

    e.g:
     $dt->addaction(array('name'=>'Edit','class'=>'\Your\Action\Class\Edit'));



$formview tamplete example

   

   /* $formview tamplete example:
       Blade tamplete*/
    <div style="padding: 20px;">
        <div class="form-group">
            <label for="exampleInputEmail1">Username(<b style="color: red">*</b>)</label>
             //$initial_data passed by "initialize_form()" method
            <input value="<?php echo $initial_data->username?>" type="text" name="username" class="form-control"  >
            <small id="emailHelp" class="form-text text-muted">We'll never save. </small>
        </div>

        //Print Buttons (Save & cancel)
        @section('save_action_form_section')

        @show
    
    </div>

    


function initialize_form(){};


    // use this method to initial_data and pass to viewform
    function initialize_form(){


        $this->initial_data=new \stdClass();
        $this->initial_data->username='Any value';
    };

     //note: you can call $initial_data in blade template (viewfrom)

    


function after_submit_form(){};


     /* Use this method to process the data after submitting the form and you can use
     it to validation the submitted data by push errors in error array */

     function after_submit_form(){
        $this->initial_data=new \stdClass();
        $this->initial_data->username='Any value';
        if ($this->initial_data->username==''){
            $this->errors[]='* Username is required';
        }else{
                //Do any thing in your data
        }

    };

    

Note: if "$formview" property is empty the action will be "confirm action -Yes,No-"

Example:

in the action of your controller put the following code:



    $dt = new Your/Datatable/Class(
            array(
                'id' => 'ID',
                'firstname' => 'Firstname',
                'lastname' => 'Lastname'
                'username' => 'Lastname'
            ));
    $dt->addaction('confirm_action','\Demo\Confirmaction');
    $dt->addaction('edit_action','\Demo\Editaction');
    return $dt->display('Your/Blade/Template',$request);

/* Datatable class:*/

   class Test extends \Phpdt\Ldt\Datatable
    {

        const PRIMARY = 'id';
        static function from(){
            return 'mdl_user';
        }
    }


/* Action blade template:*/

   Html content
   Html content
   Html content

     //Place the datatable on the page
   	@include('phpdt::datatable_view')
	@section('datatable_section')
		@parent
	@show

   Html content
   Html content
   Html content
   Html content

/* Confirm Action:*/

    class Confirm extends \Phpdt\Ldt\Actions
    {

        static function row_button($id,$actionclass)
        {
            return  '<span class="btn btn-warning">Confirm Btn</span>';
        }
    }



/* Submit Form:*/


    class Edit extends \Phpdt\Ldt\Actions
    {

        var $formview = 'Website.Views.edit';

        //End point
        function after_submit_form(){

            $this->initial_data=new \stdClass();
            $this->initial_data->username=$this->request->username ;
            if ($this->request->username=='')
            {
                $this->errors[]='* Username is required';
            }

            if(empty($this->errors))
                $this->print_message('Saved','success');

        }

        function initialize_form(){

            $items=$this->selectedItems();
            $this->initial_data=new \stdClass();
            if(count($items)==1)
            {
                $this->initial_data->username=' set username';
            }else{
                $this->initial_data->username='';
            }
        }

        static function row_button()
        {
            return  '<span class="btn btn-info">Bulk Btn</span>';
        }
    }




Note:selectedItems contains all items selected

/* Blade template form (viewform):*/
        

        <div style="padding: 20px;">
            <div class="form-group">
                <label >Username(<b style="color: red">*</b>)</label>
                <input value="<?php echo $initial_data->username?>" type="text" name="username"
                 class="form-control"  >
            </div>

            @section('save_action_form_section')

            @show

        </div>
 


By: Mohammad Alaa Aldeen
Git: https://github.com/mhdalaaaldeen