2017-04-26 83 views
0

我有一个包含多个记录的网格视图。有两种类型的记录:recordTypeA和recordTypeB。对于recordTypeA我想限制我可以打开的子窗口。我不能同时打开两个recordTypeA副本(重复)。例如,如果打开查询字符串ID为1的recordTypeA,我不想让recordTypeA id 1打开另一个窗口,但如果它们具有不同的查询字符串ID,则可以打开两个recordTypeA。根据记录ID限制子窗口

var g_windowReference = null; 


function openwindow(windowUrl, isnotRecordB) 
{ 

    var windowFeatures = "toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=yes"; 

    if (typeof isnotRecordB == 'undefined') 
    { 

     g_windowReference = window.open(windowUrl, windowFeatures); 
     g_windowReference.focus(); 
    } 
    else 
    { 
     window.open(windowUrl, windowFeatures); 
     window.focus(); 

    } 


    return false; 
} 

function closeWindow(windowUrl) 
{ 
    if ((g_windowReference !== null) && (g_windowReference.closed === false)) 
    { 

      g_windowReference.close(); 
      g_windowReference = null; 



    } 

} 

此代码工作正常,除非我打开recordTypeA ID为1,然后用recordTypeA ID 2. ID为2的记录将覆盖第一个让我可以在不同的窗口与1打开纪录的两倍。

如何才能达到理想的行为?

请帮帮忙,

谢谢

回答

1

您必须保存已打开任一类型的recordIds。

请看下面这个简单的实现。

function WindowManager(recordType) { 
    if(!recordType) { 
     throw "Please provide a recordType"; 
    } 

    var windows = {}; //recordId:window pairs {"typeA":window1, "typeB":window2} 

    this.openWindow = function (windowUrl, recordId) { 
     if(!recordId) { 
      throw "Please provide recordId"; 
     } 

     if(!windows[recordId]) {//If recordId window is not found 
      var newWindow = openwindow(windowUrl); 
      windows[recordId] = newWindow; //Storing reference to close in future 

      console.log("opened " + recordType + ":" + recordId); 
     } else { 
      throw "Window for recordId " + recordId + " of type " + recordType + " is already open"; 
     } 
    } 

    this.closeWindow = function (recordId) { 
     if(!recordId) { 
      throw "Please provide recordId"; 
     } 

     var recordWindow = windows[recordId]; 
     recordWindow.close(); 

     delete windows[recordId];//removing window after it's closed 

     console.log("closed " + recordType + ":" + recordId); 

     return recordWindow; 
    } 

    this.getRecordType = function() { 
     return recordType; 
    } 

    this.getOpenRecordIds = function() { 
     return Object.keys(windows); 
    } 

    function openwindow (windowUrl, recordId) { 
     var windowFeatures = "toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=yes"; 

     var newWindow = window.open(windowUrl, windowFeatures); 
     newWindow.focus(); 

     return newWindow; 
    } 
} 

在行动

enter image description here

注:创建每种类型的经理的对象,然后用它们可以根据需要开启或关闭窗口。

+0

谢谢你,它完美的作品。 –

+0

不客气! – 11thdimension