ajax返回值彈窗顯示


需求是:要求給一些數據加上鏈接,根據不同的數據,向后台傳入不同的參數。得到返回值后,彈窗顯示。

采用ajax,即時查詢信息。根據返回的json數據動態添加table行。 

 
寫代碼在以下幾個地方遇到困難。
1.添加鏈接時的openPositionDialog函數,由於單雙引號的緣故,使js的觸發函數,無法將員工ID傳到函數頁面。
2.返回數據,前台頁面無法解析。原因是,實體類Dealer中有Date、Set等其他類型,json無法解析,因此將要展示的數據,重新定義一個新類,只有最基本的數據類型。
3.遍歷json數據。利用json自帶的函數each函數,之前接觸較少。
4.根據返回的數據動態添加table行,關閉對話框時,需要另加代碼將生成的tabl行刪掉,否則影響下一次展示。
 
代碼:
1.頁面采用jqGrid,給數據添加鏈接
1 function showPositionLink(cellvalue, options, rowObject) {
2      var dealerId = $(rowObject).children("dealerId").text();
3      var employeeId = $(rowObject).children("id").text();
4      return "<a href='#' onclick=\"openPositionDialog('"+employeeId+"')\" style='color:#569AD5;'>" + cellvalue + "</a>";
5 }

2.展示頁面,用的是freemaker

 1 <div id='positionpanel' style='display:none'>
 2   <div>
 3   <span><b><@s.text name='staff.property.dealer.name' />:</b></span>
 4   &nbsp;
 5   <span id="dealerName"></span>
 6   &nbsp;&nbsp;
 7   <span id="dealerBrand"></span>
 8   &nbsp;&nbsp;
 9   <span id="serviceType"></span>
10   <br/>
11   <span><b><@s.text name='staff.property.job.Title' />:</b></span>
12   &nbsp;&nbsp;&nbsp;&nbsp;
13   <span id="jobTitle"></span>
14   </div>
15   <div>
16   <table id="positionTable">
17    <thead>
18    <tr height=22px>
19    <th width="200px"><@s.text name='staff.property.employee.name' /></th>
20    <th width="38%"><@s.text name='staff.property.employee.startDate' /></th>
21    <th width="39%"><@s.text name='staff.property.employee.endDate' /></th>
22    </tr>
23    </thead>
24    <span><hr/></span>
25    <tbody style="border: solid 1px #AAAAAA;">
26    </tbody>
27   </table>
28   </div>
29 </div>
30 <script language="javascript">
31 function openPositionDialog(employeeId){
32 var $positionPanelHtml = $('#positionpanel').html();
33 var $positionForm = $("<div id='positionpanel' title='<@s.text name='staff.property.position.records'/>'>").html($positionPanelHtml);
34 $.ajax({
35 type:'POST',
36             url:"${base}/${c.macro_config.async}/dealerPositionRecords.do",
37             data:{employeeId:employeeId},
38             success:function(data){
39              for(var i = 0; i < data.length; i++){
40          $("#positionTable tbody").append("<tr id=\'positionTable_tr"+i+"\' align='center'><td id=\'employeeName"+i+"\'></td><td id=\'startDate"+i+"\'></td><td id=\'endDate"+i+"\'></td></tr>");
41          }
42     $(data).each(function(index,item){   
43                 $positionForm.find("#dealerName").html(item.dealerName);
44                 $positionForm.find("#dealerBrand").html(item.dealerBrand);
45                 $positionForm.find("#serviceType").html(item.dealerService);
46                 $positionForm.find("#jobTitle").html(item.jobTitle);
47                 $positionForm.find("#employeeName"+index).html(item.employeeName);
48                 $positionForm.find("#startDate"+index).html(item.startDate);
49                 $positionForm.find("#endDate"+index).html(item.endDate);
50              });
51          for(var i = 0; i < data.length; i++){
52          $("#positionTable_tr"+i).remove();
53          }      
54          }
55         });
56         
57         $positionForm.find("#dealerBrand").val(dealerBrand);
58         
59         var $leavedialog = $positionForm.dialog({ 
60          width: 600,
61          height: 300,
62             modal:true,
63             buttons: {
64                 "<@s.text name='employee.leave.button.ok'/>": function() {
65                     $(this).dialog("destroy");
66                     $("#positionForm").remove();
67                 }
68             },
69             close: function() {
70                 $(this).dialog("destroy");
71                 $("#positionForm").remove();
72             }
73             });
74 $("#positionpanel").dialog("open");
75 }
76 </script>

3.json數據展示類

 1 public class PositionRecord extends KpiPager {
 2     private String employeeName;
 3     private String dealerId;
 4     private String dealerName;
 5     private String startDate;
 6     private String endDate;
 7     private String dealerBrand;
 8     private String dealerService;
 9     private String jobTitle;
10    
11     public PositionRecord() {
12       super();
13     }
14 
15       public String getEmployeeName() {
16             return employeeName;
17      }
18 
19       public void setEmployeeName(String employeeName) {
20             this. employeeName = employeeName;
21      }
22 
23       public String getDealerId() {
24             return dealerId;
25      }
26 
27       public void setDealerId(String dealerId) {
28             this. dealerId = dealerId;
29      }
30 
31       public String getDealerName() {
32             return dealerName;
33      }
34 
35       public void setDealerName(String dealerName) {
36             this. dealerName = dealerName;
37      }
38 
39       public String getStartDate() {
40             return startDate;
41      }
42 
43       public void setStartDate(String startDate) {
44             this. startDate = startDate;
45      }
46 
47       public String getEndDate() {
48             return endDate;
49      }
50 
51       public void setEndDate(String endDate) {
52             this. endDate = endDate;
53      }
54 
55       public String getDealerBrand() {
56             return dealerBrand;
57      }
58 
59       public void setDealerBrand(String dealerBrand) {
60             this. dealerBrand = dealerBrand;
61      }
62 
63       public String getDealerService() {
64             return dealerService;
65      }
66 
67       public void setDealerService(String dealerService) {
68             this. dealerService = dealerService;
69      }
70 
71       public String getJobTitle() {
72             return jobTitle;
73      }
74 
75       public void setJobTitle(String jobTitle) {
76             this. jobTitle = jobTitle;
77      }
78 
79 }

4.xml配置文件

1 <action name="dealerPositionRecords" class="com.bmw.assessment.action.StaffAction" method="dealerPositionRecords">
2        <result name="success" type="json">
3                 <param name="root">positionRecords</param>
4        </result> 
5 </action>

 

 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM