java - Add checked object's id to list passed to model as attribute - Stack Overflow

I need a help with dynamic checkbox list handling. I want to add id of item to list of ids (class name

I need a help with dynamic checkbox list handling. I want to add id of item to list of ids (class name SelectedIds and instance name selectedIds in code) that I passed to my template (its name is laptopIndexPage.html) from index page of Spring controller handler function by clicking on checkbox that located near by each object.

Spring controller code to handle index page (LaptopController.java):

@Controller
@RequestMapping("/manage-laptops")
public class LaptopController 
{
    @Autowired
    public LaptopService laptopService;

    @Autowired
    private DatabaseLaptopService dbLaptopService;
    // private List<String> idList = new ArrayList<>();
    @GetMapping(path = "/home")
    public String handleIndexPage(@RequestParam(required = false) Map<String, Object> allParams, Model model)
    {
        List<Laptop> laptops = new ArrayList<>();
        Pageable pageable = null;
        Page<Laptop> chunk = null;
        int pageSize = 5;
        if (!allParams.isEmpty())
        {

            if (allParams.containsKey("filter_by"))
            {
                
            }

            if (allParams.containsKey("page") && allParams.containsKey("size"))
            {
                String page = (String)allParams.get("page");
                String size = (String)allParams.get("size");

                pageable = PageRequest.of(Integer.parseInt(page) - 1, Integer.parseInt(size));
                chunk = dbLaptopService.getAllLaptops(pageable);
                laptops = chunk.getContent();
            }
            else 
            {
                pageable = PageRequest.of(0, 5);
                chunk = dbLaptopService.getAllLaptops(pageable);
                laptops = chunk.getContent();
            }

            
            HashSet<Laptop> unique = new HashSet<>();
            List<Laptop> tmp = laptops;
            unique.addAll(laptops);
            laptops = new ArrayList<>(tmp);
            laptops.clear();
            laptops.addAll(unique);
        }
        else 
        {
            pageable = PageRequest.of(0, 5);
            chunk = dbLaptopService.getAllLaptops(pageable);
            laptops = chunk.getContent();
        }
        
        

        model.addAttribute("currentPage", chunk.getNumber() + 1);
        model.addAttribute("totalItems", chunk.getTotalElements());
        model.addAttribute("totalPages", chunk.getTotalPages());
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("laptop", new Laptop());
        // model.addAttribute("idList", idList);
        SelectedIds selectedIds = new SelectedIds();
        model.addAttribute("selectedIds", selectedIds);
        model.addAttribute("laptops", laptops);
        
        return "laptopHomePage";
    }
}

HTML page:

<!DOCTYPE html>
<html lang="en" xmlns:th=";>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Device Form</title>
    <link rel="stylesheet" th:ref="@{/css/styles.css}">
    <link rel="stylesheet" href=".5.2/css/bootstrap.min.css">
    <style>
        body {
            background-color: #f8f9fa;
            padding: 30px;
        }

        .form-container {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            padding: 20px;
        }
    </style>
</head>

<body>

    <div class="container mt-5">
        <div class="form-container p-4 border rounded shadow">
            <h2 class="text-center mb-4">Device Information Form</h2>
            <form th:action="@{add}" method="post" th:object="${laptop}">
                <div class="form-group">
                    <label for="modelName">Model Name</label>
                    <input type="text" id="modelName" class="form-control" th:field="*{modelName}">
                    <p class="text-danger" th:if="${#fields.hasErrors('modelName')}" th:errors="*{modelName}"></p>
                </div>
    
                <div class="form-group">
                    <label for="description">Description</label>
                    <textarea id="description" class="form-control" th:field="*{description}" rows="4" ></textarea>
                    <p class="text-danger" th:if="${#fields.hasErrors('description')}" th:errors="*{description}"></p>
                </div>
    
                <div class="form-group">
                    <label for="ramCapacity">RAM Capacity (GB)</label>
                    <input type="number" id="ramCapacity" class="form-control" th:field="*{ramCapacity}" min="1" >
                    <p class="text-danger" th:if="${#fields.hasErrors('ramCapacity')}" th:errors="*{ramCapacity}"></p>
                </div>
    
                <div class="form-group">
                    <label for="diskCapacity">Disk Capacity (GB)</label>
                    <input type="number" id="diskCapacity" class="form-control" th:field="*{diskCapacity}" min="1" >
                    <p class="text-danger" th:if="${#fields.hasErrors('diskCapacity')}" th:errors="*{diskCapacity}"></p>
                </div>
    
                <button type="submit" class="btn btn-primary btn-block">Submit</button>
            </form>
        </div>
    </div>
    

    

    <div class="container">
        <br>
        <h1 th:text="|Total laptops: ${totalItems}"></h1>

            <div>
                <input type="text" id="model_name_filter">
                <input type="text" id="model_ram_filter">
                <button type="button" onclick="doFilter()">
                    Filter
                </button>
            </div>

            <form th:action="@{delete-selected}" method="post" id="form_del" th:object="${selectedIds}">
                <input th:field="*{ids}" hidden th:value="${selectedIds.ids}"></input>
                <input type="button" value="Delete selected" th:attr="onclick=|alert(${selectedIds.ids.size})|">
            </form>

            
        <table class="table">
            <thead>
                <tr>
                    <th>Select</th>
                    <th>Model Name</th>
                    <th>Model Description</th>
                    <th>Ram</th>
                    <th>Disk capacity</th>
                </tr>
            </thead>
            <tbody>
                <tr th:each="laptop, stat : ${laptops}">
                        <td>
                            <input type="checkbox" th:name="${selectedIds.ids}" th:value="${laptop.id}">
                        </td>
                        <form th:action="@{update}" method="post" th:object="${laptop}">
                            
                            <td hidden><input type="number" name="id" th:value="${laptop.id}"></td>
                            <td>
                                <input type="text" th:value="${laptop.modelName}" th:name="modelName">
                                <p class="text-danger" th:if="${#fields.hasErrors('modelName')}" th:errors="*{modelName}"></p>
                            </td>
                            <td>
                                <input type="text" th:value="${laptop.description}" th:name="description">
                                <p class="text-danger" th:if="${#fields.hasErrors('description')}" th:errors="*{description}"></p>
                            </td>
                            <td>
                                <input type="number"   th:value="${laptop.ramCapacity}" th:name="ramCapacity">
                                <p class="text-danger" th:if="${#fields.hasErrors('ramCapacity')}" th:errors="*{ramCapacity}"></p>   
                            </td>
                            <td>
                                <input type="number"  th:value="${laptop.diskCapacity}" th:name="diskCapacity"></td>
                                <p class="text-danger" th:if="${#fields.hasErrors('diskCapacity')}" th:errors="*{diskCapacity}"></p>
                                <td>
                            <input type="submit" value="update">
                        </form>
                    </td>
                    <td>
                        <form method="post"  th:with="rel='delete/' + ${laptop.id}"  th:action="${rel}">
                            <input type="submit" value="delete">
                        </form>
                    </td>
                </tr>
            
            </tbody>
        </table>
    </div>

When I select some elements on the page and do post on following form:

<form th:action="@{delete-selected}" method="post" id="form_del" th:object="${selectedIds}">
                <input th:field="*{ids}" hidden th:value="${selectedIds.ids}"></input>
                <input type="submit" value="Delete selected">
            </form>

and see the size of id array in console printed by this function:

@PostMapping(path = "/delete-selected")
public String deleteSelected(@ModelAttribute("selectedIds") SelectedIds selectedIds, Model model)
{
    if (selectedIds != null)
    {
        System.out.println("Selected: " + selectedIds.getIds().size());
    }   
    return "redirect:home";
}

I get:

Selected: 0

So, how to add values to list selectedIds.ids passed by me? Is it impossible?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745654545a4638465.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信