官方例子:
Summary It may prove beneficial to validate user input within the client-side events. The BeforeExitEditModeHandler allows for a perfect time to validate the the input and "lock" the user on the cell until proper data has been entered. Additional Information First, add a handler for the client-side event, BeforeExitEditModeHandler. This property can be set either in the Property Pages under displaylayout>ClientsideEvents>BeforeExitEditModeHandler or in code: UltraWebGrid1.DisplayLayout.ClientSideEvents.BeforeExitEditModeHandler = "BeforeExitEditMode" This event receives two parameters: gridname and cell id. To keep the user within the cell, return true to cancel the event. Here is a sample that will not allow a user to enter a date that is after today for a birthdate: //create a global variable so alertbox does not appear twice. box appears twice since in the BeforeEndEditMode you are calling alert(message) which tries to end the edit as well //without this then you would find yourself in an infinite loop or have an alert appear twice. var oldvalue=null; function beforeExitEditMode(gridName,cellID) { //get the cell cell=igtbl_getCellById(cellID); //get the grid grid=igtbl_getGridById(gridName); //check to see if the user is in the birthdate column if(cell.Column.Key=="BirthDate") { //get today's date var today=new Date(); //get the value of the current cell var valDate=new Date(cell.getValue()); if(valDate < today) { //if the date is before today then allow them to exit edit mode return 0; } //won't get here unless date is greater then today since a valid date would have exited the function above //the next line is neccesarry so we know that this is the first time the user is coming through the function and //we avoid duplicate alert messages if(oldvalue==null) { //set oldvalue to current value so this is not hit next time through and display alert box oldvalue=valDate; alert("BirthDate can't be in the future"); } //if the user has gotten this far then value is invlaid so return 1 to cancel the exiting of edit mode return 1; } }
我自己写的,感觉写的不太优雅,比较丑陋,但是照着官方的写总是满足不了需求
var oldVal function Grid_BeforeExitEditModeHandler(gridName, cellID) { //get the cell cell = igtbl_getCellById(cellID); //get the grid grid = igtbl_getGridById(gridName); if (cell.Column.Key == "operatingtimeperday") { oldVal = parseFloat(cell.getValue()); } } function Grid_AfterExitEditModeHandler(gridName, cellID) { //get the cell cell = igtbl_getCellById(cellID); //get the grid grid = igtbl_getGridById(gridName); //check to see if the user is in the birthdate column if (cell.Column.Key == "operatingtimeperday") { //get the value of the current cell var val = parseFloat(cell.getValue()); if (val > 24) { alert("输入时间不可大于24小时"); cell.setValue(oldVal); return 1; } if (val < 24) { return 0; } } }