一個list中包含不同的type時如何遍歷?頁面如何接收?Jquery調用後臺方法?

//接口定義

public interface SystemDataService {
public List<SystemData> loadAll();

void create(SystemData systemData);

void update(SystemData systemData);

void deleteById(Long classroomId,Long optBy);

SystemData findById(Long id);

}

//controller

public String listSurvey(Model model) {

List<SystemData> sysDatas=sysDataService.loadAll();
dealDataToModel(sysDatas,model);
return "view/application/feedback";
}


//模態框

   <div class="modal fade in" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"  style="display: none;" data-backdrop="static"> 
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
                 <button type="button" class="close" οnclick="" data-dismiss="modal"><span aria-hidden="true">×</span></button>
                 <h4 class="modal-title" id="myModalLabel1">Create New Kind</h4>
</div>


<div class="modal-body" id="modal-body">
    <div class="row">
        <input type="hidden" id="id"/>  //隱藏id
        <div class="form-horizontal" method="post" role="form" id="queryForm" style="margin-left: 50px">
            <div class="form-group">
                                   <label for="rmName" class=" col-sm-4 control-label ">A directory</label>
   <div class="col-sm-6">
     <select class="form-control" name="type" id="modelType">
         <option value="VISIT_PROCESS">學院參觀申請及整體流程安排滿意度</option>
         <option value="VISIT_CONTENT">學院參觀的內容的滿意度</option>
         <option value="VISIT_LOGISTICS">酒店、用餐、及接送服務滿意度</option>
     </select>
            </div>
            </div>
            
            <div class="form-group">
                   <label for="" class=" pull-left col-sm-4 control-label ">Second-level directory</label>
   <div class="col-sm-6">
          <input type="text" class=" pull-left form-control" id="value" name="value" >
      </div>
            </div>
            
 
            <div class="form-group">
                                     <div class="col-xs-2 pull-right">
                                        
                                     <a class="btn btn-primary" id="create" οnclick="add()">Create</a>
                                     <a class="btn btn-primary" id="update" style="display: none;"  οnclick="update()">Confirm</a>
                                 </div>
                            </div>
        </div>
     </div>
</div>
<div class="modal-footer">
             
</div>                 
</div>
</div>
</div>

                 //根據業務的需求需要把所展示的內容分成三個類別
      private void dealDataToModel(List<SystemData> sysDatas, Model model) {
Map<String,List<SystemData>> mappingmap=new HashMap<String, List<SystemData>>();
List<SystemData> processs=new ArrayList<SystemData>();
List<SystemData> contents=new ArrayList<SystemData>();
List<SystemData> logistics=new ArrayList<SystemData>();
mappingmap.put("VISIT_PROCESS", processs);
mappingmap.put("VISIT_CONTENT", contents);
mappingmap.put("VISIT_LOGISTICS", logistics);


             //使systemData根據不同的類別進行遍歷
for (SystemData data:sysDatas){
String type=data.getType();
if(mappingmap.containsKey(type)){
List<SystemData> temp=mappingmap.get(type);
temp.add(data);
}
}

model.addAttribute("v_process",processs);
model.addAttribute("v_content",contents);
model.addAttribute("v_logistics",logistics);
}


              @RequestMapping(value="/feedback/create")
      @ResponseBody
     public ResultResponse create(SystemData systemData){
ResultResponse rr=new ResultResponse();
try{
systemData.setCreateUser(this.getLoginUser().to(UserInfo.class).getId());
systemData.setCreateTime(getDate());
this.sysDataService.create(systemData);
rr.setResult(true);
}catch(Exception e){
rr.setMessage("save failed");
}
return rr;
       }

@RequestMapping(value="/feedback/update/{id}")
@ResponseBody
public ResultResponse update( @PathVariable Long id ,SystemData sd){
ResultResponse rr=new ResultResponse();
try{
sd.setCreateUser(this.getLoginUser().to(UserInfo.class).getId());
sd.setCreateTime(getDate());
sd.setUpdateTime(getDate());
sd.setUpdateTime(getDate());
sd.setCreateTime(getDate());
sysDataService.update(sd);
rr.setResult(true);
}catch(Exception e){
rr.setMessage("update failed");
}
return rr;
}


@RequestMapping(value="/feedback/delete",method=RequestMethod.POST)
@RequireLogin
@ResponseBody
public ResultResponse delete(Long Id){
ResultResponse rr=new ResultResponse();
try{
Long userId=getLoginUser().to(UserInfo.class).getId();
this.sysDataService.deleteById(Id, userId);
rr.setResult(true);
}catch(Exception e){
rr.setMessage("delete failed");
rr.setResult(false);
}

return rr;
}


//前臺內容接受

<c:if test="${not empty v_content}">
   <tr  class="text-center" colspan="3">
                      <td  rowspan="${v_content.size()+1 }">學院參觀的內容的滿意度</td>
     </tr>
     <c:forEach items="${v_content }" varStatus="vs" var="content">
               <tr  class="text-center">
                             <td οnclick="showModal('VISIT_CONTENT','${content.value }','${content.id }')" ><a > ${content.value }</a></td>
                                        <td >
                                               <button class="btn btn-danger" οnclick="del('${content.id}')">
                            <spring:message code="admin.common.button.delete"/>
             </button>
                                          </td>
                                  </tr>
   /c:forEach>
</c:if> 


//jquery調用後臺的方法

<jsp:include page="/view/common/scripts.jsp" />
         <script type="text/javascript">
         

           //點擊添加按鈕是調用模態框

        function myModal(){
        $("#myModalLabel1").text("NEW");          //添加和修改調用同一個模態框,myModalLabel1表示模態框的標題
        showModal();
         }
         
         function showModal(type,item,id){                        //type表示類型,item表示每一個選項
        $("#value").val("");                                           //將二級目錄的值設爲空
        $("#id").val("");                                                  
         
        if(id){                       //如果id存在,則顯示update的模態框

                        $("#value").val(item);     //獲取二級目錄中的值 

        $("#id").val(id);              //使隱藏的id等於當前當前的id        

         
        $("#myModalLabel1").text("Modify");   //改變模態框title
        $("#update").show();                 //顯示confirm按鈕
        $("#create").hide();                    //隱藏添加按鈕
        var options=$("#modelType").find("option");   //一級目錄下的option(表示每一個type)
                    options.each(function(){
                      if($(this).val()==type){
                           $(this).attr("selected","selected");
                        }
                              }); 
                  }
        $("#myModal").modal();    //顯示modify的模態框
         }
     
         function add(){
        $.post("${path}/arwen/visiting/feedback/create",{type:$("#modelType").val(),value:$("#value").val()},function(){   //向服務器傳遞數據
        window.location.reload();
        })
         }   
         
         function update(){
        $("#update").show();
        $.post("${path}/arwen/visiting/feedback/update/"+ $("#id").val(),{type:$("#modelType").val(),value:$("#value").val(),id:$("#id").val()},function(){  //向服務器傳遞數據
        window.location.reload();
        })
         }
         
         function del(id){
        var uri="${path}/arwen/visiting/feedback/delete";
        $.post(uri,{Id:id},function(data){
        if(data){
        if(data.result==true){
        alert("delete success !");
        $("#"+id).remove();
        window.location.href=window.location.href;
         
        }else{
        alert(data.message);
        }
        }
         
        })
         
         }
</script>
                                       

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章