How to force CellValidate on bulk copy-paste

48 views
Skip to first unread message

Mangesh

unread,
Aug 3, 2009, 4:53:46 AM8/3/09
to completeui
Hi,

I have a grid and its cells have different validations. When I type in
the cell and press tab, CellValidate event gets fired properly, but
when I paste bulk data, it doesn't get fired.

1. How can I force CellValidate on bulk copy-paste?
2. How can I prevent postback and show error/s if page data is not
valid?

Thanks and Regards,
Mangesh Gurav

fil_nitobi

unread,
Aug 3, 2009, 3:25:12 PM8/3/09
to completeui
Hi Mangesh,

To run custom validation logic for copy/paste events, attach your copy/
paste validation logic as a JavaScript function to the Grid's
OnBeforePasteEvent. As with all OnBefore-type events, returning
'false' in the JavaScript function will cancel the pending event (so
in the case of a OnBeforePasteEvent, the 'paste' action will be
cancelled). Returning true in the function will proceed with the paste
as desired.

Best regards,
Fil Maj
Web Software Engineer
Nitobi

Mangesh

unread,
Aug 18, 2009, 3:04:47 AM8/18/09
to completeui
Thanks Fil,

Copy paste validation logic is obviously same as that of individual
cell validation logic. I have already written CellValidate methods for
my cells and I would like to consume the same. Is there any way to
invoke them? Something like iterating cells in a Js function called on
OnBeforePasteEvent and calling CellValidate events of all individual
cells?

Regards,
Mangesh
> > Mangesh Gurav- Hide quoted text -
>
> - Show quoted text -

Robert Krombholz

unread,
Aug 27, 2009, 11:04:10 AM8/27/09
to completeui
Hi Mangesh,

some time ago I had the same problem.
I wanted the OnCellValidateEvent to be invoked also for pasted cells.
Here is the code that solved the problem for me (keep in mind that
this was long time ago and I was not really fimilar with nitobi &
JavaScript):

/**
* Iterates over the to-paste data and fires a ValidationEvenet for
each
* cell that will be overwritten or created by the paste option.
* The ValidateField function below will be called for each pasted
value.
*
* @param eventArgs The eventArgs given by the OnAfterPasteEvent.
*/
function pasteFiresCellValidation(eventArgs) {
//We need to create a two dimensianal array from the
eventArgs.data.
//The data is pasted like:
//11 22 33
//44 55 66
var values = eventArgs.data.split("\n");
var splittedForFields = null;
var columnCount = 0;
for (var i = 0; i < values.length; i++) {
splittedForFields = values[i].split(" ");
values[i] = new Array();
for (var a = 0; a < splittedForFields.length; a++) {
if (i == 0) {
//count all columns of the first row
columnCount++;
}
values[i][a] = splittedForFields[a];
}
}
//values[row][col] is now the right syntax to geht the
representive
//value from the pastetd data.

//declare all temporary values
var coords = eventArgs.getCoords();
var topX = coords.top.x;
var bottomX = topX + columnCount-1;
var topY = coords.top.y;
var bottomY = topY + values.length
-2;
var tmpCell = null;
var tmpSource = null;
var tmpOldValue = null;
var tmpNewValue = null;
var tmpEventArgs = null;
var rowCounter = 0;
var columnCounter = 0;
//iterate over all rows
for (var row = topY; row <= bottomY; row++) {
//iterate over all fields
for (var column = topX; column <= bottomX; column++)
{
//get the current cellObject with the row/column
counter
tmpCell = nitobi.getGrid(gridName).getCellObject(row,
column);
//get the grid datasource
tmpSource = nitobi.getGrid(gridName).getDataSource();
//get the old value from the current cell
tmpOldValue = nitobi.getGrid(gridName).getCellValue(row,
column);
//get the new value from the data array we prepared in the
beginning
tmpNewValue = values[rowCounter]
[columnCounter];
//construct a OnCellValidateEventArgs object with this
values
tmpEventArgs = new nitobi.grid.OnCellValidateEventArgs
(tmpSource,
tmpCell, tmpNewValue,
tmpOldValue);
//evaluate the eventMethod from the current column and
fire it with
//the created eventArgs
nitobi.event.evaluate(tmpCell.getColumnObject
().getOnCellValidateEvent(),
tmpEventArgs);
//increment the counter (for the prepared data
array)
columnCounter++;
}
columnCounter = 0;
//increment the counter (for the prepared data array)
rowCounter++;
}
nitobi.getGrid(gridName).clearSurfaces();
___________________________________________
Bind this function to the OnAfterPasteEvent but please review the code
before :P
Hope that this helps you.

Kind Regards
Robert

Mangesh

unread,
Sep 7, 2009, 8:25:09 AM9/7/09
to completeui
Hi Robert,
Thanks a lot! It worked for me!
You saved lots of time and efforts of mine!

I just changed it a bit. The initial code for splitting data into 2D
array was not working so I changed it to below and it worked. Giving
complete function so that someone else can directly refer it if
needed:

function OnBeforePasteInFlightGrid(eventArgs)
{
var rowValues = eventArgs.data.split("\n");

var values = new Array(rowValues.length-1);
var columnCount = rowValues[0].split(" ").length;

for(var i=0; i<= rowValues.length-2; i++)
{
values[i] = new Array(columnCount);
}

for(var p=0; p<= rowValues.length-2; p++)
{
var cellValues = rowValues[p].split(" ");
for(var q=0; q<= cellValues.length-1; q++)
{
values[p][q] =cellValues[q].trim();
}
}
var gridName = "FlightGrid";
var coords = eventArgs.getCoords();
var topX = coords.top.x;
var bottomX = topX + columnCount;
var topY = coords.top.y;
var bottomY = topY + values.length;
var tmpCell = null;
var tmpSource = null;
var tmpOldValue = null;
var tmpNewValue = null;
var tmpEventArgs = null;
var rowCounter = 0;
var columnCounter = 0;
//iterate over all rows
for (var row = topY; row < bottomY; row++)
{
//iterate over all fields
for (var column = topX; column < bottomX; column++)
{
//get the current cellObject with the row/column
counter
tmpCell = nitobi.getGrid(gridName).getCellObject(row,
column);
//get the grid datasource
tmpSource = nitobi.getGrid(gridName).getDataSource();
//get the old value from the current cell
tmpOldValue = nitobi.getGrid(gridName).getCellValue
(row,column);
//get the new value from the data array we prepared in
the beginning
tmpNewValue = values[rowCounter][columnCounter];
//construct a OnCellValidateEventArgs object with this
values
tmpEventArgs = new nitobi.grid.OnCellValidateEventArgs
(tmpSource,tmpCell, tmpNewValue,tmpOldValue);
//evaluate the eventMethod from the current column and
fire it with the created eventArgs
var result = nitobi.event.evaluate
(tmpCell.getColumnObject().getOnCellValidateEvent(),tmpEventArgs);
//increment the counter (for the prepared data array)
columnCounter++;
if(!result) return false;
}
columnCounter = 0;
//increment the counter (for the prepared data array)
rowCounter++;
}
return true;
}

Regards,
Mangesh Gurav

Mangesh

unread,
Sep 8, 2009, 1:58:04 AM9/8/09
to completeui
Hi Robert,

I have couple of more queries. Since you did similar work, probably
you may have answer for those.
Answer from anybody else is also highly welcomed.

1. What you did in your oncellvalidateevent? Currently I am showing
alert message and returning false from javascript function. Instead, I
want to change the backcolor of the defaulter cells and show some
popup (may be nitobi callout / hint) onmouseover of that cell. I
changed backcolor of cell by using below function and the changed
color appears on UI properly.

function setErrorBg(cellobj)
{
cellobj.getStyle().backgroundColor = '#FEA5A5';
}

But after this, if I do cellobj.getStyle().backgroundColor, it returns
the old color and not '#FEA5A5' which I set in above function.

2. I couldn't find any onmouseover event for nitobi cell. Is there any
other similar event?

3. Have you forced same validations on any server button? I need to
validate this data before submission of server request. Any idea how
it can be done?

Thanks and Regards,
Mangesh

Robert Krombholz

unread,
Oct 2, 2009, 4:27:16 PM10/2/09
to completeui
Hi Mangesh,

great to hear that my anser helped you out.

I implemented two ways of validation.

1.) Cell specific validation that sends a validation request to the
server (server sided-validation) after a cell has been edited.
If a value was invalid I painted the background of the cell in red.

2.) A complete Grid validation.
This functionality sends the data of the whole grid for a validation
request to the server before the real save() method is called.
Than I make a decission based on the result whether the save() should
be called or not.

I think I could also help you out with the mouseover stuff.

Sadly I don't have access to this code at the moment.
Could you sent me a reminder mail to RobertK...@gmx.de when you
read this?
(I also not very often visit this group here because of the lack of
activity here).

Regards
Robert

Mangesh

unread,
Oct 20, 2009, 5:34:15 AM10/20/09
to completeui
Hi Robert,

I have mailed you a reminder on the id as per your directions.
Please take out some time and try to send me some stuff if possible as
my project delivery is stucked up because of it.

Best Regards,
Mangesh Gurav

On Oct 3, 1:27 am, Robert Krombholz <RobertKrombh...@gmx.de> wrote:
> Hi Mangesh,
>
> great to hear that my anser helped you out.
>
> I implemented two ways of validation.
>
> 1.) Cell specific validation that sends a validation request to the
> server (server sided-validation) after a cell has been edited.
> If a value was invalid I painted the background of the cell in red.
>
> 2.) A complete Grid validation.
> This functionality sends the data of the whole grid for a validation
> request to the server before the real save() method is called.
> Than I make a decission based on the result whether the save() should
> be called or not.
>
> I think I could also help you out with the mouseover stuff.
>
> Sadly I don't have access to this code at the moment.
> Could you sent me a reminder mail to RobertKrombh...@gmx.de when you
> ...
>
> read more »

Mangesh

unread,
Nov 5, 2009, 12:16:12 AM11/5/09
to completeui
Gentle reminder.
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages