anishcana / jovial

SpringAjaxModule
0 stars 0 forks source link

Ajax #8

Open anishcana opened 6 months ago

anishcana commented 6 months ago

// Assume this is the data received from the backend var responseData = [ { name: "File 1", directory: "/path/to/directory1" }, { name: "File 2", directory: "/path/to/directory2" }, // Add more data as needed ];

// Function to populate the table with data function populateTable(data) { var tbody = $('#dataTable tbody'); tbody.empty(); // Clear existing table body content

// Loop through the data and create table rows
$.each(data, function(index, item) {
    var row = $('<tr>');
    row.append($('<td>').text(item.name));

    // Create a hyperlink for the directory column
    var directoryLink = $('<a>').attr('href', 'file://' + item.directory).text(item.directory);
    var directoryCell = $('<td>').append(directoryLink);
    row.append(directoryCell);

    tbody.append(row);
});

}

// Call the function to populate the table with the received data populateTable(responseData);

anishcana commented 6 months ago

<!DOCTYPE html>

Upload Excel File

Upload Excel File

anishcana commented 6 months ago

<!DOCTYPE html>

Upload Excel File

Upload Excel File

anishcana commented 6 months ago

import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile;

@RestController public class FileUploadController {

@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file) {
    if (file.isEmpty()) {
        return "Please select a file to upload.";
    }
    // Process the file (e.g., save to disk, parse Excel)
    // You can use Apache POI or similar libraries to handle Excel files
    return "File uploaded successfully.";
}

}

anishcana commented 6 months ago

// upload.js $(document).ready(function() { $("#uploadForm").submit(function(event) { event.preventDefault(); var formData = new FormData($(this)[0]); uploadFile(formData); }); });

function uploadFile(formData) { $.ajax({ url: "/upload", type: "POST", data: formData, contentType: false, processData: false, success: function(response) { // Handle success console.log("File uploaded successfully:", response); }, error: function(xhr, status, error) { // Handle error console.error("Error uploading file:", error); } }); }

anishcana commented 6 months ago

load Excel File

anishcana commented 6 months ago

Upload Excel File

<form id="uploadForm" enctype="multipart/form-data">
    <input type="file" name="file">
    <button type="submit">Upload</button>
anishcana commented 6 months ago

// upload.js $(document).ready(function() { $("#uploadButton").click(function() { $("#fileInput").click(); });

$("#uploadForm").submit(function(event) {
    event.preventDefault();
    var formData = new FormData($(this)[0]);
    uploadFile(formData);
});

});

anishcana commented 6 months ago

<!DOCTYPE html>

Upload Excel File

Upload Excel File

anishcana commented 6 months ago
javax.validation validation-api
anishcana commented 6 months ago

ependency>

javax.validation
<artifactId>validation-api</artifactId>

</dependenc

anishcana commented 6 months ago

import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController;

@RestController public class YourController {

@PostMapping("/createRecord")
public ResponseEntity<String> createRecord(@RequestBody YourDTO formData) {
    // Code to save the formData to the database
    // You can use your service layer to handle this
    // Return appropriate response
    return ResponseEntity.ok("New record created successfully!");
}

}

anishcana commented 6 months ago

import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController;

@RestController public class YourController {

@PostMapping("/editRecord")
public ResponseEntity<String> editRecord(@RequestBody YourDTO formData) {
    // Code to update the existing record in the database
    // You can use your service layer to handle this
    // Return appropriate response
    return ResponseEntity.ok("Record edited successfully!");
}

}

anishcana commented 6 months ago

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html>

Form

Create or Edit Record




anishcana commented 6 months ago

<!DOCTYPE html>

Dropdown Example

Dropdown Example

anishcana commented 6 months ago

import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController;

import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream;

@RestController public class ZipController {

@Value("${source.file.path}")
private String sourceFilePath;

@Value("${destination.zip.path}")
private String destinationZipPath;

@PostMapping("/zip")
public void zipFiles() throws IOException {
    FileOutputStream fos = new FileOutputStream(destinationZipPath);
    ZipOutputStream zos = new ZipOutputStream(fos);
    zipFile(sourceFilePath, sourceFilePath, zos);
    zos.close();
}

private void zipFile(String fileToZip, String fileName, ZipOutputStream zos) throws IOException {
    File file = new File(fileToZip);
    FileInputStream fis = new FileInputStream(file);
    ZipEntry zipEntry = new ZipEntry(fileName);
    zos.putNextEntry(zipEntry);
    byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
        zos.write(bytes, 0, length);
    }
    zos.closeEntry();
    fis.close();
}

}

source.file.path=/path/to/your/source/file destination.zip.path=/path/to/your/destination/zipfile.zip

anishcana commented 6 months ago

<!DOCTYPE html>

Zip Files