const toggler = document.querySelector(".btn");
toggler.addEventListener("click",function(){
document.querySelector("#sidebar").classList.toggle("collapsed");
});
<script>
function getIndexHtml() {
var htmlContent = HtmlService.createHtmlOutputFromFile('index').getContent();
return htmlContent;
}
let dataSet
window.onload = () => {
<!--$.LoadingOverlay("show00");-->
google.script.run.withSuccessHandler(data => {
dataSet = data;
showData(dataSet);
$.LoadingOverlay("hide00");
}).getData();
}
function changePage(pages) {
console.log(pages)
var idElement = [...document.querySelectorAll('section')].forEach(elm => {
if(elm.id===pages){
document.getElementById(elm.id).style.display = "block"
}else{
document.getElementById(elm.id).style.display = "none"
}
});
}
function showData(dataSet) {
$(document).ready(function () {
$('#example').DataTable({
columns: [
{title: 'ID'},
{title: 'Name'},
{title: 'Class'},
{title: 'Email'},
{title: 'Phone'},
{title: 'Phone'},
{title: 'Phone'},
{title: 'Phone'},
{title: 'Phone'},
{title: 'Phone'},
{title: 'Phone'},
{title:'Edita', defaultContent: "<button type='button' class='btn btn-outline-primary btn-xs editBtn' data-toggle='modal' data-target='#myModal' onclick='editData(this);'><i class='fa fa-edit'></i></button></td><td><button type='button' class='btn btn-outline-danger btn-xs deleteBtn' onclick='deleteData(this);'><i class='fa fa-trash''></i></button>"},
],
data: dataSet,
destroy: true,
//lengthMenu: [5,10, 25, 50, 75, 100],
lengthMenu: [10, 25, 50, { label: 'All', value: -1 }]
//order: [[0, 'desc']] esc
});
});
}
const sidebarToggle = document.querySelector("#sidebar-toggle");
sidebarToggle.addEventListener("click",function(){
document.querySelector("#sidebar").classList.toggle("collapsed");
});
document.querySelector(".theme-toggle").addEventListener("click",() => {
toggleLocalStorage();
toggleRootClass();
});
function toggleRootClass(){
const current = document.documentElement.getAttribute('data-bs-theme');
const inverted = current == 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-bs-theme',inverted);
}
function toggleLocalStorage(){
if(isLight()){
localStorage.removeItem("light");
}else{
localStorage.setItem("light","set");
}
}
function isLight(){
return localStorage.getItem("light");
}
if(isLight()){
toggleRootClass();
}
</script>
<script>
/**
* Prevent forms from submitting.
* */
function preventFormSubmit() {
var forms = document.querySelectorAll('form');
for (var i = 0; i < forms.length; i++) {
forms[i].addEventListener('submit', function(event) {
event.preventDefault();
});
}
}
window.addEventListener("load", functionInit, true);
window.addEventListener("beforeunload", falseState, true);
/**
* INITIALIZE FUNCTIONS ONLOAD
* */
function functionInit(){
$('#spinnerModal').modal('show');
preventFormSubmit();
getLastTenRows();
};
/**
* HANDLE FORM SUBMISSION
* */
function handleFormSubmit(formObject) {
google.script.run.withSuccessHandler(createTable).processForm(formObject);
setTimeout(function() {$('#myModal').modal('hide');}, 2000);
document.getElementById("message").innerHTML = "<div class='alert alert-warning' role='alert'>Data berhasil ditambahkan!.</div>";
//document.getElementById("myForm").reset();
}
/**
* Clear form when pop-up is closed.
* */
function clearForm() {
document.getElementById("message").innerHTML = "";
document.getElementById("myForm").reset();
}
/**
* hide colom 1
* */
function backhideCol0() {
var oTable = $('#dataTable').dataTable();
oTable.fnSetColumnVis( 0, false );
}
/**
* GET LAST 10 ROWS
* */
function getLastTenRows (){
google.script.run.withSuccessHandler(createTable).getLastTenRows();
}
/**
* GET ALL DATA
* */
function getAllData(){
//document.getElementById('dataTable').innerHTML = "";
google.script.run.withSuccessHandler(createTable).getAllData();
}
/**
* CREATE THE DATA TABLE
* */
function createTable(dataArray) {
document.getElementById('myForm').reset();
document.getElementById('message').innerHTML = "";
if(dataArray){
var result = "<div>"+
"<table class='table table-sm' style='font-size:1em'>"+
"<thead style='white-space: nowrap'>"+
"<tr>"+
//Change table headings to match witht he Google Sheet
"<th scope='col'>RecID</th>"+
"<th scope='col'>Data de Entrada</th>"+
"<th scope='col'>Responsável Pelo Cad</th>"+
"<th scope='col'>Forma de Entrega Documento</th>"+
"<th scope='col'>ANO</th>"+
"<th scope='col'>~MÊS</th>"+
"<th scope='col'>Trimestre</th>"+
"<th scope='col'>Período</th>"+
"<th scope='col'>Data do Evento</th>"+
"<th scope='col'>Nome do Evento</th>"+
"<th scope='col'>Local/Endereço do Evento</th>"+
"<th scope='col'>Bairro</th>"+
"<th scope='col'>Cidades/Município</th>"+
"<th scope='col'>hora. iníc.</th>"+
"<th scope='col'>hora. fim</th>"+
"<th scope='col'>Tipo de Evento</th>"+
"<th scope='col'>SubTipo</th>"+
"<th scope='col'>Qtde Dias</th>"+
"<th scope='col'>Est.de Público</th>"+
"<th scope='col'>Documento5T</th>"+
"<th scope='col'>Documento6U</th>"+
"<th scope='col'>Tipo de Policiamento</th>"+
"<th scope='col'>File 1</th>" +
"<th scope='col'>File 2</th>" +
"<th scope='col'>WC</th>"+
"<th scope='col'>AF Time</th>"+
"<th scope='col'>Aksi</th>"+
"<th scope='col'></th>"+
"</tr>"+
"</thead>";
for(var i=0; i<dataArray.length; i++) {
result += "<tr>";
for(var j=0; j<dataArray[i].length; j++){
result += "<td>"+dataArray[i][j]+"</td>";
}
result += "<td><button type='button' class='btn btn-outline-primary btn-xs editBtn' data-toggle='modal' data-target='#myModal' onclick='editData(this);'><i class='fa fa-edit'></i></button></td>";
result += "<td><button type='button' class='btn btn-outline-danger btn-xs deleteBtn' onclick='deleteData(this);'><i class='fa fa-trash''></i></button></td>";
result += "</tr>";
}
result += "</table></div>";
var div = document.getElementById('dataTable');
div.innerHTML = result;
$(document).ready(function() {
$('#dataTable').DataTable({
destroy:true,
responsive: true,
select: true,
ordering: true,
order: [[0, 'desc' ]],
pageLength: 5,
lengthMenu: [
[5, 10, 25, 50, 100,150, -1 ],
['5', '10', '25', '50','100', 'All' ]
],
"dom": "<'row'<'col-6'l><'col-6'f>>" +
"<'row'<'col-sm-12'tr>>" +
"<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",
// ganti bahasa/info di datatable
language: {
lengthMenu: "_MENU_ ",
zeroRecords: "tidak ditemukan",
info: '<i class="fas fa-angle-double-left"></i> _START_ ke _END_ dari _TOTAL_ <i class="fas fa-angle-double-right"></i>',
infoEmpty: "menampilkan 0 ke 0 dari 0 baris",
infoFiltered: "(dari _MAX_ baris data)",
infoPostFix: "",
search: '<i class="fas fa-search" fa-2x></i> :',
url: "",
paginate: {
first: "awal",
previous: '<i class="fas fa-chevron-left fa-lg" ></i>',
next: '<i class="fas fa-chevron-right fa-lg"></i>',
last: "akhir"
},
},
columnDefs: [{
targets: [1, 8, 9],
className: 'all',
},
{
targets: [0],
visible: false, //hide kolom pertama/0
searchable: true,
},
{
targets: [3],
className: 'dt-body-center',
"render": function(data, type, row, meta) {
if (type === 'display' && data.length > 5) {
data =
'<a href="https://wa.me/62' +
data +
'?text=' +
row[
3] +
'" target="_blank">' +
'<i class="fa-brands fa-whatsapp" style="font-size:20px;color:red"></i>' +
'</a>';
}
return data;
}
},
{
targets: [22], //colom 7 adalah kolom file1 doc pdf
className: 'dt-body-center',
"render": function(data, type, row, meta) {
if (type === 'display' && data.length > 20) {
data = '<a href="' + data + '?text=' + row[22] + '" target="_blank">' + '<i class="fa fa-file" style="font-size:15px;color:black"></i>' + '</a>';
}
return data;
}
},
{
targets: [23], //colom 8 adalah kolom file2 doc pdf
className: 'dt-body-center',
"render": function(data, type, row, meta) {
if (type === 'display' && data.length > 20) {
data = '<a href="' + data + '?text=' + row[23] + '" target="_blank">' + '<i class="fa fa-file" style="font-size:15px;color:black"></i>' + '</a>';
}
return data;
}
},
],
initComplete: function() {
var column1 = this.api().column(7); //colom 4 adalah kolom gender
var select = $('#filterGender')
.on('change', function() {
var val = $(this).val();
column1.search(val ? '^' + $(this).val() + '$' : val, true, false).draw();
});
column1.data().unique().sort().each(function(d, j) {
select.append('<option value="' + d + '">' + d + '</option>');
});
var exist1 = {};
$('#filterGender > option').each(function() {
if (exist1[$(this).val()])
$(this).remove();
else
exist1[$(this).val()] = true;
});
var column2 = this.api().column(9); //colom 6 adalah kolom kota
var select = $('#filterKota')
.on('change', function() {
var val = $(this).val();
column2.search(val ? '^' + $(this).val() + '$' : val, true, false).draw();
});
column2.data().unique().sort().each(function(d, j) {
select.append('<option value="' + d + '">' + d + '</option>');
});
var exist2 = {};
$('#filterKota > option').each(function() {
if (exist2[$(this).val()])
$(this).remove();
else
exist2[$(this).val()] = true;
});
var table = $('#dataTable').DataTable();
var buttons = new $.fn.dataTable.Buttons(table, {
buttons: [{
extend: 'excelHtml5',
title: 'Data Laporan',
filename: 'Download_Data_Laporan',
exportOptions: {
orthogonal: 'export',
columns: [ 0, 1, 2, 3, 4, 5, 6, 7 ],
}
},
{
extend: 'pdfHtml5',
title: 'Data Laporan',
filename: 'Download_Data_Laporan',
orientation: 'landscape',
pageSize: "A3",
customize: function (doc) {
doc.pageMargins = [20, 20, 20, 20];
},
exportOptions: {
orthogonal: 'export',
columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
}
},
{
extend: 'copyHtml5',
exportOptions: {
orthogonal: 'export',
columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
}
},
{
extend: 'csvHtml5',
title: 'Data Laporan',
filename: 'Download_Data_Laporan',
exportOptions: {
orthogonal: 'export',
columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
}
},
],
}).container().appendTo($('#buttons'));
$('#excel').on('click', function() {
$('.buttons-excel').click()
});
$('#pdf').on('click', function () {
$('.buttons-pdf').click()
});
$('#copy').on('click', function () {
$('.buttons-copy').click()
});
$('#csv').on('click', function () {
$('.buttons-csv').click()
});
//Show/hide kolom
document.querySelectorAll('a.dropdown-item').forEach((el) => {
el.addEventListener('click', function (e) {
e.preventDefault();
let columnIdx = e.target.getAttribute('data-column');
let column = table.column(columnIdx);
// Toggle the visibility
column.visible(!column.visible());
});
});
// cari tanggal/filter tanggal
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
var min = $('#min-date').val();
var max = $('#max-date').val();
var createdAt = data[5] || 0; // Sesuaikan kolom table
//var createdAt = data[5] || data[5] || 0; //
if (
(min == "" || max == "") ||
(moment(createdAt).isSameOrAfter(min) && moment(createdAt).isSameOrBefore(max))
) {
return true;
}
return false;
}
);
$('.filter-datepicker').change(function () {
table.draw();
});
},
});
});
}
}
/**
* DELETE DATA
* */
function deleteData(el) {
var oTable = $('#dataTable').dataTable();
// Hide the second column after initialisation
oTable.fnSetColumnVis(0, true);
Swal.fire({
title: 'Apa kamu yakin?',
icon: 'warning',
html: `<input type="password" id="password" class="swal2-input" placeholder="Input Password">`,
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
cancelButtonText: 'Batal',
confirmButtonText: 'Ya, Hapus data ini!',
allowOutsideClick: false,
preConfirm: () => {
var pass = "123";
var password = Swal.getPopup().querySelector('#password').value
if (password == pass) {
var recordId = el.parentNode.parentNode.cells[0].innerHTML;
google.script.run.withSuccessHandler(createTable).deleteData(recordId);
oTable.fnSetColumnVis(0, false);
} else {
Swal.showValidationMessage('Invalid Password')
}
},
}).then((result) => {
if (result.isConfirmed) {
Swal.fire(
'Hapus !',
'File telah dihapus!',
'success',
)
} else {
Swal.fire(
'Batal !',
'Batal hapus file ini :)',
'error',
)
oTable.fnSetColumnVis(0, false);
}
});
}
//FOR POPULATE FORM------------------------------------------------------------------------------------------------------
//RETRIVE DATA FROM GOOGLE SHEET FOR KOTA DROPDOWN
function createKotaDropdown() {
//SUBMIT YOUR DATA RANGE FOR DROPDOWN AS THE PARAMETER
google.script.run.withSuccessHandler(kotaDropDown).getDropdownListKota("Kota!A1:A");
}
//POPULATE KOTA DROPDOWNS
var list = document.getElementById('kota');
for (var i = 0; i < values.length; i++) {
var option = document.createElement("option");
option.value = values[i];
option.text = values[i];
list.appendChild(option);
}
}
/**
* EDIT DATA
* */
function editData(el) {
var oTable = $('#dataTable').dataTable();
// // Hide/show the column after initialisation
oTable.fnSetColumnVis( 0, true );
var recordId = el.parentNode.parentNode.cells[0].innerHTML;
google.script.run.withSuccessHandler(populateForm).getRecordById(recordId);
}
/**
* POPULATE FORM
* */
function populateForm(records) {
document.getElementById('RecId').value = records[0][0];
document.getElementById('nama').value = records[0][1];
document.getElementById('email').value = records[0][2];
document.getElementById('telp').value = records[0][3];
document.getElementById('gender').value = records[0][4];
document.getElementById('tglLahir').value = records[0][5];
document.getElementById('kota').value = records[0][6];
document.getElementById("message").innerHTML = "<div class='alert alert-warning' role='alert'>Update [ID: " + records[0][0] + "]</div>";
}
</script>