Selias
Selias

Reputation: 57

2D Array forEach table getting coordinates JTSL, JAVASCRIPT

I got a String[][] which is my playing table (coded Battleship). I display it like this:

<%
    String[][] field2 = master.getField(2);
    request.setAttribute("field", field2);
%>
<div class=feldbeschreibung>Gegnerisches Feld</div><div class=feldbeschreibung2>Dein Feld</div>
   <table class=player_field2>
     <tbody>
       <c:forEach items="${field}" var="row">
          <tr>              
            <c:forEach items="${row}" var="item">
              <td class="tdBox" onclick="attack(this, x, y)">
                <span>${item}</span>
              </td>         
            </c:forEach>
          </tr>
        </c:forEach>
     </tbody>
   </table>

This is how it looks

What I do want is to let my method attack() <-javascript on what coordinates I'm clicking. I can call the method itself like this: but I'm missing on which coordinates I'm clicking. Btw I'm pretty new to Java, Javascript, JSP and JTSL (all this is used).

Thanks in advance :-)

Upvotes: 0

Views: 100

Answers (1)

rickz
rickz

Reputation: 4474

Here is demonstration code.

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<c:set var="filler"  value="x,x,x,x,x,x,x,x,x,x"/> 
<c:set var="field" value="${[filler,filler,filler,filler,filler,filler,filler,filler,filler,filler]}"/> 
<html>
    <body>
        <table>
        <tbody>
            <c:forEach items="${field}" var="row" varStatus="xStatus">
                <tr>                
                    <c:forEach items="${row}" var="item" varStatus="yStatus">
                        <td style="border: 1px solid black;">${xStatus.count}:${yStatus.count}</td>       
                    </c:forEach>
                </tr>
            </c:forEach>
        </tbody>
        </table>
    </body> 
</html>

Upvotes: 0

Related Questions