如何计算数据表中两个日期的数量

时间:2014-06-19 08:04:33

标签: primefaces datatable calendar

如何在数据表

中计算输入框的两个日期

我做了单数日历,这里计算2dates是代码

  $(document).ready(function() {  
  //Bind onchange on textfields
  PF('fromCal').jq.change(function() { 
  calculateFromTo(PF('fromCal'), PF('toCal'));
   }); 
  PF('toCal').jq.change(function() { 
  calculateFromTo(PF('fromCal'), PF('toCal'));
   });

  //Bind dateSelect from popup
  PF('fromCal').cfg.behaviors = {
  dateSelect: function() {calculateFromTo(PF('fromCal'),   
  PF('toCal'))},                              
  };
  PF('toCal').cfg.behaviors = {
  dateSelect: function() {calculateFromTo(PF('fromCal'), 
  PF('toCal'))},                                                        
   };                        
  });

   //calculate function
   function calculateFromTo(from, to) {
   oneDay = 24 * 60 * 60 * 1000;
   fromDate = from.getDate();
   toDate = to.getDate();

   if (fromDate && toDate) {
    diffDays = Math.round(Math.abs((fromDate.getTime() -
         toDate.getTime()) / (oneDay)));
     //get whatever element and update the calculated value/text
    $('.daysNumber').val(diffDays + " Days");
  }
 }

在xhtml中我有这个

<p:calendar widgetVar="fromCal" /> 
<p:calendar widgetVar="toCal" />
<p:inputText styleClass="daysNumber" />

现在我想在datatable中计算两个日期,我怎么计算这个?任何建议或建议?谢谢! 这是我使用数据表code

的代码

1 个答案:

答案 0 :(得分:0)

将您的页面更改为:

<p:calendar widgetVar="fromCal" value="#{dayDiffBean.from}">
        <p:ajax event="dateSelect" listener="#{dayDiffBean.fromSelected}" update="diff" />
    </p:calendar>
    <p:calendar widgetVar="toCal" value="#{dayDiffBean.to}">
        <p:ajax event="dateSelect" listener="#{dayDiffBean.toSelected}" update="diff" />
    </p:calendar>
    <p:inputText id="diff" styleClass="daysNumber" value="#{dayDiffBean.diff}"/>

支持bean看起来像:

@Named
@SessionScoped
public class DayDiffBean implements Serializable{

private Date from;
private Date to;
private String diff="";
private final long oneDay=1000*60*60*24;

public void fromSelected(SelectEvent event){
    from=(Date) event.getObject();
    calDiff();
}

public void toSelected(SelectEvent event){
    to=(Date) event.getObject();
    calDiff();
}

public void calDiff(){
    if(from==null||to==null){
        diff="N/A";
        return;
    }
    diff=(to.getTime()-from.getTime())/oneDay+"";
}  
// getters and setters below...