raffles document uploader added / Free Resource Completed
This commit is contained in:
@@ -2,87 +2,113 @@
|
||||
<div class="row">
|
||||
<div class="col-sm-12">
|
||||
|
||||
<form method="POST" action="{{ route('documents.dropzone.upload') }}" class="dropzone" id="mainForm"
|
||||
enctype="multipart/form-data">
|
||||
@csrf
|
||||
{{ html()->form('POST', route('documents.upload'))->class(['needs-validation'])->attributes(['novalidate', 'enctype' => 'multipart/form-data', 'id' => 'documentForm'])->open() }}
|
||||
|
||||
<div class="mb-3">
|
||||
<div class="mb-3">
|
||||
{{ html()->label('Title')->for('title') }}
|
||||
{{ html()->span('*')->class('text-danger') }}
|
||||
{{ html()->text('title')->id('docTitle')->class('form-control')->placeholder('Enter Title')->required() }}
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
{{ html()->label('Select Model')->class('form-label')->for('model') }}
|
||||
{{ html()->label('Select Model')->class('form-label')->for('model') }}
|
||||
{{ html()->span('*')->class('text-danger') }}
|
||||
{{ html()->select('model')->id('modelSelect')->class('form-select')->required()->options(['' => '-- Select --'] + $modelOptions->toArray()) }}
|
||||
{{ html()->select('model')->id('modelSelect')->class('form-select select2')->required()->options(['' => '-- Select --'] + $modelOptions->toArray()) }}
|
||||
</div>
|
||||
|
||||
<div class="dropzone-previews mb-3"></div>
|
||||
|
||||
<div class="dz-message mb-3">
|
||||
<p class="fs-14">Drop files here or click to upload.</p>
|
||||
<div class="mb-3">
|
||||
<x-document::file-upload :uploadUrl="route('documents.store')" dropzoneId="document-dropzone"
|
||||
formId="documentForm" message="Upload Document (PDF or Images Only)"
|
||||
inputName="document" />
|
||||
</div>
|
||||
|
||||
<button type="button" class="btn btn-primary" id="submitAll">Submit</button>
|
||||
</form>
|
||||
<div class="col-sm-12">
|
||||
<div class="hstack justify-content-end gap-2">
|
||||
<button type="submit" class="btn btn-primary upload-btn">Upload</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{ html()->form()->close() }}
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@push('js')
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", function() {
|
||||
Dropzone.autoDiscover = false;
|
||||
|
||||
@pushOnce('js')
|
||||
<script>
|
||||
function clearDropzone(dropzoneIds = [], formId = '', fileFieldNames = []) {
|
||||
dropzoneIds.forEach(function(id) {
|
||||
const dropzoneElement = Dropzone.forElement(`#${id}`);
|
||||
if (dropzoneElement) {
|
||||
dropzoneElement.removeAllFiles(true);
|
||||
}
|
||||
});
|
||||
|
||||
const myDropzone = new Dropzone("#mainForm", {
|
||||
url: "{{ route('documents.dropzone.upload') }}",
|
||||
autoProcessQueue: false,
|
||||
uploadMultiple: true,
|
||||
parallelUploads: 5,
|
||||
maxFilesize: 5,
|
||||
addRemoveLinks: true,
|
||||
acceptedFiles: ".pdf,.doc,.docx,.jpg,.png",
|
||||
paramName: "file[]",
|
||||
headers: {
|
||||
'X-CSRF-TOKEN': document.querySelector('input[name="_token"]').value
|
||||
},
|
||||
init: function() {
|
||||
const dz = this;
|
||||
fileFieldNames.forEach(function(field) {
|
||||
$(`#${formId} input[name="${field}[]"]`).remove();
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
document.getElementById("submitAll").addEventListener("click", function(e) {
|
||||
const title = document.getElementById('docTitle').value;
|
||||
const model = document.getElementById('modelSelect').value;
|
||||
<script>
|
||||
$(document).on('submit', '#documentForm', function(event) {
|
||||
event.preventDefault();
|
||||
|
||||
if (!title || !model) {
|
||||
alert("Please fill in both Title and Model.");
|
||||
return;
|
||||
}
|
||||
const url = $(this).attr('action');
|
||||
const formData = new FormData(this);
|
||||
const button = $(this).find('button[type="submit"]');
|
||||
|
||||
if (dz.getQueuedFiles().length > 0) {
|
||||
dz.processQueue();
|
||||
} else {
|
||||
alert("Please select at least one file.");
|
||||
}
|
||||
});
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: url,
|
||||
data: formData,
|
||||
processData: false,
|
||||
contentType: false,
|
||||
headers: {
|
||||
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
|
||||
},
|
||||
beforeSend: () => {
|
||||
button.text('Uploading...').prop('disabled', true);
|
||||
},
|
||||
success: (response) => {
|
||||
if (response.status == true) {
|
||||
$('#documentForm')[0].reset();
|
||||
|
||||
dz.on("sending", function(file, xhr, formData) {
|
||||
formData.append("title", document.getElementById('docTitle').value);
|
||||
formData.append("model", document.getElementById('modelSelect').value);
|
||||
});
|
||||
// if (DataTable.isDataTable("#documents-table")) {
|
||||
// $("#documents-table").DataTable().ajax.reload();
|
||||
// } else {
|
||||
// target.html(response.view);
|
||||
// }
|
||||
|
||||
dz.on("successmultiple", function(files, response) {
|
||||
alert("Files uploaded successfully.");
|
||||
dz.removeAllFiles();
|
||||
document.getElementById('mainForm').reset();
|
||||
});
|
||||
clearDropzone(dropzoneIds = ["document-dropzone"],
|
||||
"documentForm", ["document"]);
|
||||
|
||||
dz.on("errormultiple", function(files, response) {
|
||||
alert("An error occurred during upload.");
|
||||
console.error(response);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
@endpush
|
||||
button.text('Upload').prop('disabled', false);
|
||||
toastr.success(response.msg);
|
||||
|
||||
}
|
||||
},
|
||||
error: (xhr) => {
|
||||
if (xhr.status === 422) {
|
||||
const errors = xhr.responseJSON.errors;
|
||||
$('.error-message').remove();
|
||||
|
||||
for (const [key, value] of Object.entries(errors)) {
|
||||
const errorMessage = $(
|
||||
'<p class="error-message text-danger mt-2"></p>').text(
|
||||
value[0]);
|
||||
$(`#${key}`).after(errorMessage);
|
||||
}
|
||||
} else {
|
||||
console.error(xhr);
|
||||
}
|
||||
},
|
||||
complete: () => {
|
||||
button.text('Upload');
|
||||
button.prop('disabled', true);
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
@endPushOnce
|
||||
|
Reference in New Issue
Block a user