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

查看:132
本文介绍了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的问题.我遵循此处中的信息,现在使用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天全站免登陆