Ajax Primefaces datatable在删除时复制数据

Ajax Primefaces datatable在删除时复制数据,ajax,jsf,jsf-2,primefaces,datatables,Ajax,Jsf,Jsf 2,Primefaces,Datatables,我在使用Hibernate4、Spring4、Lucene3、PrimeFaces5和Java7 我得到了一个数据表,数据填充在烤豆上,这个表的想法是,它向我显示一些未分类的单词,并让我对它们进行分类 例如,初始表的示例看起来很正常 123445 这是我的页面 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.

我在使用Hibernate4、Spring4、Lucene3、PrimeFaces5和Java7

我得到了一个数据表,数据填充在烤豆上,这个表的想法是,它向我显示一些未分类的单词,并让我对它们进行分类

例如,初始表的示例看起来很正常

123445

这是我的页面

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<h:head>

</h:head>

<h:form id="form">
    <p:growl id="msgs" showDetail="true" life="2000" />

    <p:dataTable id="words" var="word"
        value="#{wordCatalogatorController.unknownWords}" editable="true"
        style="margin-bottom:20px">
        <f:facet name="header">Row Editing</f:facet>

        <p:ajax event="rowEdit"
            listener="#{wordCatalogatorController.onRowEdit}" update=":form:msgs" />
        <p:ajax event="rowEditCancel"
            listener="#{wordCatalogatorController.onRowCancel}" update=":form:msgs" />

        <p:column headerText="Palabra">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{word.word}" />
                </f:facet>
                <f:facet name="input">
                    <h:outputText value="#{word.word}" />
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="Tipo Palabra">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{word.wordType}" />
                </f:facet>
                <f:facet name="input">
                    <h:selectOneMenu value="#{word.wordType}" style="width:100%"
                        converter="#{wordTypeConverter}">
                        <f:selectItems value="#{wordCatalogatorController.wordTypes}"
                            var="man" itemLabel="#{man.wordType}" itemValue="#{man}" />
                    </h:selectOneMenu>
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column style="width:32px">
            <p:rowEditor />
        </p:column>
    </p:dataTable>
</h:form>
</html>

行编辑
这方面的bean是:

@Controller
@Transactional
public class WordCatalogatorController {

    private List<Word> unknownWords = new ArrayList<Word>();

    private List<WordType> wordTypes = new ArrayList<WordType>();

    public WordCatalogatorController(){
        //inicializamos palabras desconocidas y tipos de palabras!
        for(int i = 0 ; i < 6 ; i++){
            unknownWords.add(new Word("" + i));
        }

        for(int i = 0 ; i < 4 ; i++){
            wordTypes.add(new WordType("" + i));
        }

    }

    public void onRowEdit(RowEditEvent event) {
        Word currentWord = (Word) event.getObject();

        unknownWords.remove(currentWord);
    }

    public void onRowCancel(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Edit Cancelled",
                ((Word) event.getObject()).getWord());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void onCellEdit(CellEditEvent event) {
        Object oldValue = event.getOldValue();
        Object newValue = event.getNewValue();

        if (newValue != null && !newValue.equals(oldValue)) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Cell Changed", "Old: " + oldValue + ", New:" + newValue);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
@控制器
@交易的
公共类WordCatalogatorController{
private List unknownWords=new ArrayList();
private List wordTypes=new ArrayList();
public WordCatalogatorController(){
//在帕拉布拉斯的圣骑士团!
对于(int i=0;i<6;i++){
添加(新词(“+i”);
}
对于(int i=0;i<4;i++){
添加(新的单词类型(“+i”);
}
}
公共无效onRowEdit(RowEditEvent事件){
Word currentWord=(Word)event.getObject();
未知词。删除(当前词);
}
公共作废onRowCancel(RowEditEvent事件){
FacesMessage msg=新的FacesMessage(“编辑已取消”,
((Word)event.getObject()).getWord());
FacesContext.getCurrentInstance().addMessage(null,msg);
}
public void onCellEdit(CellEditEvent事件){
对象oldValue=event.getOldValue();
Object newValue=event.getNewValue();
if(newValue!=null&!newValue.equals(oldValue)){
FacesMessage msg=新的FacesMessage(FacesMessage.SEVERITY_信息,
“单元格已更改”,“旧的:+oldValue+”,新的:+newValue);
FacesContext.getCurrentInstance().addMessage(null,msg);
}
}
然后在编辑并保存第一行(1)后,datatables将更新为1 2 3 4 5

任何想法都将受到赞赏

下面是pojo类的代码

@Entity
@Table(name="Word")
@Indexed
@AnalyzerDef(name = "searchtokenanalyzer",tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
filters = {
  @TokenFilterDef(factory = StandardFilterFactory.class),
  @TokenFilterDef(factory = LowerCaseFilterFactory.class),
  @TokenFilterDef(factory = StopFilterFactory.class,params = { 
      @Parameter(name = "ignoreCase", value = "true") }) })
      @Analyzer(definition = "searchtokenanalyzer")
public class Word {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long wordId;

    @Column(name="word")
    @Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
    @Analyzer(definition="searchtokenanalyzer")
    private String word;

    @ManyToMany(mappedBy="words")
    private Collection<Danger> dangers = new ArrayList<Danger>();

    @ManyToMany(mappedBy="words")
    private Collection<Risk> risks = new ArrayList<Risk>();

    @ManyToMany(mappedBy="words")
    private Collection<Control> controls = new ArrayList<Control>();

    @ManyToOne
    @JoinColumn(name = "wordTypeId")  
    private WordType wordType;

    public Word(String word, WordType wordType) {
        super();
        this.word = word;
        this.wordType = wordType;
    }

    public Word(String word) {
        super();
        this.word = word;
    }



    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Word){
            return ((Word)obj).getWord().equalsIgnoreCase(this.getWord());
        }else{
            return false;
        }
    }

    public Word() {
        super();

    }

    public long getWordId() {
        return wordId;
    }

    public void setWordId(long wordId) {
        this.wordId = wordId;
    }



@Entity
@Table(name = "WordType")
@Indexed
public class WordType {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long wordTypeId;

    @Column(name = "wordType")
    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
    @Analyzer(definition = "searchtokenanalyzer")
    private String wordType;

    @Column(name = "description")
    private String description;

    @OneToMany(mappedBy = "wordType")
    private Set<Word> words;

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        if (!(obj instanceof WordType)) {
            return false;
        } else {
            WordType extenalWT = (WordType) obj;
            if (this.wordType.equalsIgnoreCase(extenalWT.getWordType())
                    && this.wordTypeId == extenalWT.getWordTypeId()) {
                return true;
            } else {
                return false;
            }
        }
    }

    public WordType() {

    }

    public WordType(String wordType) {
        this.wordType = wordType;
    }

    public long getWordTypeId() {
        return wordTypeId;
    }

    public void setWordTypeId(long wordTypeId) {
        this.wordTypeId = wordTypeId;
    }

    public String getWordType() {
        return wordType;
    }

    public void setWordType(String wordType) {
        this.wordType = wordType;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set<Word> getWords() {
        return words;
    }

    public void setWords(Set<Word> words) {
        this.words = words;
    }

    @Override
    public String toString() {
        return wordType;
    }

}
@实体
@表(name=“Word”)
@索引
@AnalyzerDef(name=“searchtokenanalyzer”,tokenizer=@TokenizerDef(factory=StandardTokenizerFactory.class),
过滤器={
@TokenFilterDef(工厂=StandardFilterFactory.class),
@TokenFilterDef(工厂=LowerCaseFilterFactory.class),
@TokenFilterDef(工厂=StopFilterFactory.class,参数={
@参数(name=“ignoreCase”,value=“true”)})
@Analyzer(定义=“searchtokenanalyzer”)
公共类词{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有长字ID;
@列(name=“word”)
@字段(index=index.YES,analyze=analyze.YES,store=store.NO)
@Analyzer(定义=“searchtokenanalyzer”)
私有字符串字;
@许多(mappedBy=“words”)
私有集合=新的ArrayList();
@许多(mappedBy=“words”)
私人托收风险=新ArrayList();
@许多(mappedBy=“words”)
private Collection controls=new ArrayList();
@许多酮
@JoinColumn(name=“wordTypeId”)
私有字型字型;
公共字(字符串字、字类型字类型){
超级();
这个单词=单词;
this.wordType=wordType;
}
公共字(字符串字){
超级();
这个单词=单词;
}
@凌驾
公共布尔等于(对象obj){
if(单词的obj实例){
return((Word)obj.getWord().equalsIgnoreCase(this.getWord());
}否则{
返回false;
}
}
公共词(){
超级();
}
公共长getWordId(){
返回wordId;
}
public void setWordId(长wordId){
this.wordId=wordId;
}
@实体
@表(name=“WordType”)
@索引
公共类字类型{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有长wordTypeId;
@列(name=“wordType”)
@字段(index=index.YES,analyze=analyze.YES,store=store.NO)
@Analyzer(定义=“searchtokenanalyzer”)
私有字符串字型;
@列(name=“description”)
私有字符串描述;
@OneToMany(mappedBy=“wordType”)
私人固定词;
@凌驾
公共布尔等于(对象obj){
//TODO自动生成的方法存根
if(!(obj instanceof WordType)){
返回false;
}否则{
WordType extendalwt=(WordType)obj;
if(this.wordType.equalsIgnoreCase(extnalwt.getWordType())
&&this.wordTypeId==extnalwt.getWordTypeId()){
返回true;
}否则{
返回false;
}
}
}
公共字类型(){
}
公共字类型(字符串字类型){
this.wordType=wordType;
}
公共长getWordTypeId(){
返回wordTypeId;
}
public void setWordTypeId(长wordTypeId){
this.wordTypeId=wordTypeId;
}
公共字符串getWordType(){
返回字型;
}
公共void setWordType(字符串wordType){
this.wordType=wordType;
}
公共字符串getDescription(){
返回说明;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共集getWords(){
返回单词;
}
公共无效设置字(设置字){
这个单词=单词;
}
@凌驾
公共字符串toString(){
返回字型;
}
}

最后,最好的解决方案是为数据表实现select事件,在事件中有一个对话框来选择选项,然后刷新表,这是最终的xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<h:head>

</h:head>

<h:body>


    <h:form id="myForm">
        <p:growl id="msgs" showDetail="true" life="2000" />


        <p:panel header="Sale Item" style="width: 400px;">

            <h:panelGrid columns="2" cellpadding="5">
                <p:outputLabel value="Texto" for="acSimple" />
                <p:autoComplete id="acSimple"
                    value="#{wordCatalogatorController.texto}"
                    completeMethod="#{wordCatalogatorController.completeText}"
                    binding="#{wordCatalogatorController.autoCompleteText}" />

                <p:commandButton value="Guardar actividad" id="save"
                    update=":myForm:msgs words"
                    binding="#{wordCatalogatorController.saveButton}"
                    actionListener="#{wordCatalogatorController.saveActivity}"
                    styleClass="ui-priority-primary" process="@this" />
            </h:panelGrid>

            <p:dataTable id="words" widgetVar="words" var="word"
                value="#{wordCatalogatorController.unknownWords}" editable="true"
                style="margin-bottom:20px" rowKey="#{word.word}"
                selection="#{wordCatalogatorController.selectedWord}"
                selectionMode="single" editMode="row">

                <p:ajax event="rowSelect"
                    listener="#{wordCatalogatorController.changeClient}"
                    oncomplete="PF('activityDialog').show()"
                    update=":myForm:activityDialog" />

                <f:facet name="header">Edicion de palabras</f:facet>

                <p:column headerText="Palabra">
                    <h:outputText value="#{word.word}" />
                </p:column>

                <p:column headerText="Edicion">
                    <h:outputText value="Presione para catalogar la palabra" />
                </p:column>
            </p:dataTable>

        </p:panel>



        <p:dialog id="activityDialog" width="500px" height="600px"
            header="Palabra a catalogar: #{wordCatalogatorController.selectedWord.word}"
            widgetVar="activityDialog" modal="true" closable="false">

            <h:panelGrid columns="2" cellpadding="5">

                <p:selectOneMenu id="wordTypes"
                    value="#{wordCatalogatorController.selectedWordType}"
                    style="width: 150px;" converter="#{wordTypeConverter}">
                    <p:ajax
                        listener="#{wordCatalogatorController.wordTypeChangeListener}"
                        update=":myForm:words" />
                    <f:selectItem itemLabel="" itemValue="" />
                    <f:selectItems value="#{wordCatalogatorController.wordTypes}" />
                </p:selectOneMenu>
            </h:panelGrid>

        </p:dialog>
    </h:form>
</h:body>
</html>

<p:ajax event="rowEdit" 
        listener="#{beanName.onRowEdit}" 
        update=":growl :messages :formName:tableId"
        oncomplete="PF('tableWidgetVar').filter();" />