313 lines
9.6 KiB
JavaScript
Vendored
313 lines
9.6 KiB
JavaScript
Vendored
/**
|
|
* Fungsi untuk merender Dropdown dinamis
|
|
*
|
|
* @param {string} dropdownSelector - Selector untuk Dropdown
|
|
* @param {string} apiUrl - URL untuk endpoint API
|
|
* @param {value} selectedValue - Value untuk Selected
|
|
*/
|
|
function DropdownDefaultAll(dropdownSelector, apiUrl, selectedValue = null) {
|
|
var $dropdown = $(dropdownSelector);
|
|
$dropdown.prop("disabled", true);
|
|
$dropdown.empty(); // Menghapus semua opsi
|
|
$dropdown.append('<option value="" selected disabled>Loading...</option>');
|
|
|
|
$.ajax({
|
|
url: apiUrl, // URL untuk rute API yang diberikan sebagai parameter
|
|
type: "GET", // Jenis permintaan
|
|
success: function (data) {
|
|
// Clear existing options
|
|
$dropdown.empty();
|
|
$dropdown.append('<option value=""> - All - </option>');
|
|
|
|
// Mengisi dropdown dengan data dari API
|
|
if (data.length == 0) {
|
|
$dropdown.empty();
|
|
$dropdown.append(
|
|
'<option value=""> - No data -</option>'
|
|
);
|
|
} else {
|
|
$.each(data, function (index, item) {
|
|
$dropdown.append(
|
|
'<option value="' +
|
|
item.value +
|
|
'">' +
|
|
item.text +
|
|
"</option>"
|
|
);
|
|
});
|
|
}
|
|
|
|
// Menetapkan nilai yang dipilih jika ada dan tidak null
|
|
if (selectedValue !== null) {
|
|
$dropdown.val(selectedValue);
|
|
}
|
|
|
|
$dropdown.prop("disabled", false);
|
|
},
|
|
error: function (xhr) {
|
|
// Tangani kesalahan
|
|
$dropdown.prop("disabled", false);
|
|
$dropdown.empty();
|
|
$dropdown.append(
|
|
'<option value="" selected disabled>Unable fetch data.</option>'
|
|
);
|
|
console.error("Error fetching data from API:", xhr.responseText);
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Fungsi untuk merender Dropdown dinamis
|
|
*
|
|
* @param {string} dropdownSelector - Selector untuk Dropdown
|
|
* @param {string} apiUrl - URL untuk endpoint API
|
|
* @param {value} selectedValue - Value untuk Selected
|
|
*/
|
|
function DropdownRequire(dropdownSelector, apiUrl, selectedValue = null) {
|
|
var $dropdown = $(dropdownSelector);
|
|
$dropdown.prop("disabled", true);
|
|
$dropdown.empty(); // Menghapus semua opsi
|
|
$dropdown.append('<option value="" selected disabled>Loading...</option>');
|
|
|
|
$.ajax({
|
|
async: true,
|
|
url: apiUrl, // URL untuk rute API yang diberikan sebagai parameter
|
|
type: "GET", // Jenis permintaan
|
|
success: function (data) {
|
|
// Clear existing options
|
|
$dropdown.empty();
|
|
$dropdown.append('<option value=""> - Select Data - </option>');
|
|
|
|
// Mengisi dropdown dengan data dari API
|
|
if (data.length == 0) {
|
|
$dropdown.empty();
|
|
$dropdown.append(
|
|
'<option value=""> - No data -</option>'
|
|
);
|
|
} else {
|
|
$.each(data, function (index, item) {
|
|
$dropdown.append(
|
|
'<option value="' +
|
|
item.value +
|
|
'">' +
|
|
item.text +
|
|
"</option>"
|
|
);
|
|
});
|
|
}
|
|
|
|
if (selectedValue !== null) {
|
|
$dropdown.val(selectedValue);
|
|
}
|
|
$dropdown.prop("disabled", false);
|
|
},
|
|
error: function (xhr) {
|
|
// Tangani kesalahan
|
|
$dropdown.prop("disabled", false);
|
|
$dropdown.empty();
|
|
$dropdown.append(
|
|
'<option value="" selected disabled>Unable fetch data.</option>'
|
|
);
|
|
console.error("Error fetching data from API:", xhr.responseText);
|
|
},
|
|
});
|
|
}
|
|
|
|
function DropdownSelect2(dropdownSelector, apiUrl, placeholder = 'Select options', selectedValues = null) {
|
|
var $dropdown = $(dropdownSelector);
|
|
$dropdown.select2({
|
|
placeholder: placeholder,
|
|
allowClear: true,
|
|
multiple: true, // Menambahkan fitur multiple select
|
|
ajax: {
|
|
url: apiUrl, // URL API untuk mengambil data
|
|
dataType: 'json',
|
|
delay: 250, // Menambahkan delay untuk mengurangi beban server
|
|
processResults: function (data) {
|
|
return {
|
|
results: data.map(function(item) {
|
|
return {
|
|
id: item.value, // ID atau value dari opsi dropdown
|
|
text: item.text // Text atau label dari opsi dropdown
|
|
};
|
|
})
|
|
};
|
|
},
|
|
cache: true
|
|
}
|
|
});
|
|
|
|
// Jika ada nilai yang sudah terpilih, set value tersebut
|
|
if (selectedValues !== null && selectedValues.length > 0) {
|
|
selectedValues.forEach(function(value) {
|
|
var newOption = new Option(value.text, value.id, true, true);
|
|
$dropdown.append(newOption).trigger('change');
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Initialize DataTable with dynamic settings and custom search.
|
|
* @param {string} tableSelector - Selector for the DataTable element.
|
|
* @param {string} urlAjax - URL for fetching data from the server.
|
|
* @param {Array} columns - Columns configuration for DataTable.
|
|
* @param {Object} options - Additional DataTable options.
|
|
* @param {Object} searchParams - Custom search parameters with values.
|
|
*/
|
|
|
|
var tableInstance = null;
|
|
|
|
function initDataTable(
|
|
tableSelector,
|
|
urlAjax,
|
|
columns,
|
|
options = {},
|
|
customSearchForm = {}
|
|
) {
|
|
if ($.fn.DataTable.isDataTable(tableSelector)) {
|
|
$(tableSelector).DataTable().destroy(); // Hapus instance DataTables sebelumnya
|
|
$(tableSelector).empty(); // Kosongkan tabel untuk inisialisasi ulang
|
|
}
|
|
try {
|
|
tableInstance = $(tableSelector).DataTable({
|
|
processing: true,
|
|
serverSide: true,
|
|
ajax: {
|
|
url: urlAjax,
|
|
type: "GET",
|
|
headers: {
|
|
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr(
|
|
"content"
|
|
),
|
|
},
|
|
data: function (d) {
|
|
// Menambahkan parameter dari objek customSearchForm ke dalam request DataTables
|
|
$.each(customSearchForm, function (key, value) {
|
|
if (value !== "") {
|
|
d[key] = value; // Hanya tambahkan jika ada nilainya
|
|
}
|
|
});
|
|
},
|
|
},
|
|
columns: columns,
|
|
...options,
|
|
});
|
|
} catch (e) {
|
|
console.log("Error occurred: " + e);
|
|
}
|
|
}
|
|
|
|
function initSelect2EmailField(elementId, jsonData, selectedValue) {
|
|
// Periksa jika jsonData kosong
|
|
var dataval = [];
|
|
|
|
if (!jsonData || jsonData.length === 0) {
|
|
console.error("JSON data is empty.");
|
|
return;
|
|
}
|
|
dataval = JSON.parse(jsonData);
|
|
let selectOptions = dataval.map(function (d) {
|
|
return { id: d, text: d };
|
|
});
|
|
|
|
// Menginisialisasi Select2 pada elemen yang diberikan
|
|
$(elementId).select2({
|
|
placeholder: "Select Email",
|
|
theme: "bootstrap4",
|
|
data: selectOptions,
|
|
});
|
|
|
|
// Menampilkan nilai yang sudah dipilih di awal
|
|
$(elementId).val(selectedValue).trigger("change");
|
|
}
|
|
|
|
function initializeDropzoneUploader(element, url) {
|
|
var dz = new Dropzone(element, {
|
|
url: url, // URL untuk upload file
|
|
maxFilesize: 10, // Ukuran maksimal file (MB)
|
|
acceptedFiles: "", // Tipe file yang diizinkan
|
|
addRemoveLinks: true, // Tampilkan tombol hapus
|
|
dictRemoveFile: "Hapus",
|
|
parallelUploads: 5, // Unggah paralel untuk 5 file sekaligus
|
|
autoProcessQueue: false, // Proses otomatis saat file dipilih
|
|
|
|
// Fungsi callback untuk hasil sukses
|
|
success: function (file, response) {
|
|
console.log("File uploaded successfully", response);
|
|
},
|
|
|
|
// Fungsi callback untuk menangani error
|
|
error: function (file, response) {
|
|
console.log("Failed to upload file", response);
|
|
},
|
|
});
|
|
dz.autoDiscover = false;
|
|
}
|
|
|
|
|
|
function numericOnlyComma(inputText) {
|
|
return inputText.replace(/[^0-9.]/g, ''); // Remove any character that is not a number or dot
|
|
}
|
|
|
|
|
|
|
|
|
|
function shortChar(fileName) {
|
|
if (fileName.length > 10) {
|
|
return fileName.substring(0, 7) + '...';
|
|
}
|
|
return fileName;
|
|
}
|
|
|
|
|
|
function showLoader(element){
|
|
$(element).waitMe({
|
|
effect : 'ios',
|
|
text : 'Sedang memuat data...',
|
|
});
|
|
}
|
|
|
|
function CheckArrayNull(params) {
|
|
let invalidFields = [];
|
|
|
|
for (let key in params) {
|
|
if (params.hasOwnProperty(key)) {
|
|
if (params[key] === null || params[key] === undefined || params[key] === '') {
|
|
invalidFields.push(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (invalidFields.length > 0) {
|
|
swal.fire({
|
|
title: 'Validation Error',
|
|
text: `Field berikut: ${invalidFields.join(', ')} tidak boleh kosong.`,
|
|
icon: 'error',
|
|
confirmButtonText: 'OK'
|
|
});
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
function ValidationField(paramValue, Messages) {
|
|
if (paramValue === null || paramValue === undefined || paramValue === '') {
|
|
swal.fire({
|
|
text: `${Messages}`,
|
|
icon: 'error',
|
|
confirmButtonText: 'OK'
|
|
});
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function hideLoader(element){
|
|
$(element).waitMe("hide");
|
|
}
|
|
|
|
|