Удаление только одной строки в таблице - SpringBoot & Java & Thymeleaf - PullRequest
0 голосов
/ 04 октября 2018

Новое в Spring-boot, Java и Thymeleaf ... Попытка сделать так, чтобы кнопка корзины удаляла только одну строку в этой таблице.Прямо сейчас, если нажата какая-либо мусорная кнопка, все строки таблицы будут удалены.Я запустил отладчик, и мой контроллер выбирает rowId для любой нажатой кнопки, поэтому не уверен, почему он удаляет все строки, а не только одну.Есть идеи?

enter image description here

//code that loads form and table (table is made up of Ams360Policies)
  @GetMapping("/directBind")
  public String getDirectBind(Model model){
        List<String> businessAgencies = new ArrayList<String>();
        businessAgencies.add("Personal");
        businessAgencies.add("Commercial");
        businessAgencies.add("Life");
        businessAgencies.add("Benefits");
        businessAgencies.add("Health");
        businessAgencies.add("Non P and C");
        model.addAttribute("businessAgencies", businessAgencies);

        DirectBind directBind = new DirectBind();

        List<Ams360Policy> ams360Policies = new ArrayList();
        Ams360Policy ams360Policy = new Ams360Policy();
        ams360Policies.add(ams360Policy);
        model.addAttribute("ams360Policies", ams360Policy);

        List<String> billTypeList = new ArrayList<String>();
        billTypeList.add("Direct Bill");
        billTypeList.add("Agency Bill");
        model.addAttribute("billTypeList", billTypeList);
        ams360Policy.setBillTypeOptions(billTypeList);

        List<String> businessAgencyList = new ArrayList<String>();
        directBind.setBusinessAgencyList(businessAgencyList);

        model.addAttribute("directBind", directBind);

        return "directBind";
    }


//code to add a Row to table

    @RequestMapping(value="/directBind", params="addPolicy")
    public String addPolicy(final DirectBind directBind, Model model){
        List<Ams360Policy> ams360Policies =  directBind.getAms360Policies();
        Ams360Policy ams360Policy = new Ams360Policy();
        ams360Policies.add(ams360Policy);
        model.addAttribute("ams360Policies", ams360Policies);


        List<String> billTypeList = new ArrayList<String>();
        billTypeList.add("Direct Bill");
        billTypeList.add("Agency Bill");
        model.addAttribute("billTypeList", billTypeList);
        ams360Policy.setBillTypeOptions(billTypeList);

        List<String> businessAgencyList = new ArrayList<String>();
        directBind.setBusinessAgencyList(businessAgencyList);

        return "directBind";
    }

//code to Remove row of table

@RequestMapping(value = "/directBind", params="removeRow")
public String removeRow(final DirectBind directBind, final HttpServletRequest req, Model model){
    final Integer rowId = Integer.valueOf(req.getParameter("removeRow"));
    List<Ams360Policy> ams360Policies =  directBind.getAms360Policies();
    model.addAttribute("ams360Policies", ams360Policies);
    directBind.setAms360Policies(ams360Policies);

    Ams360Policy ams360Policy = new Ams360Policy();
    List<String> billTypeList = new ArrayList<String>();
    billTypeList.add("Direct Bill");
    billTypeList.add("Agency Bill");
    model.addAttribute("billTypeList", billTypeList);
    ams360Policy.setBillTypeOptions(billTypeList);

    List<String> businessAgencyList = new ArrayList<String>();
    directBind.setBusinessAgencyList(businessAgencyList);

    directBind.getAms360Policies().remove(1);
    model.addAttribute("directBind", directBind);
    return "directBind";
}

//html code for table 
<div>
  <h4 style="display: inline;">AMS360 Policy Setup</h4>
  <input type="submit" formnovalidate="formnovalidate"  name="addPolicy" class="btn btn-default" style="margin-left: 1rem; margin-bottom: 1rem;" value="+"></input>
        </div>
        <div class="col-sm-12">
            <hr/>

            <table class="table table-striped AMSTable" data-classes="table-no-bordered" data-striped="true" data-show-columns="true" data-pagination="true">
                <thead>
                <tr>
                    <th>Policy Number</th>
                    <th>Policy Term Start Date</th>
                    <th>Policy Term End Date</th>
                    <th>Line of Coverage</th>
                    <th>Parent Company</th>
                    <th>Writing Company</th>
                    <th>Bill Type</th>
                    <th>Quote Premium</th>
                    <th>Commission</th>
                </tr>
                </thead>
                <tbody>
                <tr id="newPolicyRow" th:each="ams360Policy, stat : ${ams360Policies}">
                    <td> <input type="text" class="form-control" th:field="*{ams360Policies[__${stat.index}__].policyNumber}"/></td>
                    <td> <input type="text" class="form-control" th:field="*{ams360Policies[__${stat.index}__].policyTermDateStart}"/></td>
                    <td> <input type="text" class="form-control" th:field="*{ams360Policies[__${stat.index}__].policyTermDateEnd}"/></td>
                    <td> <input type="text" class="form-control" th:field="*{ams360Policies[__${stat.index}__].lineOfCoverage}"/></td>
                    <td> <input type="text" class="form-control" th:field="*{ams360Policies[__${stat.index}__].parentCompany}"/></td>
                    <td> <input type="text" class="form-control" th:field="*{ams360Policies[__${stat.index}__].writingCompany}"/></td>
                    <td id="billTypeCell">
                        <div  th:each="billType : ${billTypeList}">
                            <input type="checkbox" th:field="*{ams360Policies[__${stat.index}__].billTypeOptions}" th:value="${billType}"/>
                            <label th:text="${billType}" id="billTypeLabel"></label>
                        </div>
                    </td>
                    <td> <input type="text" class="form-control" th:field="*{ams360Policies[__${stat.index}__].quotePremium}"/></td>
                    <td> <input type="text" class="form-control" th:field="*{ams360Policies[__${stat.index}__].commission}"/></td>
                    <td class="text-right"> <button type="submit" name="removeRow" th:value="${stat.index}" class="btn btn-danger" ><span class="fa fa-trash"></span></button></td>
                </tr>
                </tbody>
            </table>
</div>

Когда я отлаживаю, я получаю следующее ... enter image description here

...