jqgrid 检索单元格的数据并对其进行操作 [英] jqgrid retrieve data of cell and manipulate it

查看:17
本文介绍了jqgrid 检索单元格的数据并对其进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 dataEvents 事件中检索用户输入的值.我只想允许数字 0-24,如果用户插入像 4,5(德语写作)这样的数字,我想用."替换,".因此将4,5"转换为4.5".

I want to retrieve from a dataEvents event the value the user entered. I want to only allow the numbers 0-24 and if the user inserts a number like 4,5 (german writing) I want to replace the "," with a ".". Thus convert "4,5" to "4.5".

但我正在努力获取用户输入的数据.我使用的方法总是返回空白.

But I'm struggling with getting the data the user entered. The method I'm using always returns blank.

colModel:[
    {name:'sum',index:'sum', width:45, editable: true, sortable:false,
     editoptions: { dataEvents: [ 
                        {
                            type: 'keypress', // keydown
                            fn: function(e) {
                                // console.log('keypress');
                                var v=$(e.target).text();
                                alert(v); // v is empty.
                                //reset the target value, actually I want to replace
                                // enter code here a comma with a point
                                // only allow the numbers 0 - 24
                            }
                        }
                    ] 
                  }
    },
],

推荐答案

您可以将 ',' 替换为 '.'更好地在keyup"事件处理程序中使用以下内容

You can do replacement of ',' to '.' better inside of 'keyup' event handler with the following

$(this).val($(this).val().replace(/,/,'.'));

所以你可以使用以下 dataEvents

dataEvents: [
    {
        type: 'keyup',
        fn: function(e) {
            var oldVal = $(this).val();
            var newVal = oldVal.replace(/,/,'.');
            if (oldVal !== newVal) {
                $(this).val(newVal);
            }
        }
    },
    {
        type: 'keypress',
        fn: function(e) {
            var key = e.charCode || e.keyCode; // to support all browsers
            if((key < 48 || key > 57) &&   // if non digit
               key !== 46 && key !== 44 && key !== 8 && // and not . or , or backspace
               key !== 37 && key !== 39) { // arrow left and arrow right
                return false;
            }
        }
    }
]

以下演示上,您可以实时查看结果.我在示例中看到的唯一缺点如下:如果您尝试在文本中间键入逗号 ,则光标位置(插入符号)将在替换逗号指向.在你的情况下,这可能不是一个真正的问题.如果您确实想保存光标位置,您可能应该这样做document.selection 用于 IE 或 .selectionStart.selectionEnd 用于 Firefox.

On the following demo you can see the results live. The only disadvantage which I see in the example is following: if you will try to type comma in the middle of the text, the cursor position (caret) will be changed to the end of the input after the replacement comma to point. Probably it is not a real problem in your case. It you do want to save the cursor position you should probably do this document.selection using for IE or .selectionStart and .selectionEnd for Firefox.

更新:我修复了在 Firefox 的 keypress 事件中使用 e.keyCode 的问题.我遵循 here 的信息,现在使用 e.charCode ||e.keyCode 而不是 e.keyCode.上述代码和演示现已修复.

UPDATED: I fixed the problem with the usage of e.keyCode inside of keypress event in the Firefox. I follows the information from here and use now e.charCode || e.keyCode instead of e.keyCode. The above code and the demo are fixed now.

这篇关于jqgrid 检索单元格的数据并对其进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆