Telerik MVC Grid - 排序停止 javascript

发布于 2025-01-06 04:39:09 字数 1496 浏览 1 评论 0原文

我在 MVC3 中有一个使用 Telerik 网格的页面。在网格中,每一行都有一个文本框,我在其中输入小数。我有一个 css 类“HoursNew”,用于连接 jQuery 插件 ( autoNumeric.js )。我还有一个 onBlur 函数,可以将任何小数输入四舍五入到最接近的半个小数。 问题是,对列进行排序后,所有 JavaScript 似乎都停止工作。 autoNumeric.js 停止与我触发 onBlur 的函数一起工作。

    $(document).ready(function () {
    $('.HoursNew').autoNumeric({ pSign: 's', vMin: '-999.5', vMax: '999.5', mRound: 'C', aPad: false });
    $('.HoursNew').blur(function () { roundToHalf(this); });
});

            // Round decimal to nearest .5
            // v is input object
            function roundToHalf(v) {
                var value = v.value;
                var r;
                //alert(v.id);
                var converted = parseFloat(value); // Make sure we have a number
                var decimal = (converted - parseInt(converted, 10)); // Pull the decimal value
                decimal = Math.round(decimal * 10);
                if (decimal == 5) { return (parseInt(converted, 10) + 0.5); } // leave alone if .5
                if ((decimal < 3) || (decimal > 7)) {
                    r = Math.round(converted); // Round up or down to nearest whole number
                } else {
                    r = (parseInt(converted, 10) + 0.5); //round to .5
                }
                //alert(r);
                // reset input value to new value
                $('#' + v.id).val(r);
            }

页面在第一次加载时工作正常,只是在排序后出现这个问题。有什么建议吗?

I have a page in MVC3 using a Telerik grid. In the grid, for each row, is a textbox that I'm entering an decimal. I have a css class, 'HoursNew', which is used to connect a jQuery plugin ( autoNumeric.js ). I also have an onBlur function to round any decimal input to the nearest half decimal.
Problem is, after sorting a column, all JavaScript seems to stop working. The autoNumeric.js stops working along with the function I have that fires onBlur.

    $(document).ready(function () {
    $('.HoursNew').autoNumeric({ pSign: 's', vMin: '-999.5', vMax: '999.5', mRound: 'C', aPad: false });
    $('.HoursNew').blur(function () { roundToHalf(this); });
});

            // Round decimal to nearest .5
            // v is input object
            function roundToHalf(v) {
                var value = v.value;
                var r;
                //alert(v.id);
                var converted = parseFloat(value); // Make sure we have a number
                var decimal = (converted - parseInt(converted, 10)); // Pull the decimal value
                decimal = Math.round(decimal * 10);
                if (decimal == 5) { return (parseInt(converted, 10) + 0.5); } // leave alone if .5
                if ((decimal < 3) || (decimal > 7)) {
                    r = Math.round(converted); // Round up or down to nearest whole number
                } else {
                    r = (parseInt(converted, 10) + 0.5); //round to .5
                }
                //alert(r);
                // reset input value to new value
                $('#' + v.id).val(r);
            }

Page works fine on first load, just this problem after a sort. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再可℃爱ぅ一点好了 2025-01-13 04:39:09

我有预感,排序时,网格会进行 ajax 调用来渲染新的网格。这意味着您在 $(document).ready 中执行的任何绑定都将被删除。

请查看 Telerik 文档中有关客户端事件的页面。您需要将此部分添加到网格定义中:

.ClientEvents(events => events
    .OnDataBound("onDataBound")

并且要重新绑定此 javascript:

function onDataBound(e) {
    $('.HoursNew').autoNumeric({ pSign: 's', vMin: '-999.5', vMax: '999.5', mRound: 'C', aPad: false });
    $('.HoursNew').blur(function () { roundToHalf(this); });

}

I have a hunch that when sorting, the grid is making an ajax call that renders a new grid. What this means is that any bindings you performed in $(document).ready are removed.

Take a look at this page on client-side events in the Telerik documentation. You'll need to add this section to your grid definition:

.ClientEvents(events => events
    .OnDataBound("onDataBound")

And this javascript to rebind:

function onDataBound(e) {
    $('.HoursNew').autoNumeric({ pSign: 's', vMin: '-999.5', vMax: '999.5', mRound: 'C', aPad: false });
    $('.HoursNew').blur(function () { roundToHalf(this); });

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文