9 Replies Latest reply on Dec 2, 2015 5:11 AM by Michal Petrov

    RichFaces CDK component rerender

    Ivan Ivanov Newbie

      Hi.

       

      I'm trying to create my own component.

      I want to add some kind of buttons or links to my component, which will fire its decode renderer method (for do some logic) and then rerender my component with new data from decode.

      For example, I have <a> tag into my component with onclick="RichFaces.ajax(...)", where first parameter is ID of my custom component.

      When I click to this link, decode calls, but component do not rerender.

       

      How can I force it to rerender after decode by clicking this link? (I can't use something from a4j, because I need to generate this links dynamically).

       

      Thank you.

        • 1. Re: RichFaces CDK component rerender
          Michal Petrov Master

          In order to rerender something the component that triggered the ajax request has to have @render set up, which in your case isn't possible since <a> is not a component, why can't you use a commandButton?

          • 2. Re: RichFaces CDK component rerender
            Ivan Ivanov Newbie

            Im not talking about xhtml, just talking about custom components with custom Java-renderer, where I have encode method with ResponseWriter, where I write writer.startElement("a", ..);

            Look at this, for example: http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=extendedDataTable&sample=edt-sorting&skin=blueSky

            Here we have <a> tags in header, when you click this <a> tags, table sorting and render new order, but I didnt understant how it works inside even with sources, thats why I started this question, I need to do something like this in my custom component.

            • 3. Re: RichFaces CDK component rerender
              Michal Petrov Master

              You need to understand how JSF/RichFaces work - the information about what actions to execute or what components to rerender is obtained from the components (that is Java objects representing the components), not from the request - you cannot set up rerendering in the HTML you're generating, it has to be set up on the component.

               

              In the case of the dataTable the column renderer is called and in turn calls the renderers of its children, one of them being a4j:commandLink (with @render="table") which generates the <a> element.

              • 4. Re: RichFaces CDK component rerender
                Ivan Ivanov Newbie

                So where (class/method) should I add a4j:commandLink to my component if I want to add it dynamically?

                For example, where is ExtendedDataTable adding this to header of columns? I cant find, can you link to source?

                • 5. Re: RichFaces CDK component rerender
                  Michal Petrov Master

                  The source is in the showcase, right under the table. (And we're also on GitHub)

                   

                  <rich:column sortBy="#{car.vendor}"
                               sortOrder="#{carsSortingBean.sortsOrders['vendor']}"
                               sortType="custom">
                      <f:facet name="header">
                          <h:panelGrid columns="2">
                              <a4j:commandLink execute="@this" value="Vendor" render="table"
                                               action="#{carsSortingBean.sort}">
                                  <f:param name="sortProperty" value="vendor"/>
                              </a4j:commandLink>
                            …
                  
                  • 6. Re: RichFaces CDK component rerender
                    Ivan Ivanov Newbie

                    Ah, ok. Sorry, then I chosed the wrong example. I want to embed that into my component and generate this links dynamically for each column (for example), how can I do this?

                    Need this behavior by default, not only when user used a4j:commandLink into my component, you know?

                    • 7. Re: RichFaces CDK component rerender
                      Michal Petrov Master

                      Again you just need to set @render on the component (and pass its id to the RichFaces.ajax method) But wouldn't it be easier to just create a composite component? Provide the basic renderer for the component you're creating and then combine it with commandLinks in the composite.

                      • 8. Re: RichFaces CDK component rerender
                        Ivan Ivanov Newbie

                        Ok, nothing is working still..

                         

                        I tried to create UICommandLink dynamically, it was created, but don't work with render or execute.

                        I tried it on ExtendedDataTable, at doEncodeBegin method (I asked before, where should I create this component dynamically, but didnt get answer, so I chosed this method, I cant see any alternatives).

                         

                        For each column of table I wrote this code:

                        UICommandLink link = (UICommandLink) context.getApplication().createComponent(context, UICommandLink.COMPONENT_TYPE, "org.richfaces.CommandLinkRenderer");
                        link.setValue("Some link");
                        link.setRender(component.getId()); //component here is our table
                        link.setExecute(component.getId()); //component here is our table
                        //link.setParent(column); //also tried this, but dont work, a lot of exceptions about "parent not null but not properly set"
                        
                        column.getChildren().add(link); //column is UIColumn of table
                        
                        

                         

                        So this code generates for me links in each table column:

                        <a href="#" id="tableForm:tableContent:j_id2" name="tableForm:tableContent:j_id2" onclick="RichFaces.ajax("tableForm:tableContent:j_id2",event,{"incId":"1"} );return false;">Some link</a>
                        
                        

                        tableContent here - right id of my table.

                         

                        And when I click it, nothing happens (no rerender of table, no execute (decode)), just ajax-spinner appears for a second.

                         

                        What did I wrong, maybe add link to column on wrong stage (too late)?

                        • 9. Re: RichFaces CDK component rerender
                          Michal Petrov Master

                          You don't have to call setParent() (and you're not supposed to), JSF will handle it when it's processing the component. Your method will however add another link to the column every time you rerender it (if you'd put the link into xhtml you wouldn't need to check). On the other hand the rendering should work, what does the server response look like?