Take the id of the selected button

0

I have a form in which I need to know which button I clicked, to get the id , and so with the id delete a row in a BD, the problem I have in how to get the id of the button . I put all the code that influences:

<form class="form-horizontal"  action="Propietario" method="post">
<input type="hidden" name="ajax" value="" />
<input type="hidden" name="accion" value="modRatios" />
<table id="ratios" class="display dataTable no-footer">
    <thead>
        <tr>
            <th><span class="texto" data-i18n="panelcontrol.propietario.precios.horas">...</span></th>
            <th><span class="texto" data-i18n="panelcontrol.propietario.precios.preciomax">...</span></th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <c:forEach items="${sessionScope.propietarioRatios}" var="ratios" varStatus="myIndex">
        <tr>
            <td><input type="text" name="horas${myIndex.index + 1}" value="<c:out value="${ratios.horasRD}" />" style="margin:1em auto;" required></td> 
            <td><input type="text" name="precioMax${myIndex.index + 1}" value="<c:out value="${ratios.descuentoRD}" />" style="margin:1em auto;" required></td>
            <td><button class="btn btnaction btnamarillo" name="accion" value="borrarRatios" id="ratio${ratios.idratio}" type="button"><span class="glyphicon glyphicon-remove"></span></button> </td>//Este es el button en cuestion!!!
        </tr>
        </c:forEach>
    </tbody>
    <tfoot>
        <tr>
            <td><button class="btn btnaction" id="add" type="button"><span class="texto" data-i18n="panelcontrol.propietario.precios.añadir">...</span><span class="glyphicon glyphicon-plus"></span></button></td>
            <td><button class="btn btnaction btnamarillo" type="submit" id="precioRat"><span class="texto" data-i18n="panelcontrol.propietario.precios.guardar">...</span><span class="glyphicon glyphicon-floppy-disk"></span></button></td>
            <td></td>
        </tr>
    </tfoot>
</table>

This is the form, the c:forEach draws rows depending on the data in the database, which can be 1 or several, on the button, the ids, are ratio1 , ratio2 , ratio10 ...

if(vAccion!=null){
            switch(vAccion){
                case "misusos":
                    cargarmisusuos(request, response, base);
                    break;
                case "ocupacion":
                    cargarocupacion(request, response, base);
                    break;
                case "plazas":
                    cargarplazas(request, response, base);
                    break;
                case "plazaactivar":
                    activarplaza(request, response, base);
                    break;
                case"plazasmultiple":
                    activarplazamultiple(request, response, base);
                    break;
                case "facturas":
                    cargarfacturas(request, response, base);
                    break;
                case "generarfacturas":
                    generarfactura(request, response, base);
                    break;
                case "descargarfactura":
                    descargarfactura(request, response, base);
                    respuestapordefecto = false;
                    break;
                case "precios":
                    cargarratios(request, response, base);
                    cargarcomunidades(request, response, base);
                    break;
                case "modCoeficiente":
                    modificarCoeficiente(request, response, base);
                    break;
                case "modRatios":
                    modificarRatios(request, response, base);
                    break;
                case "borrarRatios":
                    borrarRatios(request,response,base);
                    break;
                default:
                    request.setAttribute("errorpanelcontrol", "propietarioaccionnopermitida");
                    break;
            }

        }

private void borrarRatios (HttpServletRequest request, HttpServletResponse response, BaseWeb base){
       //Establecemos la navegacion activa en la sesión
        HttpSession session = request.getSession();
        PanelControlNav pcnav = (PanelControlNav)session.getAttribute("PanelControlNav");
        pcnav.setNavActivo("propietario");
        pcnav.setTabActivo("propietario", "propprecios");
        session.setAttribute("PanelControlNav", pcnav);

        PropietarioWebControler.borrarRatios(request, response, base);
   }

That switch , depends on the button that you press send to one method or another, but of course if I have 10 delete buttons, it will go to the same method, but it does not pass the id or anything.

public static void borrarRatios (HttpServletRequest request, HttpServletResponse response, BaseWeb base){
        HttpSession session = request.getSession();

        RatioDescuentoControlador ratContr = new RatioDescuentoControlador(base.getVariablesGlobales());

        try{

        }catch(Exception ex){
            base.getLoggerGarageScanner().error("Se ha producido un error");
            request.setAttribute("errorpanelcontrol", "propietariocomunidaderrorcarga");
        }
        finally{

        }
    }

And this is the method where I have to delete the rows of the BD. (that's already done)

    
asked by urrutias 19.04.2017 в 12:10
source

2 answers

0

The most used in these cases is a <input type="hidden" name="nombreAccion" value="ratio${ratios.idratio}"> . By doing this you have an invisible field from which you can draw the value, which is equal to the id of the button.

  <c:forEach items="${sessionScope.propietarioRatios}" var="ratios" varStatus="myIndex">
            <tr>
                <td><input type="text" name="horas${myIndex.index + 1}" value="<c:out value="${ratios.horasRD}" />" style="margin:1em auto;" required></td> 
                <td><input type="text" name="precioMax${myIndex.index + 1}" value="<c:out value="${ratios.descuentoRD}" />" style="margin:1em auto;" required></td>
                <td><button class="btn btnaction btnamarillo" name="accion" value="borrarRatios" id="ratio${ratios.idratio}" type="button"><span class="glyphicon glyphicon-remove"></span></button> </td>//Este es el button en cuestion!!!
                <td><input type="hidden" name="nombreAccion" value="ratio${ratios.idratio}"></td>
            </tr>
  </c:forEach>

And you would take the id to be deleted as follows:

String id =  (String)request.getParameter("nombreAccion");
    
answered by 19.04.2017 в 12:52
0

The ID value simply does not exist for the server; the browser does not even send it. So assign the id of the ratio to value of the button, which is a value that will be sent in the request.

<button ... name="accion" value="borrarRatio${ratios.idRatio}" id="ratio${ratios.idratio}" type="submit">

Then you only have to modify your treatment so that, if accion="borrarRatio" + [número] , recover the number. For example, by regexp, or simply by substrings and startsWith , etc.

The other alternative is to put a hidden field and, when you press the button, assign the corresponding value using javascript, but it seems an unnecessary complication.

Of course, in all cases the problem is that what you have written in other fields of other ratios will be lost and will be loaded from the business layer. Consider -when you have a lot more fluency- take a look at the development with Ajax.

    
answered by 19.04.2017 в 12:52