根据记录ID限制子窗口

时间:2017-04-26 17:57:51

标签: javascript c# asp.net

我有一个包含多条记录的网格视图。有两种类型的记录:recordTypeA和recordTypeB。对于recordTypeA,我想限制我可以打开的子窗口。我不能同时打开recordTypeA的两个副本(重复)。

例如,如果打开了查询字符串id为1的recordTypeA,我不希望另一个windowTypeA id为1的窗口打开,但如果它们有不同的查询字符串id,我可以打开两个recordTypeA。

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;



    }

}

除非我打开id为1的recordTypeA,然后打开id为2的recordTypeA,否则此代码工作正常。具有id 2的记录将覆盖第一个使我可以在不同窗口中打开1次两次的记录。

如何实现理想的行为?

请帮忙,

谢谢

1 个答案:

答案 0 :(得分: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

注意:创建每种类型的管理器对象,然后根据需要使用它们打开或关闭窗口。