Jqgrid extracts and manipulates cell data

I want to extract the value entered by the user from the dataEvents event. I want to allow only numbers 0-24, and if the user inserts a number, for example 4,5 (German), I want to replace "," with ".". Thus, convert "4.5" to "4.5".

But I'm struggling with getting user input. The method I use always returns empty.

 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 } } ] } }, ], 
0
jquery jqgrid
source share
1 answer

You can replace ',' with '.' better inside a keyup event handler with the following

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

So you can use the following 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; } } } ] 

In the next demo you can see the results live. The only drawback that I see in this example is the following: if you try to enter a comma in the middle of the text , the cursor position (carriage) will be changed until the end of the input after replacing the comma. This is probably not a real problem in your case. That you want to keep the cursor position, you should probably do this document.selection for IE or .selectionStart and .selectionEnd for Firefox.

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

+7
source share

Source: https://habr.com/ru/post/650813/


All Articles