Search code examples
javascriptthymeleaf

How can i use for loop in javascript using thymeleaf?


I am using thymeleaf so when I run this application ,it gives me an error in (for(int i=0;i<10;i++) It means I have to respect the syntaxe of thymeleaf.My question is howa can I write this script using thymeleaf.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handing Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
        <script type="text/javascript" src="jquery-1.11.3.js"></script>

</head>
<body>
    <h1>Result</h1>
    <p th:text="'columns_number: ' + ${db.columns_number}" />
    <h3>Création de la base de données</h3>
    <p>Table_name: <input type="text" th:field="${db.table_Name}" /></p>
<table id='tablona' border='1px'>
        <tr>
        <th>field</th>
                <th>Size</th>
                <th>Type</th>
                <th>null</th>
                </tr>


</table>

<script th:inline="javascript">
    /*<![CDATA[*/
    $( document ).ready(function() {

        for(int i=0;i<[[${T(Integer).parseInt(db.columns_number)}]];i++)
      {
        $('<tr>'+
        '<td><input id="field" type="text" name="field'+i+'"  maxlength="255"  required="required"/></td>'+
        '<td><input id="Size" type="text"  name="Size'+i+'"  maxlength="255" required="required"/></td>'+
        '<td><SELECT id="Type" name="Type'+i+'">'+
'<OPTION VALUE="varchar">varchar</OPTION>'+
'<OPTION VALUE="int">int</OPTION>'+
'<OPTION VALUE="text">long</OPTION>'+
'<OPTION VALUE="float">float</OPTION>'+
'<OPTION VALUE="double">double</OPTION>'+
'<OPTION VALUE="Date">Date</OPTION>'+
'<OPTION VALUE="Time">Time</OPTION>'+
'<OPTION VALUE="Year">Year</OPTION>'+
'<OPTION VALUE="Real">Real</OPTION>'+
'<OPTION VALUE="Boolean">Boolean</OPTION>'+
'<OPTION VALUE="longText">longText</OPTION>'+
'<OPTION VALUE="Binary">Binary</OPTION>'+
'</SELECT></td>'+
'<td><SELECT id="null" name="nullabilite'+i+'">'+
'<OPTION VALUE="null">null</OPTION>'+
'<OPTION VALUE="not_null">not_null</OPTION>'+
'</SELECT></td>'+
    '</tr>').appendTo($("#tablona")).html()

    }
    });
    /*]]>*/
</script>
      <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>    

</body>
</html> 

Solution

  • You should wrap your script in this structure :

    <script th:inline="javascript">
        /*<![CDATA[*/
        $( document ).ready(function() {
            for(i=0;i<10;i++) {
                ...
            }
        });
        /*]]>*/
    </script>
    

    EDIT :

    Don't forget to store your javascript and other static files in the /src/main/webapp folder of your spring-boot project

    EDIT2 :

    You can do directly your script with thymeleaf :

    <tr th:each="i : ${#numbers.sequence( 1, db.columns_number)}">
        <td><input id="field" th:name="${'field'+i}" maxlength="255"
            required="required" type="text" /></td>
        <td><input id="Size" th:name="${'Size'+i}" maxlength="255"
            required="required" type="text" /></td>
        <td><select id="Type" th:name="${'Type'+i}">
                 ...
            </select></td>
        <td><select id="null" th:name="${'nullabilite'+i}">
                <option value="null">null</option>
                <option value="not_null">not_null</option>
        </select></td>
    </tr>