How to dynamically add Primefaces inputfields with JQuery

后端 未结 1 1221
死守一世寂寞
死守一世寂寞 2021-01-23 06:31

I am trying to dynamically add Labels and textfields from Primefaces to my webpage. I want to use JQuery. Until now I realize the same task with Primefaces only and it works qui

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-23 07:14

    Looks like you completely missed the point of JSF and are rather new to web development. JSF is a server side language/framework which produces HTML code (rightclick page in browser and do View Source, do you see it now? no single JSF tag, it's one and all HTML). jQuery is a client side language which works with HTML DOM tree only. The is not a known HTML element. The browser don't have any idea how to deal with it. It only knows HTML like and friends.

    You need to salvage the functional requirement in a "JSF-ish" way. Here's a kickoff example:

    
        
            
            
            
            

    with

    @ManagedBean
    @ViewScoped
    public class Bean implements Serializable {
    
        private List items;
    
        @PostConstruct
        public void init() {
            items = new ArrayList<>();
        }
    
        public void add() {
            Item item = new Item();
            item.setLabel("label" + items.size());
            items.add(item);
        }
    
        public void remove(Item item) {
            items.remove(item);
        }
    
        public List getItems() {
            return items;
        }
    
    }
    

    and

    public class Item {
    
        private String label;
        private String value;
    
        // Let your IDE generate getters/setters/equals/hashCode.
    }
    

    That's all. No JavaScript mess necessary and everything is ready for save in server side by just adding another command button pointing to a save() method which passes the items variable further to the EJB/service/DAO class.

    Otherwise, if you really want to do it the jQuery way, then you should drop JSF altogether and go for something request-based like Spring MVC. You only need to keep in mind that you've to write down all that HTML/CSS/JS yourself.

    See also:

    • What is the need of JSF, when UI can be achieved from CSS, HTML, JavaScript, jQuery?

    0 讨论(0)
提交回复
热议问题