anishcana / jovial

SpringAjaxModule
0 stars 0 forks source link

Pagination #5

Open anishcana opened 3 months ago

anishcana commented 3 months ago

// TableController.java package com.example.demo;

import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable;

@Controller public class TableController {

@GetMapping("/table")
public String showTable(Model model) {
    // Here you would retrieve your table data from a service or repository
    // For simplicity, I'll create a dummy record
    Record record = new Record();
    record.setId(1);
    record.setName("John Doe");
    record.setAge(30);

    model.addAttribute("record", record);
    return "table";
}

@GetMapping("/next/{id}")
public String showNextScreen(@PathVariable int id, Model model) {
    // Here you would retrieve the record based on the ID from a service or repository
    // For simplicity, I'll create a dummy record again
    Record record = new Record();
    record.setId(id);
    record.setName("John Doe");
    record.setAge(30);

    model.addAttribute("record", record);
    return "nextScreen";
}

}

anishcana commented 3 months ago

<!DOCTYPE html>

Clickable Table
Click Here
anishcana commented 3 months ago

<!DOCTYPE html>

Next Screen

Record Details

ID:

Name:

Age:

anishcana commented 3 months ago

<!DOCTYPE html>

Next Screen

Record Details

ID:

Name:

Age:

anishcana commented 3 months ago
<h1>Record Details</h1>
<p>ID: <span th:text="${record.id}"></span></p>
<p>Name: <span th:text="${record.name}"></span></p>
<p>Age: <span th:text="${record.age}"></span></p>
anishcana commented 3 months ago

{ if (id != null) { Record record = recordService.getRecordById(id); if (record != null) { model.addAttribute("record", record); return "nextScreen"; }

anishcana commented 3 months ago

private final RecordService recordService;

@Autowired
public TableController(RecordService recordService) {
    this.recordService = recordService;
}
anishcana commented 3 months ago

// RecordService.java package com.example.demo;

import org.springframework.stereotype.Service;

import java.util.ArrayList; import java.util.List;

@Service public class RecordService {

// Dummy data for demonstration
private final List<Record> records = new ArrayList<>();

public RecordService() {
    // Initialize dummy records
    records.add(new Record(1, "John Doe", 30));
    records.add(new Record(2, "Jane Smith", 25));
    // Add more records as needed
}

public List<Record> getAllRecords() {
    return records;
}

public Record getRecordById(int id) {
    for (Record record : records) {
        if (record.getId() == id) {
            return record;
        }
    }
    return null; // Return null if record with given ID is not found
}

}

anishcana commented 3 months ago
        <td><a th:href="@{/next(id=${record.id})}">Click Here</a></td>