javascript中的IF条件不能打印正确的ID

时间:2013-06-06 15:42:59

标签: javascript xpages

在XPages中,我试图通过复选框从视图中提取一个值。

我有一个名为'viewpanel2'的视图,其中包含LoggerID个,每个LoggerID由数值组成。

我有一个按钮,当选中LoggerID旁边的复选框并单击按钮时,使用javascript从视图中提取值。我遇到的问题是它会不断打印视图中所有LoggerID的所有值,而不仅仅是已经选中的值。

以下是按钮的代码:

var viewPanel = getComponent("viewPanel2"); //in the view panel select the check box
var docIDArray = viewPanel.getSelectedIds(); //get that selected ID
for (i = 0; i < docIDArray.length; i++) {
    var docId = docIDArray[0];
    var doc = database.getDocumentByID(docId);
    var moo = doc.getItemValue("LoggerID"); //selected ID is defined

    var vw = database.getView('BrowserStats'); //Get the Notesview
    var vwEntries = vw.getAllEntries(); //Get handle of all entries
    var vwCount = vwEntries.getCount(); //Get the Count to use as uBound
    var vwEntry = vwEntries.getFirstEntry(); //Get first value

    //Get the first entry and get the column value of the first entry
    var plot_LoggerID = vwEntry.getColumnValues().elementAt(2);

    for (var x = 1; x < vwCount; x++) {
    //Looping through all the entries and collect the column values
    vwEntry = vwEntries.getNextEntry(vwEntry);
    plot_LoggerID = plot_LoggerID + "," + vwEntry.getColumnValues().elementAt(2);
    }
    //comparing the entries to the selected ID and if found extracting the values for LoggerID
}
if (moo = vwEntry) {
    getComponent("extracted_LoggerID").setValue("["+plot_LoggerID+"]");
} else {
    print = ("no data available");
}

我相信我的if条件不起作用。有人可以提出解决方案吗?

3 个答案:

答案 0 :(得分:7)

你正在做作业,而不是比较。这样做:

if (moo === vwEntry) {

或者如果它们属于同一类型,则应使用'==='。

答案 1 :(得分:2)

你错了的部分是:

if (moo = vwEntry) {

主要原因是JavaScript中的单个=符号表示您为变量指定值。

您应该使用==来比较两个值,或者===来比较两个需要相同的值。



Javascript符号

=

是一项任务。


==

是比较


===

是您希望值相同的比较(相同的类型)。

答案 2 :(得分:0)

这是工作代码。我相信

var doc = database.getDocumentByID(docId); 

说获取所选文件的信息

var moo = doc.getItemValue("LoggerID"); 

说出你想要的文件。然后不需要if语句就可以简单地说

getComponent("extracted_LoggerID").setValue(moo); 

获取该doc id,进入视图并提取所选文档ID的数据。

整个工作代码看起来像这样

  var viewPanel = getComponent("viewPanel1"); //in the view panel select the check box
     var docIDArray = viewPanel.getSelectedIds(); //get that selected ID

  for (i = 0; i < docIDArray.length; i++) {
   var docId = docIDArray[i];
   var doc = database.getDocumentByID(docId);
      var moo = doc.getItemValue("LoggerID"); //selected ID is defined
      var h = doc.getItemValue("G");
      var e = doc.getItemValue("IE");
      var f = doc.getItemValue("FF");
      var c = doc.getItemValue("CH");
      var s = doc.getItemValue("SA");
      var o = doc.getItemValue("OP");
      var l = doc.getItemValue("LM");

//comparing the entries to the selected ID and if found extracting the values for LoggerID

      var vw = database.getView('BrowserStats'); //Get the Notesview
      var vwEntries = vw.getAllEntries(); //Get handle of all entries
      var vwCount = vwEntries.getCount(); //Get the Count to use as uBound
      var vwEntry = vwEntries.getFirstEntry(); //Get first value

//Get the first entry and get the column value of the first entry

      var plot_LoggerID = vwEntry.getColumnValues().elementAt(2);
      var plot_G = vwEntry.getColumnValues().elementAt(5);    
      var plot_IE = vwEntry.getColumnValues().elementAt(6);    
      var plot_FF = vwEntry.getColumnValues().elementAt(7);    
      var plot_CH = vwEntry.getColumnValues().elementAt(8);    
      var plot_SA = vwEntry.getColumnValues().elementAt(9);
      var plot_OP = vwEntry.getColumnValues().elementAt(10);    
      var plot_LM = vwEntry.getColumnValues().elementAt(11);            

      for (var x = 1; x < vwCount; x++) {
  //    Looping through all the entries and collect the column values
      vwEntry = vwEntries.getNextEntry(vwEntry);
      plot_LoggerID = plot_LoggerID + "," + vwEntry.getColumnValues().elementAt(2);
      plot_G = plot_G + "," + vwEntry.getColumnValues().elementAt(5);    
      plot_IE = plot_IE + "," + vwEntry.getColumnValues().elementAt(6);    
      plot_FF = plot_FF + "," + vwEntry.getColumnValues().elementAt(7);    
      plot_CH = plot_CH + "," + vwEntry.getColumnValues().elementAt(8);    
      plot_SA = plot_SA + "," + vwEntry.getColumnValues().elementAt(9);    
      plot_OP = plot_OP + "," + vwEntry.getColumnValues().elementAt(10);    
      plot_LM = plot_LM + "," + vwEntry.getColumnValues().elementAt(11);    

     }

      getComponent("extracted_LoggerID").setValue(moo);
      getComponent("extracted_G").setValue(h);
      getComponent("extracted_IE").setValue(e);    
      getComponent("extracted_FF").setValue(f);    
      getComponent("extracted_CH").setValue(c);    
      getComponent("extracted_SA").setValue(s);    
      getComponent("extracted_OP").setValue(o);   
      getComponent("extracted_LM").setValue(l);       
    }

看起来我从不需要if语句。