dataTable, add row button when using serverside processing - javascript

I use server side processing files from the download as starting point with the ssp.class.php file. All works fine, but not the button. In the last column, I want to add some buttons. A lot to be found about this, but not working for me because I defined column data in php and not js. I can't put a function inside the column array like in the js examples.
I have the following code running:
<script type="text/javascript">
$(document).ready(function() {
pageSetUp();
var table = $('#dt_basic').dataTable({
"processing": true,
"serverSide": true,
"ajax": "index.php?loc=client/clients"
});
});
</script>
PHP
<?php
public function clients() {
$columns = array(
array( 'db' => 'id', 'dt' => 0 ),
array( 'db' => 'name', 'dt' => 1 ),
array( 'db' => 'address', 'dt' => 2 ),
array( 'db' => 'telephone', 'dt' => 3 ),
);
$table = 'clients';
$primaryKey = 'id';
$sql_details = array(
'user' => #####,
'pass' => #####,
'db' => #####,
'host' => #####
);
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
}
?>

Related

How to use $_GET inside server side processing script of Datatables?

I'm using this example's code https://datatables.net/examples/server_side/simple.html
I want to use $_GET variable in the server_processing.php file so that I can specify the value and doing this from the url. For example table.php?table-id=table1
index.html
Table 1
Table 2
Table 3
table.php
<?php
// Get
$var = $_GET['table-id'];
?>
<table id="example" style="width:100%">
<thead>
<tr>
<th>Payslip ID</th>
<th>Employee ID</th>
<th>Employee Name</th>
</tr>
</thead>
</table>
<script>
$(document).ready(function() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "scripts/server_processing.php"
} );
} );
</script>
scripts/server_processing.php
<?php
// DB table to use
$table = '$var';
// Table's primary key
$primaryKey = 'id';
// indexes
$columns = array(
array( 'db' => 'first_name', 'dt' => 0 ),
array( 'db' => 'last_name', 'dt' => 1 ),
array( 'db' => 'position', 'dt' => 2 )
);
// SQL server connection information
$sql_details = array(
'user' => 'root',
'pass' => '',
'db' => 'sampledb',
'host' => 'localhost'
);
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
variable table-id is undefined. Is there a way to do this? The link part is important so I cannot remove that from the code.
You need to pass the value of $_GET['table-id'] through to the AJAX call by using the ajax.data feature.
The ajax.data option provides the ability to add additional data to the request, or to modify the data object being submitted if required.
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": {
"url": "scripts/server_processing.php",
"data": {
"table_id": <?= $_GET['table_id'] ?>
}
}
} );
Then $_GET['table_id'] should be available within scripts/server_processing.php.

pagination in server side datatable not working (without changing the URL)

i have a datatable for which my jquery is as follows,
$('#example').DataTable({
"lengthMenu": [[10,15,20,25,-1], [10,15,20,25,'All']],
"processing": true,
"serverSide": true,
ajax: {
url: 'url2',
dataSrc: 'data'
},
columns: [
{ data: 'column1' },
{ data: 'column2' },
{ data: 'column3' },
{ data: 'column4' },
{ data: 'column5' },
{ data: 'column6' }
]
});
this is working properly. The only issue is although its showing pagination links, they are not clickable and all the rows are displayed in first page itself.
for eg. if there are 100 rows, the links are getting generated 1-10 but all the 100 records are showing on the first page itself.
I've referred ,
https://datatables.net/examples/data_sources/server_side
what am i doing wrong here?
Thank you for your suggestions
Server side code -
$total_records = $this->model2->getTotal();
$query['results'] = $this->model1->get_Data();
$data = array('data' => $query['results'],
"draw" => (isset($_REQUEST["draw"]) ? $_REQUEST["draw"] : 0),
"recordsTotal" => $total_records,
"recordsFiltered" => $total_records
);
echo json_encode($data);
I think i know what am i doing wrong,
when i print $_GET in my php code it comes out to be empty. But it is supposed to have the limit and offset value.
How to i send limit offset in $_GET?
You need to know how many records go on a page, and how many records there are.
Your SQL will end in OFFSET 30 LIMIT 10 (for example).
First get total Records. Page will come through your URL and default to page 1 if not set.
The offset is calculated like this (example, page 3):
$totalPages = ceil($totalRecords / $numPerPage); // round up the way.
$offset = ($page * $numPerPage) - $numPerPage; // (3 * 10 = 30) - 10 = offset 20
Your SQL will therefore grab records with OFFSET 20 LIMIT 10
This is what your server.php should looks like. Think you are missing some lines of code:
<?php
$table = 'employees';
$primaryKey = 'id'; // Table's primary key
$columns = array(
array( 'db' => 'id', 'dt' => 0 ),
array( 'db' => 'first_name', 'dt' => 1 ),
array( 'db' => 'last_name', 'dt' => 2 ),
array( 'db' => 'position', 'dt' => 3 ),
array( 'db' => 'date', 'dt' => 4 ),
array( 'db' => 'updated', 'dt' => 5 ),
);
$sql_details = array(
'user' => 'username',
'pass' => 'password',
'db' => 'database',
'host' => 'localhost'
);
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_POST, $sql_details, $table, $primaryKey, $columns )
);
?>
End result: https://databasetable-net.000webhostapp.com/
This should get you in the right direction. Think the ssp.class.php file might need added too, unsure if you have it (you can find it on github). Hope this helps!

add image loader WooCommerce Products Custom Fields

I have pasted this code in my main function.php and it works great
but I want to add an image to my woocommerce product and I do not know how I can achieve that. Does anyone have an idea for doing it?
function woo_add_custom_general_fields() {
global $woocommerce, $post;
woocommerce_wp_text_input(
array(
'id' => 'telnr',
'label' => __( 'Nr. Tel)', 'woocommerce' ),
'placeholder' => 'Nr',
'desc_tip' => 'true',
'description' => __( 'tel nr.', 'woocommerce' )
)
);
}
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_telnr = $_POST['telnr'];
if( !empty( $woocommerce_telnr ) )
update_post_meta( $post_id, 'telnr', esc_html( $woocommerce_telnr ) );
}
This is what I tried and which fails:
function woo_options_add($options) {
// This is a option heading
$woo_metaboxes = array(
"image" => array (
"name" => "image",
"label" => "Post Image",
"type" => "upload",
"desc" => "Upload file hereā€¦"
)),
// Return new options
return $options;
}

Dropdown javascript onload

I have a dropdown menu, that has a onchange function. Once the function is executed it changes another dropdown menu.
I need to make it so it executes the script onload.
1st dropdown:
echo $form->field($model, 'company_id')->dropDownList($items_company, ['prompt' => 'Select Company', 'style' => 'width:400px;', 'onchange' => '
$.post("index.php?r=project/lists&id=' . '"+$(this).val(), function( data ) {
$( "select#project-client" ).html( data );
console.log("On change");
console.log(data);
});
',])->label('Company');
2nd dropdown:
echo '<label class="control-label">Company Client</label>';
echo Select2::widget([
'model' => $model,
'attribute' => 'client',
'theme' => Select2::THEME_BOOTSTRAP,
'options' => [ 'label' => 'Client',
'multiple' => true, 'style' => 'width:400px;', 'overwriteInitial' => true],
'pluginOptions' => [
'disabled' => false,
],
]);
This is what I tried:
$(document).ready(function () {
var currentProjectCompany = $('#project-company_id').val();
$.post("index.php?r=project/lists&id=' . '" + currentProjectCompany, function (data) {
$("select#project-client").html(data);
console.log("Company ID:");
console.log(currentProjectCompany);
console.log("Clients");
console.log(data);
});
});
Move the onchange code into its own function (it should be there anyway), and execute that function in the ready() function.
That way it will fire both onchange and onload.
I do the same check my code it may help you .But i use ajax and jquery.
For firs dropdown .
echo $form->dropDownListGroup(
$model 'id', array(
'wrapperHtmlOptions' => array(),
'widgetOptions' => array(
'data' => abc::getName($model->id),
'htmlOptions' => array(
'prompt' => 'Select Project',
'ajax' => array(
'type' => 'POST',
'url' => ( Yii::app()->createUrl('/' . $domain_name . '/qq/xyz/abc') ),
'update' => '#seconddropdownid',
//'dataType' => 'json',
'data'=>array('id'=>'js:this.value'),
)
),
),
)
);
in second dropdown :
echo $form->dropDownListGroup(
$project, 'tag', array(
'wrapperHtmlOptions' => array(),
'widgetOptions' => array(
'data' =>$this->getProjectTags(),
'htmlOptions' => array(
'prompt' => 'Select Tags',
),
)
)
);
on change of the second list you can update the the list-view of yii .

datatables format column from multiple sources?(Server side processing)

Im trying to get server side processing working with concatenated columns.
I came across this this post: Datatables - Server-side processing - DB column merging
But when I use that format I get SQL errors. But I also want to insert say... a space between the fields... is this possible?
Edit:
Example:
Table init:
var customer_Table = $('#customer_Table').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "php/server_processing.php",
stateSave: true,
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
'order': 2, 'asc' ]],
"columns": [{"title":"id","visible":false,"type":"html-string"},{"title":"name","visible":true,"type":"html-string"},[{"title":"address","visible":true,"type":"html-string"}
} );
Column scheme:
$columns = array(
array( 'db' => 'id', 'dt' => "id" ),
array( 'db' => 'name', 'dt' => "Name" ),
array( 'db' => "`street` . ' ' . `city` . '<br>' . `postal` . ' ' . `country`" 'dt' => "address"
)
);
Error:
{"error":"An SQL error occurred: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`id`,`name`, `JCP`, ``street` . ' ' . `city` . '<br>' . `postal` . ' ' . `country``...' at line 1"}
Judging from the error message, the processing engine is expecting a valid column reference for 'db'. Each 'db' should refer to a valid column reference in the WHERE clause, and each 'dt' contains the label to be displayed for that column.
Your reference is, according to your snippet:
"`street` . ' ' . `city` . '<br>' . `postal` . ' ' . `country`"
Which doesn't mean much to most database engines I am familiar with. Try something like this:
$columns = array(
array( 'db' => 'id', 'dt' => "id" ),
array( 'db' => 'name', 'dt' => "Name" ),
array( 'db' => "CONCAT(`street`, ' ', `city`, '<br>', `postal`, ' ', `country`)",
'dt' => "address")
);
$columns = array(
array( 'db' => 'id', 'dt' => "id" ),
array( 'db' => 'name', 'dt' => "Name" ),
array( 'db' => "CONCAT(`street`, ' ', `city`, '<br>', `postal`, ' ', `country`)",
'as' => "address",
'dt' => "address")
);

Categories