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

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")
);

Related

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!

dataTable, add row button when using serverside processing

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 )
);
}
?>

In yii2 how to change Dropdown data as per role from?

I have data in Drop down list as per role(for every role table are different).
I don't have any idea how to do because I am not familiar with yii2 First select a role and after selecting role I want data from different different table as per role
<?= $form->field($model, 'role')->dropDownList( [ 'A' => 'Admin', 'M' => 'Member', 'P' => 'Practice', ],['prompt'=>'--Select a Role--',]);?>
<?= $form->field($model, 'code')->dropDownList(
ArrayHelper::map(Member::find()->all(), 'id', 'memberCode'),
['id'=>'memberCode']
);
?>
You Need to update your 2nd dropdown when you select any value from dropdown 1st. Lets say 2nd drop down have id #dropdown2 so, i dit in yii you can change it according to yii2.
echo $form->dropDownListGroup(
$model, 'id', array(
'wrapperHtmlOptions' => array(),
'widgetOptions' => array(
'data' => CSystemGenerated::getProjectName($model->c_id),
'htmlOptions' => array(
'prompt' => 'Select Project',
'ajax' => array(
'type' => 'POST',
'url' => your url,
'update' => '#dropdown2',
//'dataType' => 'json',
'data'=>array('id'=>'js:this.value'),
)
),
),
)
);
<?php
echo $form->dropDownListGroup(
$model, 'tag', array(
'wrapperHtmlOptions' => array(),
'widgetOptions' => array(
'data' =>$this->getTags(),
'htmlOptions' => array(
'prompt' => 'Select Tags',
),
)
)
);
?>
check this link it will help you more.
Link1
link2
Please Refer to Kartik Dependent Drop Down in Yii2 ..This will surely helpfull

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 .

Add new field CakePHP Javascript

I have a list of item and retrieving it in PHP array:
array(
'Daging Kambing Beku 2' => array(
(int) 4 => '80.00',
),
'Daging Rusa' => array(
(int) 32 => '4000.00'
),
'Sayur Kangkung' => array(
(int) 31 => '5000.00'
),
'Sayur Sawi' => array(
(int) 30 => '600.00',
(int) 29 => '4.00'
),
'Sayur Lemau' => array(
(int) 28 => '1.00'
),
'Sayur Sayuranj' => array(
(int) 27 => '1.00'
),
'Sayur Segar ke?' => array(
(int) 26 => '5.00'
),
'French Fries' => array(
(int) 25 => '90.00'
),
'Daging Arnab' => array(
(int) 33 => '5.00'
),
'Daging Kerbaul' => array(
(int) 34 => '20.00'
)
)
Array explain :
'Daging Kambing Beku 2' => array(
(int) 4 => '80.00',
),
'Daging Kambing Beku 2' is the name of item.
(int) 4 is the ID for the items table.
'80.00' is the price .
My problem is,I want to make a function add new Item each time Customer make an order.
I'd try using jquery read the PHP array and to add a list of item when Customer clicking "Add New Item" but it's failed.
I want to do something like this:
https://demo.phppointofsale.com/index.php/sales
Customer can add many Items without submitting page frequently.
I'm using CakePHP for my development.
I'll appreciate any kind of help. thanks.
Just a reminder, to use php array in js, you need to do some convertion like :
<script>
<?php echo "var js_array = ".json_encode($php_array).';'?>
</script>

Categories