I am using
a kendo grid for user input and for each parameter I have a max and min value.
I managed to implement a validation so that the user cannot input a value outside the
limits and if a value outside the range is entered there is a text shown “Value
must be between <max> and <min>”. This was done via a validation
function.
Now to be
more flexible with number of decimals, I added a number editor to the input
column. After this it is still not possible for the user to enter a value
outside the range, which is good, but the text is not showing. Why is this and
what can I do to get this text back although I am using a number editor?
this
.myDataSource =
new
kendo.data.DataSource({
autoSync:
true
,
data: [
{ label:
"Parameter1"
, ref: 89.82, min: 60, max: 100, isInput:
true
},
{ label:
"Parameter2"
, ref: 9.18, min: 8, max: 20, isInput:
false
}
],
schema: {
model: {
fields: {
label: { type:
"string"
, editable:
false
},
ref: {
validation: {
required:
true
,
validateFunction:
function
(isInput) {
return
OfflinePredictionController.validateInput(
"#myGrid"
, isInput);
}
}
},
min: { type:
"number"
, editable:
false
},
max: { type:
"number"
, editable:
false
},
isInput: { type:
"boolean"
, editable:
false
}
}
}
},
});
$(
"#myGrid"
).kendoGrid({
dataSource:
this
.myDataSource,
scrollable:
false
,
editable:
true
,
columns: [
{
field:
"label"
,
title:
"Parameter"
,
},
{
field:
"ref"
,
title:
"Reference"
,
editor: numberEditor,
width:
"140px"
,
},
{
hidden:
true
,
field:
"min"
,
width:
"30px"
},
{
hidden:
true
,
field:
"max"
,
width:
"30px"
},
{
hidden:
true
,
field:
"isInput"
,
width:
"40px"
}
]
});
static validateInput(gridName: string, input: any) {
var
minIndex = 0;
var
maxIndex = 0;
var
grid = $(gridName).data(
"kendoGrid"
);
for
(
var
columnIndex = 0; columnIndex < grid.columns.length; columnIndex++) {
if
(grid.columns[columnIndex].field ==
"min"
) {
minIndex = columnIndex;
}
else
if
(grid.columns[columnIndex].field ==
"max"
) {
maxIndex = columnIndex;
}
}
var
reference = $(input).val();
var
min = parseFloat($(input).closest(
"tr"
).find(
"td:eq("
+ minIndex +
")"
).text());
var
max = parseFloat($(input).closest(
"tr"
).find(
"td:eq("
+ maxIndex +
")"
).text());
if
((reference < min) || (reference > max)) {
input.attr(
"data-validateFunction-msg"
,
"Value must be between "
+ min +
" and "
+ max);
return
false
;
}
return
true
;
}
function
numberEditor(container, options) {
$(
'<input name="'
+ options.field +
'"/>'
).appendTo(container).kendoNumericTextBox({
format:
"{0:n4}"
,
decimals: 4,
step: 0.1,
});
}