2

Here is the code in my View, I have a dropdown list and basically what i want is that when i click on any item on this list I get the value of the item and it complete the input in the name field.

I managed to do this on JS with a button that load the input but the pb is that it's in Ajax and to be honest I am knew to this and I don't really understand how to make it functional. Should I do it using basic JS ?

<h:outputText value="Name" styleClass="section-label"/>
    <h:inputText id="name" value="#{contextSchemeDetailBean.contextScheme.schemeName}"
                 required="true" maxlength="255"
                 requiredMessage="Please fill out 'Name' field."
                 label="Name" styleClass="section-content input-section">
    <f:validateLength maximum="255"/>
    </h:inputText>
<h:outputText value="Code List" styleClass="section-label"/>
<p:autoComplete id="inputCodeList" required="true"
                requiredMessage="Please fill out 'Code List' field."
                styleClass="section-content input-section"
                completeMethod="#{codeListBean.completeInput}"
                dropdown="true" scrollHeight="220">
<p:ajax event="itemSelect" listener="#{codeListBaseBean.codeList.guid}"/>
</p:autoComplete>

I dont know if I am really clear, if not dont hesitate to ask. Thanks !!

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Sox -
  • 592
  • 1
  • 9
  • 28
  • You tagged your question waaaay to broad. Yes you might be using java and javascript but the 'issue' is not in your javascript code, or in your java code. Hence you get answers that are not really applicable to your case. One question, what do you mean by _" and it complete the input in the name field."_, do you mean it should be appended to what already is in the `inputText` – Kukeltje Jul 02 '18 at 19:05
  • 1
    Yes that is what I meant, not really like an append but more like a clear and a new input. Basically you pick an item in the dropdown list and there is a SQL request that pick the corresponding element (name for example) for this item and complete the input related to this ! – Sox - Jul 02 '18 at 19:35
  • Well, in the listener in the `` you can do that SQL thing and than 'update' the inputText as is basic 'ajax'. See https://stackoverflow.com/questions/25339056/understanding-primefaces-process-update-and-jsf-fajax-execute-render-attributes – Kukeltje Jul 02 '18 at 19:58
  • If questions are answered accept the answer and leave the question as is please – Kukeltje Jul 16 '18 at 07:08

2 Answers2

-1

here is a code you can start with if needed any help just ask :)

 <script type="text/javascript">
      $('#yourid').on('change', function(e){
        console.log(e);
        var mytextbox = e.target.value;
        $.get('/' + mytextbox,function(data) {
          console.log(data);
          $('#yourid').append('<option value="0" disable="true" selected="true">=== Select something ===</option>');
}


    </script>

$(function(){
    $('select').change(function(){
        $that = $(this);
        $('textarea').val(function(){
            return $(this).prop('defaultValue') + ' '+$that.val();
        });
    });
});
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<select>
    <option value="">select one</option>
    <option value="aaaa">aaaa</option>
    <option value="bbbb">bbbb</option>
    <option value="cccc">cccc</option>
    <option value="dddd">dddd</option>
    <option value="eeee">eeee</option>
</select>
<textarea></textarea>
You Can run it from "Run code snippet" button
Justin Imran
  • 69
  • 2
  • 8
  • 1
    I'll go this way, thanks ! I will tell you what I get using this ! Thanks again for your consideration ! – Sox - Jul 02 '18 at 18:43
  • just a little update @Sox- easy use this jquery code this does what you need $(function(){ $('select').change(function(){ $that = $(this); $('textarea').val(function(){ return $(this).prop('defaultValue') + ' '+$that.val(); }); }); }); – Justin Imran Jul 02 '18 at 18:47
  • You ran into not to good tagging by OP. It is not javascript related at all but a JSF issue/question. – Kukeltje Jul 02 '18 at 19:04
-1

It Seems, you are using struts? If so you can use selector tag.

In Struts 2 , you can use the tag to create a HTML drop down box.

<s:select label="What's your favor search engine" 
    headerKey="-1" headerValue="Select Search Engines"
    list="searchEngine" 
    name="yourSearchEngine" />

find full example from here: https://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/