Skip to content

This api help to datatables server-side integrations by SSP code otimized

Notifications You must be signed in to change notification settings

minasvisual/datatables-js-server-api

Repository files navigation

datatables-js-server-api (Use release 1.1)

This api help to datatables server-side integrations by SSP code otimized

#How to use:

Config:

required 'path/to/Datatables.php';

//set config connections and requests $config['config'] = array( 'user' => 'user', 'pass' => 'pass', 'db' => 'db name', 'host' => 'host' );

$dt = new Datatables( $config );

Adding columns

//addCols($field, $alias=null, $params=array(), $dt=null ) 
$dt->addCols( 'post_id', 'p' );
// or
$dt->addCols( 
				array( 
					array('title', 'p' ),
					array('category', 'c', ['as'=>'cat'] ),
					array('created_at', 'p', 
					      array( 'formatter' => function( $d, $row ) {
            				return array( 'display'=>date( 'd/m/Y', strtotime($d) ), 'timestamp'=>$d ); // output formated
        				})
					)
				) // end params
			);

Adding tables ( required least 1 )

// addTables(  $table, $key='id', $alias  )
$dt->addTables( 'posts', 'post_id', 'p');
//or
$dt->addTables( array(
				array( 'categories', 'category_id', 'c' ),
				...
			));

Adding joins (new method linked)

// addJoin( $join_table, $join_pk, $join_alias, $fk, $join_type='', $target_alias=null )
$dt->addTables( 'posts', 'id', 'p')->addJoin('users', 'id', 'u', 'user_id', 'LEFT')->addJoin('users','id','a','autor')

Add additional where

// addWhere($field, $value='', $alias=null, $op='=', $concat='and' )
$dt->addWhere( 'created_at', $_POST['initdate'], 'p', '>=' );
or 
$dt->addWhere( 
        array(
          array( 'created_at', $_POST['initdate'], 'p', '>='),
          ....
        )
      );

Show return:

echo json_encode( $dt->render() );

Javascript render helper

//renderJS( $selector, $params, $lang=null, $urlParams=null, $inst='table' )
$dt->renderJS(
			"#table",            // html selector table   
			'"processing": true, // server side
			"serverSide": true,  // server side
			"ajax": {            // data source
				"url": "path/to/data",
				"type": "GET"    // Use POST if you prefer
			},
			"columnDefs": [      // columns modifications
	            {  targets: 2, render: { _: "display",  sort: "timestamp"  }  	}, //get date formated
	    ],',
			null,                // Language json file (Default portuguese-br) | see doc https://datatables.net/plug-ins/i18n/
			array( 
				"param" => '$("#input").val()',   // additional url parameters to filter data source
	    ),
		);
Outputs basic javascript call

For javascript configuration and server side working see https://datatables.net/;

About

This api help to datatables server-side integrations by SSP code otimized

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages