feat: slicing cms slider
parent
98fcb3eaa6
commit
7981812499
|
|
@ -0,0 +1,75 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace BpsRwApp.Controllers
|
||||
{
|
||||
[Route("[controller]/[action]")]
|
||||
public class SliderUtamaController : AppControllerBase
|
||||
{
|
||||
public IActionResult Index()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Table()
|
||||
{
|
||||
var data = Enumerable.Range(1, 3)
|
||||
.Select(index =>
|
||||
{
|
||||
var isPublished = index % 3 != 0;
|
||||
var publishToggle = isPublished
|
||||
? $"<input type=\"checkbox\" class=\"toggle toggle-success text-primary-500 toggle-publish\" data-id=\"{index}\" checked />"
|
||||
: $"<input type=\"checkbox\" class=\"toggle toggle-success toggle-publish\" data-id=\"{index}\" />";
|
||||
|
||||
return new
|
||||
{
|
||||
no = index,
|
||||
judul = "Pengelolaan Sampah Lingkup Rukun Warga",
|
||||
deskripsi = "Program dari Dinas Lingkungan Hidup Provinsi Jakarta yang bertujuan untuk mengoptimalkan pengelolaan sampah di tingkat RW",
|
||||
gambar = "/images/hero-bg.png",
|
||||
publish = publishToggle,
|
||||
aksi = "<div class=\"flex gap-2\">" +
|
||||
$"<button class=\"btn bg-white text-primary-500 btn-sm border-primary-500 rounded-full btn-edit\" data-id=\"{index}\">Edit</button>" +
|
||||
$"<button class=\"btn bg-white text-gray-800 btn-sm border-gray-800 rounded-full btn-detail\" data-id=\"{index}\">Detail</button>" +
|
||||
"</div>",
|
||||
};
|
||||
})
|
||||
.ToArray();
|
||||
|
||||
var response = new
|
||||
{
|
||||
data = data
|
||||
};
|
||||
|
||||
return Json(response);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Create()
|
||||
{
|
||||
// create data
|
||||
return Json(new { success = true, message = "Slider berhasil ditambahkan" });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Update()
|
||||
{
|
||||
// update data
|
||||
return Json(new { success = true, message = "Slider berhasil diupdate" });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Delete(int id)
|
||||
{
|
||||
// delete data
|
||||
return Json(new { success = true, message = "Slider berhasil dihapus" });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult TogglePublish(int id, bool isPublished)
|
||||
{
|
||||
// publish status
|
||||
return Json(new { success = true, message = $"Status publish berhasil diubah menjadi {(isPublished ? "Published" : "Unpublished")}" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -266,9 +266,16 @@
|
|||
</details>
|
||||
</li>
|
||||
<li>
|
||||
<details @(controller == "TentangBpsRw" ? "open" : "")>
|
||||
<details @(new[] { "TentangBpsRw", "SliderUtama" }.Contains(controller) ? "open" : "")>
|
||||
<summary>CMS</summary>
|
||||
<ul>
|
||||
<li>
|
||||
<a asp-controller="SliderUtama" asp-action="Index"
|
||||
class="@(controller == "SliderUtama" ? "menu-active" : "")">
|
||||
<span class="icon icon-fill">text_fields</span>
|
||||
Slider Utama
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a asp-controller="TentangBpsRw" asp-action="Index"
|
||||
class="@(controller == "TentangBpsRw" ? "menu-active" : "")">
|
||||
|
|
|
|||
|
|
@ -25,19 +25,19 @@
|
|||
|
||||
<!-- Navigation Menu -->
|
||||
<div class="grid gap-1 lg:gap-3 grid-cols-5">
|
||||
<a href="#" class="rounded-full flex justify-center items-center gap-2 hover:@hoverColor px-4 py-2 transition-colors">
|
||||
<a href="/" class="rounded-full flex justify-center items-center gap-2 hover:@hoverColor px-4 py-2 transition-colors">
|
||||
<div class="text-white text-sm lg:text-base font-semibold font-jakarta leading-normal">Beranda</div>
|
||||
</a>
|
||||
<a href="#" class="rounded-full flex justify-center items-center gap-2 hover:@hoverColor px-4 py-2 transition-colors">
|
||||
<a href="#tentang" class="rounded-full flex justify-center items-center gap-2 hover:@hoverColor px-4 py-2 transition-colors">
|
||||
<div class="text-white text-sm lg:text-base font-semibold font-jakarta leading-normal">Tentang</div>
|
||||
</a>
|
||||
<a href="#" class="rounded-full flex justify-center items-center gap-2 hover:@hoverColor px-4 py-2 transition-colors">
|
||||
<a href="#pergub" class="rounded-full flex justify-center items-center gap-2 hover:@hoverColor px-4 py-2 transition-colors">
|
||||
<div class="text-white text-sm lg:text-base font-semibold font-jakarta leading-normal">Regulasi</div>
|
||||
</a>
|
||||
<a href="#" class="rounded-full flex justify-center items-center gap-2 hover:@hoverColor px-4 py-2 transition-colors">
|
||||
<a href="/Panduan" class="rounded-full flex justify-center items-center gap-2 hover:@hoverColor px-4 py-2 transition-colors">
|
||||
<div class="text-white text-sm lg:text-base font-semibold font-jakarta leading-normal">Panduan</div>
|
||||
</a>
|
||||
<a href="#" class="rounded-full flex justify-center items-center gap-2 hover:@hoverColor px-4 py-2 transition-colors">
|
||||
<a href="/Edukasi" class="rounded-full flex justify-center items-center gap-2 hover:@hoverColor px-4 py-2 transition-colors">
|
||||
<div class="text-white text-sm lg:text-base font-semibold font-jakarta leading-normal">Edukasi</div>
|
||||
</a>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,407 @@
|
|||
@{
|
||||
ViewData["Title"] = "Slider Utama";
|
||||
}
|
||||
|
||||
<!-- Toast Notification -->
|
||||
<div id="successToast" class="fixed top-20 right-6 z-9999 hidden">
|
||||
<div class="alert bg-green-50 border border-green-200 shadow-lg flex items-start gap-3 pr-12 min-w-[320px]">
|
||||
<div class="shrink-0">
|
||||
<div class="w-5 h-5 rounded-full bg-green-500 flex items-center justify-center">
|
||||
<span class="icon text-white text-xs">check</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex-1">
|
||||
<h3 id="toastTitle" class="font-semibold text-gray-900">Slide berhasil ditambah!</h3>
|
||||
<p id="toastMessage" class="text-sm text-gray-600">Berhasil! Slide baru telah berhasil ditambahkan.</p>
|
||||
</div>
|
||||
<button onclick="closeToast()" class="absolute top-3 right-3 text-gray-400 hover:text-gray-600">
|
||||
<span class="icon text-lg">cancel</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="breadcrumbs text-sm mb-4">
|
||||
<ul>
|
||||
<li class="text-gray-500"><a>CMS</a></li>
|
||||
<li id="breadcrumb-active">Slider Utama</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<!-- Header -->
|
||||
<div class="flex items-center justify-between mb-6">
|
||||
<h2 class="text-xl font-semibold text-gray-800">Slider Utama</h2>
|
||||
<div class="flex items-center gap-3">
|
||||
<button onclick="modal_tambah.showModal()" class="btn btn-primary rounded-full">
|
||||
<span class="icon icon-fill">add</span>
|
||||
Tambah
|
||||
</button>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Table -->
|
||||
<div class="card bg-white shadow-sm">
|
||||
<div class="card-body p-0">
|
||||
<table class="table-zebra table w-full" id="slider-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="w-[5%]">No</th>
|
||||
<th class="w-[20%]">Judul</th>
|
||||
<th class="w-[35%]">Deskripsi</th>
|
||||
<th class="w-[10%]">Gambar</th>
|
||||
<th class="w-[10%]">Publish</th>
|
||||
<th class="w-[15%]">Aksi</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody></tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Modal Tambah Slider -->
|
||||
<dialog id="modal_tambah" class="modal modal-bottom sm:modal-middle">
|
||||
<div class="modal-box w-full sm:max-w-lg">
|
||||
<button class="btn btn-sm btn-circle absolute right-2 top-2" onclick="modal_tambah.close()">✕</button>
|
||||
<h3 class="text-lg font-bold mb-4">Tambah Slider</h3>
|
||||
<form id="form_tambah" enctype="multipart/form-data">
|
||||
<div class="space-y-4">
|
||||
<!-- Preview iamge -->
|
||||
<div id="preview_container_tambah" class="hidden mb-4">
|
||||
<img id="preview_gambar_tambah" src="" alt="Preview"
|
||||
class="w-full h-64 object-cover rounded-lg shadow-md">
|
||||
</div>
|
||||
|
||||
<fieldset class="fieldset">
|
||||
<legend class="fieldset-legend">Gambar <span class="text-red-500">*</span></legend>
|
||||
<input type="file" id="input_gambar" name="Gambar" class="file-input file-input-bordered w-full"
|
||||
accept="image/*" required>
|
||||
<p class="text-sm text-gray-500 mt-1">Ukuran maksimal 5MB.</p>
|
||||
</fieldset>
|
||||
<fieldset class="fieldset">
|
||||
<legend class="fieldset-legend">Judul <span class="text-red-500">*</span></legend>
|
||||
<input type="text" name="Judul" class="input input-bordered w-full" placeholder="Masukkan judul"
|
||||
required>
|
||||
</fieldset>
|
||||
<fieldset class="fieldset">
|
||||
<legend class="fieldset-legend">Deskripsi <span class="text-red-500">*</span></legend>
|
||||
<textarea name="Deskripsi" class="textarea textarea-bordered w-full h-32"
|
||||
placeholder="Masukkan deskripsi" required></textarea>
|
||||
</fieldset>
|
||||
</div>
|
||||
<div class="modal-action mt-6">
|
||||
<button type="button" class="btn rounded-full" onclick="resetFormTambah()">
|
||||
<span class="icon">cancel</span>
|
||||
Tutup
|
||||
</button>
|
||||
<button type="submit" class="btn bg-primary-500 text-white rounded-full hover:bg-primary-700">
|
||||
<span class="icon">save</span>
|
||||
Simpan
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<form method="dialog" class="modal-backdrop">
|
||||
<button onclick="resetFormTambah()">close</button>
|
||||
</form>
|
||||
</dialog>
|
||||
|
||||
<!-- Modal Edit Slider -->
|
||||
<dialog id="modal_edit" class="modal modal-bottom sm:modal-middle">
|
||||
<div class="modal-box w-full sm:max-w-lg">
|
||||
<button class="btn btn-sm btn-circle absolute right-2 top-2" onclick="modal_edit.close()">✕</button>
|
||||
<h3 class="text-lg font-bold mb-4">Edit Slider</h3>
|
||||
<form id="form_edit" enctype="multipart/form-data">
|
||||
<input type="hidden" id="edit_id" name="Id">
|
||||
<div class="space-y-4">
|
||||
<!-- Preview image edit -->
|
||||
<div id="preview_container_edit" class="mb-4">
|
||||
<img id="preview_gambar_edit" src="" alt="Preview" class="w-full rounded-lg shadow-md">
|
||||
</div>
|
||||
|
||||
<fieldset class="fieldset">
|
||||
<legend class="fieldset-legend">Gambar</legend>
|
||||
<input type="file" id="edit_gambar" name="Gambar" class="file-input file-input-bordered w-full"
|
||||
accept="image/*">
|
||||
<p class="text-sm text-gray-500 mt-1">Ukuran maksimal 5MB. Kosongkan jika tidak ingin mengubah
|
||||
gambar.</p>
|
||||
</fieldset>
|
||||
<fieldset class="fieldset">
|
||||
<legend class="fieldset-legend">Judul <span class="text-red-500">*</span></legend>
|
||||
<input type="text" id="edit_judul" name="Judul" class="input input-bordered w-full"
|
||||
placeholder="Masukkan judul" required>
|
||||
</fieldset>
|
||||
<fieldset class="fieldset">
|
||||
<legend class="fieldset-legend">Deskripsi <span class="text-red-500">*</span></legend>
|
||||
<textarea id="edit_deskripsi" name="Deskripsi" class="textarea textarea-bordered w-full h-32"
|
||||
placeholder="Masukkan deskripsi" required></textarea>
|
||||
</fieldset>
|
||||
</div>
|
||||
<div class="modal-action mt-6">
|
||||
<button type="button" class="btn rounded-full" onclick="resetFormEdit()">
|
||||
<span class="icon">cancel</span>
|
||||
Tutup
|
||||
</button>
|
||||
<button type="submit" class="btn bg-primary-500 text-white rounded-full hover:bg-primary-700">
|
||||
<span class="icon">save</span>
|
||||
Simpan
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<form method="dialog" class="modal-backdrop">
|
||||
<button onclick="resetFormEdit()">close</button>
|
||||
</form>
|
||||
</dialog>
|
||||
|
||||
<!-- Modal Detail Slider -->
|
||||
<dialog id="modal_detail" class="modal modal-bottom sm:modal-middle">
|
||||
<div class="modal-box w-full max-w-2xl">
|
||||
<button class="btn btn-sm btn-circle absolute right-2 top-2" onclick="modal_detail.close()">✕</button>
|
||||
<h3 class="text-lg font-bold mb-4">Detail Slider</h3>
|
||||
<div class="space-y-4">
|
||||
<div>
|
||||
<img id="detail_gambar" src="" alt="Slider Image" class="w-full rounded-lg shadow-md">
|
||||
</div>
|
||||
<div>
|
||||
<p class="text-sm text-gray-500">Judul</p>
|
||||
<p class="font-semibold text-lg" id="detail_judul">-</p>
|
||||
</div>
|
||||
<div>
|
||||
<p class="text-sm text-gray-500">Deskripsi</p>
|
||||
<p id="detail_deskripsi">-</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-action mt-6">
|
||||
<button type="button" class="btn rounded-full" onclick="modal_detail.close()">
|
||||
<span class="icon">cancel</span>
|
||||
Tutup
|
||||
</button>
|
||||
<button type="button" id="btn_edit_from_detail"
|
||||
class="btn bg-primary-500 text-white rounded-full hover:bg-primary-700">
|
||||
<span class="icon">edit</span>
|
||||
Edit
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<form method="dialog" class="modal-backdrop">
|
||||
<button>close</button>
|
||||
</form>
|
||||
</dialog>
|
||||
|
||||
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
|
||||
<script src="https://cdn.datatables.net/2.3.4/js/dataTables.js"></script>
|
||||
<script src="/plugins/datatables/dataTables.tailwindcss.js"></script>
|
||||
<script type="text/javascript">
|
||||
var table;
|
||||
|
||||
function showToast(title, message) {
|
||||
$('#toastTitle').text(title);
|
||||
$('#toastMessage').text(message);
|
||||
$('#successToast').removeClass('hidden');
|
||||
|
||||
setTimeout(function () {
|
||||
closeToast();
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
function closeToast() {
|
||||
$('#successToast').addClass('hidden');
|
||||
}
|
||||
|
||||
// reset form
|
||||
function resetFormTambah() {
|
||||
$('#form_tambah')[0].reset();
|
||||
$('#preview_container_tambah').addClass('hidden');
|
||||
$('#preview_gambar_tambah').attr('src', '');
|
||||
modal_tambah.close();
|
||||
}
|
||||
|
||||
function resetFormEdit() {
|
||||
$('#form_edit')[0].reset();
|
||||
modal_edit.close();
|
||||
}
|
||||
|
||||
$(document).ready(function () {
|
||||
table = new DataTable('#slider-table', {
|
||||
ajax: '@Url.Action("Table", "SliderUtama")',
|
||||
scrollX: true,
|
||||
autoWidth: false,
|
||||
initComplete: function () {
|
||||
$('div.dt-scroll-body thead').css('visibility', 'collapse');
|
||||
},
|
||||
columnDefs: [
|
||||
{ width: '5%', targets: 0 },
|
||||
{ width: '20%', targets: 1 },
|
||||
{ width: '35%', targets: 2 },
|
||||
{ width: '10%', targets: 3 },
|
||||
{ width: '10%', targets: 4 },
|
||||
{ width: '15%', targets: 5 }
|
||||
],
|
||||
columns: [
|
||||
{ data: 'no', orderable: false, searchable: false },
|
||||
{
|
||||
data: 'judul',
|
||||
render: function (data) {
|
||||
return `<div class="break-words whitespace-normal">${data}</div>`;
|
||||
}
|
||||
},
|
||||
{
|
||||
data: 'deskripsi',
|
||||
render: function (data) {
|
||||
return `<div class="break-words whitespace-normal">${data}</div>`;
|
||||
}
|
||||
},
|
||||
{
|
||||
data: 'gambar',
|
||||
render: function (data) {
|
||||
return `<img src="${data}" alt="Slider" class="w-16 h-16 object-cover rounded">`;
|
||||
}
|
||||
},
|
||||
{ data: 'publish', orderable: false },
|
||||
{ data: 'aksi', orderable: false, searchable: false },
|
||||
]
|
||||
});
|
||||
|
||||
// Preview image
|
||||
$('#input_gambar').on('change', function (e) {
|
||||
const file = e.target.files[0];
|
||||
if (file) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function (e) {
|
||||
$('#preview_gambar_tambah').attr('src', e.target.result);
|
||||
$('#preview_container_tambah').removeClass('hidden');
|
||||
}
|
||||
reader.readAsDataURL(file);
|
||||
} else {
|
||||
$('#preview_container_tambah').addClass('hidden');
|
||||
}
|
||||
});
|
||||
|
||||
// Preview image - edit
|
||||
$('#edit_gambar').on('change', function (e) {
|
||||
const file = e.target.files[0];
|
||||
if (file) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function (e) {
|
||||
$('#preview_gambar_edit').attr('src', e.target.result);
|
||||
}
|
||||
reader.readAsDataURL(file);
|
||||
}
|
||||
});
|
||||
|
||||
// Form Tambah
|
||||
$('#form_tambah').on('submit', function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
let formData = new FormData(this);
|
||||
|
||||
$.ajax({
|
||||
url: '@Url.Action("Create", "SliderUtama")',
|
||||
type: 'POST',
|
||||
data: formData,
|
||||
processData: false,
|
||||
contentType: false,
|
||||
success: function (response) {
|
||||
if (response.success) {
|
||||
showToast('Slide berhasil ditambah!', 'Berhasil! Slide baru telah berhasil ditambahkan.');
|
||||
modal_tambah.close();
|
||||
$('#form_tambah')[0].reset();
|
||||
$('#preview_container_tambah').addClass('hidden');
|
||||
table.ajax.reload();
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
showToast('Gagal!', 'Terjadi kesalahan saat menyimpan data.');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Form Edit
|
||||
$('#form_edit').on('submit', function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
let formData = new FormData(this);
|
||||
|
||||
$.ajax({
|
||||
url: '@Url.Action("Update", "SliderUtama")',
|
||||
type: 'POST',
|
||||
data: formData,
|
||||
processData: false,
|
||||
contentType: false,
|
||||
success: function (response) {
|
||||
if (response.success) {
|
||||
showToast('Slide berhasil diubah!', 'Berhasil! Slide telah berhasil diubah.');
|
||||
modal_edit.close();
|
||||
table.ajax.reload();
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
showToast('Gagal!', 'Terjadi kesalahan saat mengupdate data.');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
table.on("click", ".btn-edit", function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
let rowData = table.row($(this).closest('tr')).data();
|
||||
$('#edit_id').val(rowData.no);
|
||||
$('#edit_judul').val(rowData.judul);
|
||||
$('#edit_deskripsi').val(rowData.deskripsi);
|
||||
$('#preview_gambar_edit').attr('src', rowData.gambar);
|
||||
|
||||
modal_edit.showModal();
|
||||
});
|
||||
|
||||
table.on("click", ".btn-detail", function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
let rowData = table.row($(this).closest('tr')).data();
|
||||
$('#detail_judul').text(rowData.judul);
|
||||
$('#detail_deskripsi').text(rowData.deskripsi);
|
||||
$('#detail_gambar').attr('src', rowData.gambar);
|
||||
|
||||
// Simpan data
|
||||
$('#btn_edit_from_detail').data('rowData', rowData);
|
||||
|
||||
modal_detail.showModal();
|
||||
});
|
||||
|
||||
$('#btn_edit_from_detail').on('click', function () {
|
||||
let rowData = $(this).data('rowData');
|
||||
|
||||
modal_detail.close();
|
||||
|
||||
// default data
|
||||
$('#edit_id').val(rowData.no);
|
||||
$('#edit_judul').val(rowData.judul);
|
||||
$('#edit_deskripsi').val(rowData.deskripsi);
|
||||
$('#preview_gambar_edit').attr('src', rowData.gambar);
|
||||
|
||||
modal_edit.showModal();
|
||||
});
|
||||
|
||||
// Toggle Publish
|
||||
$(document).on('change', '.toggle-publish', function () {
|
||||
let isChecked = $(this).is(':checked');
|
||||
let id = $(this).data('id');
|
||||
|
||||
$.ajax({
|
||||
url: '@Url.Action("TogglePublish", "SliderUtama")',
|
||||
type: 'POST',
|
||||
data: {
|
||||
id: id,
|
||||
isPublished: isChecked
|
||||
},
|
||||
success: function (response) {
|
||||
if (response.success) {
|
||||
showToast('Status berhasil diubah!', 'Berhasil! Status publish telah berhasil diubah.');
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
showToast('Gagal!', 'Terjadi kesalahan saat mengubah status.');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
Loading…
Reference in New Issue