how to add new data and save to database in ag-grid - polymer

how to add new data and save to database in ag-grid? in https://ag-grid.com/javascript-grid-insert-remove/ i just get information to insert and remove data but not saving to database.
i use polymer to develope it.
this is the function to add data :
fAddBtn: function(){
var name = this.$.name.value;
var email = this.$.email.value;
var phone = this.$.phone.value;
var website = this.$.website.value;
var city = this.$.city.value;
var zipcode = this.$.zipcode.value;
var newList = {
'id' : '43',
'name' : name,
'email' : email,
'phone' : phone,
'website' : website,
'address' : {
'city' : city,
'zipcode' : zipcode
}
};
// this.set('rowData.0', newList);
this.gridOptions.api.addItems([newList]);
this.$.addDialog.close();
this.$.name.value = '';
this.$.email.value = '';
this.$.phone.value = '';
this.$.website.value = '';
this.$.city.value = '';
this.$.zipcode.value = '';
},

Related

Update a contact group by using a People API

Hello I need to update a created contact and remove from all earlier groups assigned. In this example a contact is created and it is later moved to Carpenter and now the actial question begins
How do I move a created contact to a different group. That is I need the contact to be moved to BLACKSMITHS group if that doesnt exist create BLACKSMITHS and assign BLACKSMITHS and remove from earlier assigned CARPENTERS
function doGet(e) {
// 1. CREATE CONTACT:
var contactResource = {
"names": [{
"displayNameLastFirst": "Smith Jefferson Jones",
"familyName": "Jones",
}],
/* "phoneNumbers": [{
'value': "+12345679962"
}],
"emailAddresses": [{
'value': ' '
}]*/
}
var contactResourceName = People.People.createContact(contactResource)["resourceName"];
// 2. CHECK IF GROUP EXISTS:
var groupName = "CARPENTERS";
var groups = People.ContactGroups.list()["contactGroups"];
var group = groups.find(group => group["name"] === groupName);
// 3. CREATE GROUP IF DOESN'T EXIST:
if (!group) {
var groupResource = {
contactGroup: {
name: groupName
}
}
group = People.ContactGroups.create(groupResource);
}
var groupResourceName = group["resourceName"];
// 4. ADD CONTACT TO GROUP:
var membersResource = {
"resourceNamesToAdd": [
contactResourceName
]
}
//HOW DO I MODIFY A CONTACT so that its must be removed from "CARPENTERS" group and ADDED TO "BLACKSMITHS" group
//??????????????????
People.ContactGroups.Members.modify(membersResource, groupResourceName);
return ContentService.createTextOutput("Success");
}
You just need to:
Add the contact to the new group (BLACKSMITHS), the way you did it in the code you shared.
Remove the contact from the previous group (CARPENTERS), using resourceNamesToRemove at contactGroups.members.modify.
Check the example below.
Code snippet:
// 1. Create Blacksmiths group if doesn't exist, add contact to group:
var groupNameToAdd = "BLACKSMITHS";
var groupToAdd = groups.find(group => group["name"] === groupNameToAdd);
if (!groupToAdd) { // Create group if doesn't exist
var groupToAddResource = {
contactGroup: {
name: groupNameToAdd
}
}
groupToAdd = People.ContactGroups.create(groupToAddResource);
}
var groupToAddResourceName = groupToAdd["resourceName"];
var membersToAddResource = {
"resourceNamesToAdd": [
contactResourceName
]
}
People.ContactGroups.Members.modify(membersToAddResource, groupToAddResourceName);
// 2. Remove contact from Carpenters:
var groupNameToRemove = "CARPENTERS";
var groupToRemove = groups.find(group => group["name"] === groupNameToRemove);
if (groupToRemove) { // Check that group exists
var groupToRemoveResourceName = groupToRemove["resourceName"];
var membersToRemoveResource = {
"resourceNamesToRemove": [
contactResourceName
]
}
People.ContactGroups.Members.modify(membersToRemoveResource, groupToRemoveResourceName);
}

How to save to database by using ajax

I have a code which is works fine, but the data cannot save to the database. I want to insert cost, currency_rate, profit_rate and pprice to database through Ajax. Here are the code of javascript and update.php, I have tried to modify the code to save in my Mysql server, but it didn't success. Can someone help with this?
javascript
$(".profitRate").change(function() {
var myArray = [];
//find closest table->next table
var elem = $(this).closest('table').next('table');
var action = elem.find('tr').data('action');
console.log(action)
var profitRate = Number($("#profitRate").val());
//looping
elem.find('tr').each(function() {
//get cost
var cost = $(this).find('input[name=cost]').val();
//get curency rate
var currecy_rate = $(this).find('select[name=currency_rate]').val();
//calculate profit
var profit_total = Math.round(cost * profitRate * currecy_rate)
$(this).find('input[name=pprice]').val(profit_total)
//add to json object
auto_array = {};
auto_array["cost"] = cost;
auto_array["currecy_rate"] = currecy_rate;
auto_array["pprice"] = profit_total;
myArray.push(auto_array) //push to array
});
console.log(myArray)
form_data = elem.find('tr').data('action');
$.ajax({
data: {
action: action,
form_data: form_data,
},
url: 'update.php',
type: 'post',
beforeSend: function() {
},
success: function(data) {
if(data == 1){
}
}
});
})
update.php
<?php
if ($_POST['action'] == 'update_price') {
parse_str($_POST['form_data'], $my_form_data);
$id = $my_form_data['id'];
$cost = $my_form_data['cost'];
$profit_rate = $my_form_data['profit_rate'];
$currency_rate = $my_form_data['currency_rate'];
$pprice = $my_form_data['pprice'];
$sql = $query = $finalquery = $sqlresult = '';
if ($cost){
$sql.="cost='$cost',";
}
if ($profit_rate){
$sql.="profit_rate='$profit_rate',";
}
if ($currency_rate){
$sql.="currency_rate='$currency_rate',";
}
if ($pprice){
$sql.="pprice='$pprice',";
$finalquery = rtrim($sql,',');
$query="UPDATE `gp_info` SET $finalquery where id=$id";
$sqlresult=mysql_query($query);
if($sqlresult){
$reback=1;
}else{
$reback=0;
}
echo $reback;
}
}

Node.js pushing data into Excel is not working

Hi I am trying to push data from my array of queries into excel but only the header seems to be displayed but the results are being pushed. When I did a console.log to see whether they would be pushed it says undefined. Any ideas as to why?
Example code:
var excel_data=[];
excel_data.push=(['Source', 'Count of thing 1', 'Count of thing 2', 'Count of thing 3']);
var database = new database(config);
var query_array = ['select count(*) as count_of_thing1 from table1', 'select count(*) as count_of_thing2 from table2', 'select count(*) as count_of_thing3 from table3'];
query_array.forEach(q => {
database.query(q)
.then(rows => {
_.each(rows, function(row){
excel_data.push([row.count_of_thing1, row.count_of_thing2, row.count_of_thing3]);
});
})
var wb = XLSX.utils.book_new();
var ws_name = "Query_Results";
var wscols = [ {width: 15} ];
var wsrows = [];
var ws = XLSX.utils.aoa_to_sheet(excel_data, {cellDates:true});
ws['!cols'] = wscols;
ws['!rows'] = wsrows;
XLSX.utils.book_append_sheet(wb, ws, ws_name);
var filenames = [
['mysql.xlsx', {bookSST:true}],
['mysql.csv', {bookSST:true}],
];
filenames.forEach(function(r) {
XLSX.writeFile(wb, r[0], r[1]);
XLSX.readFile(r[0]);
});
});

SQL Titanium using SQLite

I have a database look like this
I'm trying to get the NamaHalaman by doing some sql Query
function getPageByMdl(modul) {
var sql = "SELECT * FROM Halaman WHERE ModulLayanan = '"+modul+"' ORDER BY Urutan ASC";
var results = [];
var sqlResultSet = db.execute(sql);
while (sqlResultSet.isValidRow()) {
results.push({
namahalaman : sqlResultSet.fieldByName('NamaHalaman')
});
sqlResultSet.next();
}
sqlResultSet.close();
return results;
}
However the result length always give me 0 although as you can see it has 2 value in it. Another problem also happen when i try to run
var sql = "SELECT Distinct ModulLayanan FROM Halaman Order ASC";
it only returned the first value from the database which is Whistleblowing system.
Oh here's my function on showing the result
function loadDetailModule() {
data = getPageByMdl(modulTemp);
if (data.length > 0) {
var tableData = [];
//get through each item
for (var i = 0; i < data.length; i++) {
var detailmodul = data[i];
//create table row
var row = Titanium.UI.createTableViewRow({
dataIndex : i,
_title : detailmodul.namahalaman,
hasChild : true,
className : 'detailmodul_halaman',
filter : detailmodul.namahalaman,
height : 70,
backgroundColor : '#fff',
});
//title label for row at index i
var titleLabel = Titanium.UI.createLabel({
text : detailmodul.namahalaman,
font : {
fontSize : 14,
fontWeight : ' bold'
},
left : 70,
top : 10,
height : 50,
width : 210,
color : '#232',
dataIndex : i
});
row.add(titleLabel);
//add our little icon to the left of the row
var iconImage = Titanium.UI.createImageView({
image : 'img/eggcooking.png',
width : 50,
height : 50,
left : 10,
top : 10
});
row.add(iconImage);
//add the row to data array
tableData.push(row);
}
// set the data to tableview's data
detailModuleTable.data = tableData;
detailModuleTable.show();
} else {
detailModuleTable.hide();
}
}
Thank you in advance
the problem is because the Titanium does not refresh your Database when you run your application.
What you need is:
1. uninstall your application in emulator
or
2. you can change the database name
Titanium.Database.install('SLTemp.sqlite', 'SLTemp1');
var db = Ti.Database.open('SLTemp1');
to
Titanium.Database.install('SLTemp.sqlite', 'SLTemp');
var db = Ti.Database.open('SLTemp');

No data from the database after the files downloaded

I want to display the data on a particular condition of the table after the user enters the data you want to display.
But always failed to display the data. I am confused where the fault lies.
It looks like the variable $thn = $ _POST['thn'], $bln = $ _POST['bln'], $periode = $ _POST['periode'] is empty.
please help.
I have four files.
Here the codes:
1.absen_xls.php:
<?php
include '../../inc/inc.koneksi.php';
ini_set('display_errors', 1); ini_set('error_reporting', E_ERROR);
include '../../excel/PHPExcel.php';
include '../../excel/PHPExcel/IOFactory.php';
$table = 'absen_karyawan';
$bln = $_POST['bln']; //this not work, I don't know why?
$thn = $_POST['thn']; //this not work, I don't know why?
$periode = $_POST['periode']; //this not work, I don't know why?
$where = "WHERE tahun = '$thn' AND bulan = '$bln' AND periode = '$periode'";
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$sql = "SELECT namakaryawan,tahun,bulan,periode,absen FROM $table $where";
$query = mysql_query($sql);
// bold
$objPHPExcel->getActiveSheet()->getStyle("A2:C2")->applyFromArray(array("font" => array( "bold" => true)));
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A2', 'No')
->setCellValue('B2', 'Nama')
->setCellValue('C2', 'Kehadiran');
$baris = 3;
$no = 0;
while($row=mysql_fetch_array($query)){
$no = $no +1;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("A$baris", $no)
->setCellValue("B$baris", $row['namakaryawan'])
->setCellValue("C$baris", $row['absen']);
$baris = $baris + 1;
}
//border
$border = $baris - 1;
$styleArray = array(
'borders' => array(
'allborders' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
)
)
);
$objPHPExcel->getActiveSheet()->getStyle('A2:C'.$border)->applyFromArray($styleArray);
unset($styleArray);
//width
$objPHPExcel->getActiveSheet()->getColumnDimension("A")->setWidth(5);
$objPHPExcel->getActiveSheet()->getColumnDimension("B")->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension("C")->setWidth(15);
//align center
$objPHPExcel->getActiveSheet()->getStyle('A2:C'.$border)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Absen');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excelformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="absen.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
?>
2.ajax.php:
// JavaScript Document
$(document).ready(function(){
$(function(){
$('button').hover(
function() { $(this).addClass('ui-state-hover'); },
function() { $(this).removeClass('ui-state-hover'); }
);
});
$("#tampil_data").load('modul/lap-absen/tampil_data.php');
$("#print").click(function(){
var thn = $("#thn").val();
var bln = $("#bln").val();
var periode = $("#periode").val();
cetakabsen(thn,bln,periode);
});
function cetakabsen(c1,c2,c3){
var thn = c1;
var bln = c2;
var periode = c3;
$.ajax({
type : "POST",
url : "modul/lap-absen/absen_xls.php",
data : "thn="+thn+"&bln="+bln+"&periode="+periode,
success : function(data){
window.open('http://localhost/gudang/modul/lap-absen/absen_xls.php');
}
});
}
});
Change your function to this one:
function cetakabsen(c1,c2,c3){
var thn = c1;
var bln = c2;
var periode = c3;
var data = "thn="+thn+"&bln="+bln+"&periode="+periode;
window.open('http://localhost/gudang/modul/lap-absen/absen_xls.php?'+data);
}
And receive your values like this in absen_xls.php:
$bln = mysql_real_escape_string($_GET['bln']);
$thn = mysql_real_escape_string($_GET['thn']);
$periode = mysql_real_escape_string($_GET['periode']);
PS: Go for mysqli or PDO, dont use the mysql extension.