Ace
Ace

Reputation: 867

readOnly attribute not being set with JavaScript function in Struts 2

I am trying to create an edit link, such as, when it is clicked, it opens the details for that row in read-only mode.

Here is the link:

<c:set var="deletableBook" value="0"/>

<a href="" title="Edit Book Info" onClick='resetDateAndMakeReadOnly(${deletableBook}); return performAction(${item.bookId}, "bookEdit");'>Edit</a>

And here's the function that gets called:

function resetDateAndMakeReadOnly(allEditable) {      
 var e22 = document.getElementById('book_Date');
 var e3 = document.getElementById('book_type');
 var e4 = document.getElementById('book_Number');
 if (allEditable){
     e22.readOnly=false;
     e3.disabled=false;
     e4.readOnly=false;
     alert("read and write");
 } else {
     e22.readOnly=true;
     e3.disabled=true;
     e4.readOnly=true;
     alert("readOnly new");
 }
 e22.value = "<c:out value='${params.book_Date}'/>";
 return false;    }

And currently nothing seems to change when this method is run. I've confirmed that it makes it to the correct part of the logic, but things are still editable.

Upvotes: 2

Views: 882

Answers (2)

Roman C
Roman C

Reputation: 1

The attribute deletableBook is not a boolean value, and is not false that you expect in the javascript function. To switch the variable in the action

session.put("allEditable", !(session.get("allEditable")==null?Boolean.FALSE:(Boolean)session.get("allEditable")));

then use

$(document).ready(function(){
    resetDateAndMakeReadOnly(<s:property value="%{#session.allEditable}"/>);
});

that will reset fields attributes depending on allEditable when the page is reloaded. But this

<s:a href="" title="Edit Book Info" onClick="resetDateAndMakeReadOnly(%{#session.allEditable});">Make readonly</s:a>

will not reload the page and keep the value that you have in the allEditable session attribute. That syntax may be a little bit confuse the IDE but is correctly evaluate the OGNL expression and renders like

<a title="Edit Book Info" onClick="resetDateAndMakeReadOnly(false);">Make readonly</a>

there's no href attribute, that's why the page is not reloaded.

Also elements in the JSP should be findable by their id attribute should be set.

Upvotes: 0

Aleksandr M
Aleksandr M

Reputation: 24396

It is because you are using link with empty href to trigger your javascript function which will reload your page. Use javascript:void(0); inside href.

<a href="javascript:void(0);" title="Edit Book Info" onClick='resetDateAndMakeReadOnly(${deletableBook}); return performAction(${item.bookId}, "bookEdit");'>Edit</a>

Upvotes: 2

Related Questions