覆盖内置于第三方控件的javascript(infragistics UltraWebGrid)

时间:2012-11-30 16:36:38

标签: javascript override infragistics

我使用的是Infragistics的旧版UltraWebGrid,需要替换一些内置的javascript。已编译的js看起来像是将一堆函数添加到对象类型作为各种api。格式如:

var igtbl_ptsBand = ["functionname1",function(){...},"functionname2",function(){...},... 

等等。我该怎么改写呢?

基本上,控件是以与新浏览器不兼容的方式向页面添加html,并且执行此操作的javascript代码只需要稍微调整一下。我找到了代码......我只需要改变它。

可以找到代码here

我在转储代码示例中添加了答案。我不会选择这个答案

Similar SO question

3 个答案:

答案 0 :(得分:1)

我不建议这样做。您应该始终尝试使用第三方库,而不是反对。话虽如此,这应该有效:

igtbl_ptsBand[igtbl_ptsBand.indexOf("functionYouWantToOverwrite") + 1] = function () {
    // your new stuff...
};

答案 1 :(得分:1)

您提到的数组似乎是各种各样的函数表:

var igtbl_ptsBand = ["func1", function() { }, "func2", function() { } ]

我建议使用链接而不仅仅是覆盖。使用链接,您可以注入自己的代码,但仍然可以调用原始函数。假设您要替换“func2”和链。你可以这样做:

var origFunc, findex, ix;
if (igtbl_ptsBand.indexOf) {
    // indexOf is supported, use it
    findex = igtbl_ptsBand.indexOf("func2") + 1;
} else {
    // Crippled browser such as IE, no indexOf, use loop
    findex = -1;
    for (ix = 0;  ix < igtbl_ptsBand.length;  ix += 2) {
        if (igtbl_ptsBand[ix] === "func2") {
            findex = ix + 1;
            break;
        }
    }
}
if (findex >= 0) {
    // Found it, chain
    origFunc = igtbl_ptsBand[findex];
    igtbl_ptsBand[findex] = function() {
         // Your new pre-code here
         // Call original func (chain)
         origFunc();
         // Your new post-code here
    };
}
当然,

origFunc可能有参数,你可能想使用JavaScript call()函数将“this pointer”设置为特定的东西,例如:

origFunc.call(customThis, arg1, arg2...);

如果参数在数组中,则可以使用apply()而不是call()。

答案 2 :(得分:0)

好的,我正在做的事情。我会用我的进度更新这个。

这解决了我的问题。事实证明我必须将函数阵列应用于父“行”的所有子对象。为此,我在“Fix Rows”功能中添加了代码。我把它分开是因为我正在运行其他浏览器JS错误,我正在这个js文件中修复。

这是我添加到我的.net页面的js文件,如此...

    </form>
    <script type="text/javascript" src="../../scripts/BrowserCompat.js"></script>
</body>
</html>

Brows_FixUltraWebGrid();

function Brows_FixUltraWebGrid() {
    FixRows();
}

function FixRows() {

    FixGridRows_render();
    for (var i = 0; i < igtbl_ptsRows.length; i += 2)
        igtbl_Rows.prototype[igtbl_ptsRows[i]] = igtbl_ptsRows[i + 1];

}


function FixGridRows_render() {

    var origFunc, findex, ix;
    if (igtbl_ptsRows.indexOf) {
        // indexOf is supported, use it
        findex = igtbl_ptsRows.indexOf("render") + 1;
    } else {
        // Crippled browser such as IE, no indexOf, use loop
        findex = -1;
        for (ix = 0; ix < igtbl_ptsRows.length; ix += 2) {
            if (igtbl_ptsRows[ix] === "render") {
                findex = ix + 1;
                break;
            }
        }
    }
    if (findex >= 0) {
        // Found it, chain
        origFunc = igtbl_ptsRows[findex];
        igtbl_ptsRows[findex] = function() {
            // Your new pre-code here
            // Call original func (chain)
            //origFunc();
            // Your new post-code here
            var strTransform = this.applyXslToNode(this.Node);
            if (strTransform) {
                var anId = (this.AddNewRow ? this.AddNewRow.Id : null);

                //new logic to include tbody if it is not there
                var tadd1 = '';
                var tadd2 = '';
                if (!(/\<tbody\>/.test(strTransform))) {
                    tadd1 = '<tbody>';
                    tadd2 = '</tbody>';
                }
                this.Grid._innerObj.innerHTML =
                    "<table style=\"table-layout:fixed;\">" + tadd1 + strTransform + tadd2 + "</table>";

                //old line
                //this.Grid._innerObj.innerHTML = "<table style=\"table-layout:fixed;\">" + strTransform + "</table>";

                var tbl = this.Element.parentNode;
                igtbl_replaceChild(tbl, this.Grid._innerObj.firstChild.firstChild, this.Element);
                igtbl_fixDOEXml();
                var _b = this.Band;
                var headerDiv = igtbl_getElementById(this.Grid.Id + "_hdiv");
                var footerDiv = igtbl_getElementById(this.Grid.Id + "_fdiv");
                if (this.AddNewRow) {
                    if (_b.Index > 0 || _b.AddNewRowView == 1 && !headerDiv || _b.AddNewRowView == 2 && !footerDiv) {
                        var anr = this.AddNewRow.Element;
                        anr.parentNode.removeChild(anr);
                        if (_b.AddNewRowView == 1 && tbl.tBodies[0].rows.length > 0)
                            tbl.tBodies[0].insertBefore(anr, tbl.tBodies[0].rows[0]);
                        else
                            tbl.tBodies[0].appendChild(anr);
                    }
                    this.AddNewRow.Element = igtbl_getElementById(anId);
                    this.AddNewRow.Element.Object = this.AddNewRow;
                }
                this.Element = tbl.tBodies[0];
                this.Element.Object = this;
                this._setupFilterRow();
                for (var i = 0; i < this.Band.Columns.length; i++) {
                    var column = this.Band.Columns[i];
                    if (column.Selected && column.hasCells()) {
                        var col = this.getColumn(i);
                        if (col)
                            igtbl_selColRI(this.Grid.Id, col, this.Band.Index, i);
                    }
                }
                if (this.ParentRow) {
                    this.ParentRow.ChildRowsCount = this.length;
                    this.ParentRow.VisChildRowsCount = this.length;
                }
            }
            console.log('overridden row render function executed');
        };
    }
}
相关问题