cndavy

导航

 
View Code
自己目前开发的项目中使用的错误类型词条编辑的功能,使用 datatable行编辑
  1 <h:form id="typeform">
  2          <p:growl id="msge" showDetail="true"/>
  3        <p:outputPanel id ="paneltype" >
  4         <p:dataTable id="tabletype" var="tp"
  5                      value="#{typeProblemAction.typeofproblemList}" resizableColumns="true" emptyMessage="没有记录"
  6                          editable="true"   rows="10"         dynamic ="true"
  7                          onRowEditUpdate="msge"
  8                          paginator="true"
  9                          scrollable="true"
 10                          selection="#{typeProblemAction.selectType}"
 11                          selectionMode="single" rowKey="#{tp.id}"
 12 
 13 
 14                          paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
 15                          rowsPerPageTemplate="20,50,100"
 16                     >           <p:ajax listener="#{typeProblemAction.rowEdit}" event="rowEdit" update=":typeform:msge"/>
 17             <p:ajax event="rowSelect" update =":typeform:tabletype :typeform:paneltype" listener="#{typeProblemAction.onSelectRecord}"/>
 18             <p:column id="selectline" selectionMode="single">
 19                 <p:cellEditor>
 20                 <f:facet name="output">
 21                     <h:outputText value="选择">
 22                     </h:outputText>
 23                 </f:facet>
 24                     <f:facet name="input">
 25                         <h:outputText value="选择">
 26                         </h:outputText>
 27                     </f:facet>
 28                 </p:cellEditor>
 29             </p:column>
 30                 <p:column style="width:80px" sortBy="#{tp.id}"
 31                           headerText="编号"
 32                           filterBy="#{tp.id}" filterMatchMode="startsWith">
 33 
 34                     <p:cellEditor>
 35                         <f:facet name="output">
 36                             <h:outputText value="#{tp.id}"/>
 37                         </f:facet>
 38                         <f:facet name="input">
 39                             <p:inputText value="#{tp.id}" />
 40                         </f:facet>
 41                     </p:cellEditor>
 42 
 43 
 44                 </p:column>
 45                 <p:column style="width:80px" headerText="简介"
 46                           filterBy="#{tp.message}"
 47                           filterMatchMode="contains">
 48 
 49                     <p:cellEditor>
 50                         <f:facet name="output">
 51                             <h:outputText value="#{tp.message}"/>
 52                         </f:facet>
 53                         <f:facet name="input">
 54                             <p:inputText value="#{tp.message}"/>
 55                         </f:facet>
 56                     </p:cellEditor>
 57 
 58                 </p:column>
 59 
 60 
 61                 <p:column headerText="错误类型" style="width:25px">
 62                     <p:cellEditor>
 63                         <f:facet name="output">
 64                             <h:outputText value="#{tp.status}"/>
 65                         </f:facet>
 66                         <f:facet name="input">
 67                             <h:selectOneMenu value="#{tp.status}">
 68                                 <f:selectItem   itemLabel="激活"    itemValue="Y"/>
 69                                 <f:selectItem  itemLabel="失效"     itemValue="N"/>
 70                             </h:selectOneMenu>
 71                         </f:facet>
 72                     </p:cellEditor>
 73                 </p:column>
 74 
 75                 <p:column headerText="编辑行" style="width:50px">
 76                     <p:rowEditor>
 77 
 78                     </p:rowEditor>
 79 
 80                 </p:column>
 81 
 82             </p:dataTable>
 83 
 84             <p:commandButton value="添加"      rendered="#{not empty typeProblemAction.selectType}"
 85                              update=":typeform" actionListener="#{typeProblemAction.addNewLine}">
 86             </p:commandButton>
 87             <p:commandButton value="删除"    immediate="true"    rendered="#{not empty typeProblemAction.selectType}"
 88                              onclick="cd.show()"  >
 89             </p:commandButton>
 90                 <p:confirmDialog message="要删除该纪录吗?"
 91                                  header="删除纪录" severity="alert"
 92                                  widgetVar="cd">
 93                     <p:commandButton value="确认" immediate="true" actionListener="#{typeProblemAction.deleteLine}"
 94                                      update=":typeform:paneltype :typeform:msge" oncomplete="cd.hide()"/>
 95                     <p:commandButton value="取消" onclick="cd.hide();" type="button" />
 96                 </p:confirmDialog>
 97 
 98 
 99              </p:outputPanel>
100 
101         </h:form>
102         </div>
 
 
View Code
 1 public void rowEdit(RowEditEvent ev) {
 2       Typeofproblem obj = null;
 3      try {
 4             obj = (Typeofproblem) ev.getObject();  obj.setLastdate(new java.util.Date());
 5            typeProblemService.updateTypeProblem(obj);
 6             FacesContext.getCurrentInstance().addMessage(nullnew FacesMessage(FacesMessage.SEVERITY_WARN, "编辑保存", obj.toString()));
 7            } catch (Exception ex) {
 8                FacesContext.getCurrentInstance().addMessage(nullnew FacesMessage(FacesMessage.SEVERITY_ERROR, "Error !", ex.toString()));
 9                  FacesContext.getCurrentInstance().validationFailed();
10               }
11        }
12    public  String addNewLine(){
13        try{
14            Typeofproblem tp =new Typeofproblem();
15            tp.setStatus("N");
16            tp.setLastdate(new java.util.Date());
17           if (this.selectType!=null) {
18               tp.setId(this.selectType.getId()+".00");
19           }
20          if( typeProblemService.insertTypeProblem(tp)==1)
21           MessageUtil.addInfo("生成新纪录");
22           // typeofproblemList=typeProblemService.selectTypeList();
23            typeofproblemList.add(typeofproblemList.indexOf(selectType)+1 ,tp);
24           // selectType=tp;
25        }
26        catch (Exception e){
27            MessageUtil.addError("生成新纪录出错" + e.getMessage());
28        }
29        return null
30    }
31     public  String deleteLine(){
32         try{
33 
34             if (this.selectType!=null) {
35                 int i =0;
36                 for(Typeofproblem t :typeofproblemList){
37                     if (t.getId().startsWith(selectType.getId())){
38                          i++;
39                     }
40                 }
41                 if (i>1){ //存在下级
42                     MessageUtil.addError(selectType.getId()+ "存在下级纪录!");
43                     return  null;
44                 }
45                 typeProblemService.deleteTypeProblem(selectType);
46                 //typeofproblemList=typeProblemService.selectTypeList();
47                 typeofproblemList.remove(selectType);
48 
49                 logger.debug(selectType.getId()+" delete"); selectType=null;
50             }
51 
52         }
53         catch (Exception e){
54             MessageUtil.addError("删除出错" + e.getMessage());
55         }
56         return null;
57     }
58     public String onSelectRecord( SelectEvent event) {
59         Typeofproblem tp= (Typeofproblem) event.getObject();
60     return null;
61     }
posted on 2012-05-03 09:30  cndavy  阅读(797)  评论(0编辑  收藏  举报