0
votes

I have a problem with checking the select/grails select tag value via grails if tag. My code looks like this, html:

<select id="select1" name="select1">
    <option value=2>2</option>
    <option value=3>3</option>
</select>

or grails select:

<g:select id="select1" name="select1" from="${[2, 3] }" value="2"/>

and the grails if:

...
<g:each var="rowNumber" in = "${0..5}">
    <g:if test="${rowNumber} < ${select1.value}"> <!-- or ${select1.val()} or select1.value --> 
        ...
    </g:if>
</g:each>...

Code throws an NullPointerException, and says that select1 is a null object and he cannot evaluate method val(), or cannot get attribute value. Anyone have an idea what I should do to fix this problem? Thanks for help!

EDIT

Inside my if statement I have a render template, and when I change the value of select I want to render this templates again, but with saving what I have already type there (e.g if I have a textfield in my template).

EDIT2

I messed up a little bit. I want to create a dynamic table, e.g at start it could have 2 rows & columns, then I want to be able to enlarge/decreasenumber of rows/columns (e.g. by clicking button/buttons) of course by clicking the button, I want to save already filled table in ajax, then render table with new number of rows/columns, and fill earlier filled cell with their previous values (new cells will be empty). e.g. filled table 2x2

a a
a a

when I enlarge this table to 3x2 I want the table looks like this:

a a
a a
_ _

where _ is an empty cell.

1
You cannot access select1 in your GSP since it runs on the server side and select1 actually lives in a browser. Means your approach is wrong. But if you could explain what you are trying to achieve we maybe can provide you a working approach. - saw303
#saw303 I want to dynamically check if the actual selected value is greater than my variable in foreach loop, and depends on the result render views. I tried use js function instead but it tolds me that the method is undefined. Here is my code: ... <g:if test="${checkRows(rowNumber) }"> ... js: function checkRows(rowNumber) { var elem = document.getElementById('rowsNumberSelect').val(); if(rowNumber < elem) { return true; } return false; } - LEEL
Where do you want to perform this check? On the client-side (the browser) or on the server-side (your Grails controller)? Currently you mix Javascript (client-side) with tag libraries (server-side)? - saw303

1 Answers

0
votes

Since you want to work on the client side you need to work with Javascript only.

function checkRows(rowNumber) 
{ 
    var value= $('#select1').val(); 
    return rowNumber < elem;
}

If you want to trigger the function when the select value changes use

$(document).ready(function () { 
   $('#select1').change(function() {
       checkRows(34 /* use your value here */);
   }); 
});