1

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

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

等等。我将如何覆盖它?

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

代码可以在这里找到

我添加了一个答案来转储代码示例等等。我不会选择这个答案

类似的问题

4

3 回答 3

1

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

igtbl_ptsBand[igtbl_ptsBand.indexOf("functionYouWantToOverwrite") + 1] = function () {
    // your new stuff...
};
于 2012-11-30T16:38:40.403 回答
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 指针”设置为特定的东西,例如:

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

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

于 2012-11-30T16:54:59.020 回答
0

好的,这就是我正在做的事情。我会用我的进展来更新这个。

这解决了我的问题。事实证明,我必须将函数数组应用于父“行”的所有子对象。为此,我在“修复行”函数中添加了代码。我将其拆分是因为我正在跨其他浏览器 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');
        };
    }
}
于 2012-11-30T20:43:51.587 回答