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.
ID | Username | Firstname | Lastname | Actions |
---|
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
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: 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)
/* 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-"
$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
<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