-3


hello all.I'am new to jsf.
Now i know that to set the Managed Bean attributes i can use forms, But i'm using JavaScript in my jsf page.
what i want is to execute the javascript methode onclickAdd() before submitting the form so the user can see what's changed in an anychart map of tunisia along to a table. My jsf page have a dropdown list.

   <h:form>
    Gouvernorats &#160; Temperature (C°) <br/>
     <h:selectOneMenu id="list" value="#{GouvBean.name}" >
         <f:selectItem itemValue="Tunis" itemLabel="Tunis" />
         <f:selectItem itemValue="Médenine" itemLabel="Medenine"/>
         <f:selectItem itemValue="Gabès" itemLabel="Gabès"/>
         <f:selectItem itemValue="Sfax" itemLabel="Sfax"/>
         <f:selectItem itemValue="Sidi Bou Zid" itemLabel="Sidi Bou Zid"/>
         <f:selectItem itemValue="Kassérine" itemLabel="Kassérine"/>
         <f:selectItem itemValue="Mahdia" itemLabel="Mahdia"/>
         <f:selectItem itemValue="Sousse" itemLabel="Sousse"/>
         <f:selectItem itemValue="Monastir" itemLabel="Monastir"/>
         <f:selectItem itemValue="Tataouine" itemLabel="Tataouine"/>
         <f:selectItem itemValue="Kebili" itemLabel="Kebili"/>
         <f:selectItem itemValue="Tozeur" itemLabel="Tozeur"/>
         <f:selectItem itemValue="Bèja" itemLabel="Bèja"/>
         <f:selectItem itemValue="Jendouba" itemLabel="Jendouba"/>
         <f:selectItem itemValue="Bizerte" itemLabel="Bizerte"/>
         <f:selectItem itemValue="Manubah" itemLabel="Manubah"/>
         <f:selectItem itemValue="Ben Arous" itemLabel="Ben Arous"/>
         <f:selectItem itemValue="Zaghouan" itemLabel="Zaghouan"/>
         <f:selectItem itemValue="Siliana" itemLabel="Siliana"/>
         <f:selectItem itemValue="Le Kef" itemLabel="Le Kef"/>
         <f:selectItem itemValue="Gafsa" itemLabel="Gafsa"/>
         <f:selectItem itemValue="Kairaouan" itemLabel="Kairaouan"/>
         <f:selectItem itemValue="Nabeul" itemLabel="Nabeul"/> 


     </h:selectOneMenu>

     <h:inputText id="Temperature" value="#{GouvBean.temperature}" ><f:ajax listener="#{GouvBean.inchange}"/></h:inputText>


     <h:commandButton value="+"onclick="onClickAdd();" >
        <f:ajax execute="@form"/>
    </h:commandButton>
  </h:form>

The javascript method changes a table and an AnyChart map:
this is the table:

     <table id="myTable">
         <thead><tr><th>Gouvernorats</th><th>Temperature</th>   </tr></thead>
         <tbody></tbody>

     </table>

and this is JavaScript method:

     //Define a JavaScript method
        function onClickAdd(){

            //saving the value of the list
         var Gov = document.getElementById("list");
         var Gid=Gov.options[Gov.selectedIndex];
         //deleting  the current valye(added value) from the list
                Gov.remove(Gov.selectedIndex)
         var C°=document.getElementById('Temperature');

            //Getting refrence to tbody
            var tbodyRef=document.getElementById("myTable").getElementsByTagName("tbody")[0];
            //appending a row to tbody
            var row=tbodyRef.insertRow(tbodyRef.rows.length);

            var cell1=row.insertCell(0);
            var cell2=row.insertCell(1);
            var cell3=row.insertCell(2);

            //Creation of delete button
            var del=document.createElement("BUTTON");
            var t=document.createTextNode("-");

            //Giving value to the cells 
            cell1.innerHTML=Gid.value;
            cell2.innerHTML=C°.value;
            cell3.appendChild(del);



            //adding a listener to the button   
            del.appendChild(t);
            //OnAddChangeValue(cell1.innerHTML,cell2.innerHTML);




            del.addEventListener("click",function(){

            //1)knowing the row where the button is(index)
            var x=del.parentNode.parentNode.rowIndex;
                x=x-1;

            //2)moving the value of the first cell back to the list
            var l=document.getElementById("list");

              //create option node
            var node=document.createElement("option");

              //create attribute value
            var attr1=document.createAttribute("value");

               //creation of label
            var label=document.createTextNode(tbodyRef.rows[x].cells[0].innerHTML);

                 //setting a value to the attribute
                 attr1.value=tbodyRef.rows[x].cells[0].innerHTML;

                  //linking...
                  node.setAttributeNode(attr1);
                  node.appendChild(label);
                  l.appendChild(node);
            //3)deleting the row from the tbody

                    tbodyRef.deleteRow(x);

            //  OnDeleteChangeValue(cell1.innerHTML);

            });

            }

     </script>

and this is h:head

<h:head><title>Hello world-JSF-Input Form</title>

</h:head>

help me please

NB: IT WORKS WHEN I ADD h:form before but in this case javascript dont work !

Ahmed Haddad
  • 73
  • 1
  • 8
  • 1
    I don't see any javascript – Kukeltje Jan 01 '18 at 11:02
  • The javascript method is "onClickAdd" in the attribute "onclick" of **h:commandButton**. I didn't post the javascript code – Ahmed Haddad Jan 02 '18 at 05:27
  • Sure, but it could as well have been named 'bla'... that would have been as useful as 'onClickAdd'... And a form 'required'... Please create a [mcve]. I think you lack some basic jsf knowledge. – Kukeltje Jan 02 '18 at 08:33
  • You want me to post the javascript script ? to be complete ? – Ahmed Haddad Jan 02 '18 at 10:02
  • I want you to post a [mcve]... – Kukeltje Jan 02 '18 at 10:08
  • Verifiable=> then i need to post the hole jsf page is that permitted ? – Ahmed Haddad Jan 02 '18 at 10:25
  • There is also a 'minimal' part in there... Start stripping... It helps narrowing down the problem and getting things clear. Also read https://stackoverflow.com/tags/jsf/info – Kukeltje Jan 02 '18 at 10:29
  • This is not a [mcve]. `h:head` missing in the xhtml, really no `h:form`? And if you change the content of the javascript function to `console.log('hi')` do the main parts work (besides your `table` not being updated). You can still narrow things down. And you seem on the one hand want to use jsf and on the other hand you don't... kind of a mixed solution here. – Kukeltje Jan 02 '18 at 10:54
  • **h:head ** isn't important it contains **the title** and some **style**. the problem is with **h:form** if i add it every thing works but javascript mehod dosen't work. if i remove the h:form the javascript method works. if i change the code to just an "alert("hello world")" and i keep **h:form** everything works fine ! – Ahmed Haddad Jan 02 '18 at 11:11
  • Ok, good luck.... if you know things better, then solve them yourself. But before that, read https://stackoverflow.com/questions/13452551/unable-to-understand-hhead-behaviour/ (the 4 upvoted answer, not the accepted one) – Kukeltje Jan 02 '18 at 11:13
  • no help me please i'm beginning with jsf – Ahmed Haddad Jan 02 '18 at 11:27
  • Ok, but then please try to make a real [mcve]. It is not that difficult. Start by putting all (x)html in one file, instead of code snippets. Are the style and css things in the `h:head` relevant? Relevant being that if you remove them it starts working? Most likely not so remove them (that is making it minimal). If just calling javascript is a problem when a form is present (I would think it's needed), than the whole `` thing can be removed as is the content of the `onClickAdd` and just have it log something. Are all the `f:selectItem` tags relevant or does it fail it too when using 2
    – Kukeltje Jan 02 '18 at 11:43
  • And what is your JSF version? This because it might be better to use new annotations for the managedbean and scope. The ones you use are deprecated in newer jsf versions. And in _"NB: IT WORKS WHEN I ADD h:form before but in this case javascript dont work !"_, the 'don't work' part should be specified... – Kukeltje Jan 02 '18 at 11:46
  • i'm using **jsf 2.2**. the hole javascript method don't work when i add **h:form**, but to notice when i change the hole code of the methode to just an "alert("hello")". every thing works right. – Ahmed Haddad Jan 02 '18 at 11:53
  • This is still not a [mcve]... Read my previous comments again... And if changing the content of the javascript to just an alert makes it work, you have a pure javascript problem, nothing jsf related. That is also why making a [mcve] helps narrowing down problems. And JSF on a servlet container or a java-ee server? Read https://stackoverflow.com/questions/11986847/java-ee-6-javax-annotation-managedbean-vs-javax-inject-named-vs-javax-faces about the annotations – Kukeltje Jan 02 '18 at 12:00
  • 1)The application server i use is **Tomcat ** integreted on **eclipse**. 2) you said that 's a javascript problem ! how ? if i remove **h:form** the method works fine. – Ahmed Haddad Jan 02 '18 at 12:18
  • If you remove the form, the commandButton most likely is not submitting anything and the onclick works in that the result of what is done, is visible client-side. If the form is added, the whole page is refreshed and the result of your javascript is (most likely) lost. But since you do not have a [mcve], it is hard to tell and keep in mind that we are doing all this support in our free (non-paid) time and that I almost spend 1 hour already on your Q (for which customers are usually billed €125 per hour with a minimum of 4 hours)... so all the [mcve] work you do, makes it easier for us. – Kukeltje Jan 02 '18 at 12:31
  • ok ! i will keep in mind . just that i've exams tomorow and this week i'm not focusing 100%. i will try to make the question more clear in few days ..thanks for your help – Ahmed Haddad Jan 02 '18 at 12:40
  • You last edit even made it **less** an [mcve]... Spending time upfront making it a [mcve] helps getting answers more quickly. Good luck with your exams – Kukeltje Jan 02 '18 at 12:44
  • **"If the form is added, the whole page is refreshed and the result of your javascript is (most likely) lost"**.for that my question was since the beginning is there a way to send the Managedbeans without forms. – Ahmed Haddad Jan 02 '18 at 12:45
  • After your edit this text: _"what i want is to execute the javascript methode **onclickAdd()** before submitting the form so the user can see what's changed in an anychart map of tunisia along to a table."_ sort of indicates you do not want to submit anything, want the user to have a sort of 'preview' and then choose e.g. 'ok' somewhere and then do the submit. Well, then implement it that way. Read all the links in my answer, all the info is in there. The whole complexity in the javascript function is irrelevant for this question, the many selects are irrelevant. Make it into a [mcve] – Kukeltje Jan 04 '18 at 19:26

2 Answers2

0

You use ajax in the h:selectOneMenu and h:inputText that sends the values to the server/beans already if they are in an h:form (as you found out). But instead of asking how do I send data without a form (which makes the commandButton 'work'), the question should have been

How do I prevent an h:commandButton from refreshing the page.

For which the answer is given here:

But since you do not seem to need (or even want) to send anything to the server on clicking the (command)Button, a plain non submitting html button with an onClick would be sufficient. This can be achieved by adding type="button" to the h:commandButton (or by using the plain html counterpart which is legal too)

Some more general knowledge:

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • no,i want to send beans to the ManagedBean class, but before that i want to execute the javascript methode before. i have changed my **h:commandButton** as you suggested using **f:ajax**. but the problem isn't resolved again. the javascript method dosen't work before submitting the form. – Ahmed Haddad Jan 04 '18 at 15:49
  • Then your question was not clear (again it was **NOT** a [mcve]) And with making this answer I already spend waaaaay to much time on it... Good luck... – Kukeltje Jan 04 '18 at 15:55
  • i m really sorry . i appreciate your help – Ahmed Haddad Jan 04 '18 at 16:03
0

After debugging the page in google chrome browser , i found that i need to:

1) include<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.15/proj4.js"/>

2) i realized that the id value in jsf is changed in html :

if an id of an element is "list" in jsf it becomes "j_idt6:list" in html.
so in getElmentbyid i need to do like this: document.getElementById("j_idt6:list") instead of document.getElementById("list")


now everything work fine.
Ahmed Haddad
  • 73
  • 1
  • 8
  • Great it works for you but.... 1: is completely, utterly not explainable by your original question. 2: is only related to the pure javascript part and not the more generic problem. So the relation between the Q and A is sort of not understandable for anyone but you... and your XHTML code is still sort of not good (or in line with what you described your intentions were...)... next time [mcve], [mcve], [mcve] please – Kukeltje Jan 05 '18 at 11:57