mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-02-16 18:25:12 -06:00
Support setting text fields to empty strings or NULL in the data editor grid. Fixes #1790
This commit is contained in:
parent
c17e201a92
commit
54ba1fbe59
@ -15,7 +15,8 @@
|
||||
"ReadOnlyText": ReadOnlyTextEditor,
|
||||
"ReadOnlyCheckbox": ReadOnlyCheckboxEditor,
|
||||
"ReadOnlypgText": ReadOnlypgTextEditor,
|
||||
"ReadOnlyJsonText": ReadOnlyJsonTextEditor
|
||||
"ReadOnlyJsonText": ReadOnlyJsonTextEditor,
|
||||
"CustomNumber": CustomNumberEditor
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -106,13 +107,36 @@
|
||||
$input.focus();
|
||||
};
|
||||
|
||||
// When text editor opens
|
||||
this.loadValue = function (item) {
|
||||
$input.val(defaultValue = item[args.column.field]);
|
||||
$input.select();
|
||||
if (item[args.column.field] === "") {
|
||||
$input.val("''");
|
||||
}
|
||||
else {
|
||||
$input.val(defaultValue = item[args.column.field]);
|
||||
$input.select();
|
||||
}
|
||||
};
|
||||
|
||||
this.serializeValue = function () {
|
||||
return $input.val();
|
||||
var value = $input.val();
|
||||
// If empty return null
|
||||
if (value === "") {
|
||||
return null;
|
||||
}
|
||||
// single/double quotes represent an empty string
|
||||
// If found return ''
|
||||
else if (value === "''" || value === '""') {
|
||||
return '';
|
||||
}
|
||||
else {
|
||||
// If found string literals - \"\", \'\', \\'\\' and \\\\'\\\\'
|
||||
// then remove slashes.
|
||||
value = value.replace("\\'\\'", "''");
|
||||
value = value.replace('\\"\\"', '""');
|
||||
value = value = value.replace(/\\\\/g, '\\');
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
this.applyValue = function (item, state) {
|
||||
@ -246,6 +270,9 @@
|
||||
};
|
||||
|
||||
this.serializeValue = function () {
|
||||
if ($input.val() === "") {
|
||||
return null;
|
||||
}
|
||||
return $input.val();
|
||||
};
|
||||
|
||||
@ -637,4 +664,76 @@
|
||||
this.init();
|
||||
}
|
||||
|
||||
function CustomNumberEditor(args) {
|
||||
var $input;
|
||||
var defaultValue;
|
||||
var scope = this;
|
||||
|
||||
this.init = function () {
|
||||
$input = $("<INPUT type=text class='editor-text' />");
|
||||
|
||||
$input.bind("keydown.nav", function (e) {
|
||||
if (e.keyCode === $.ui.keyCode.LEFT || e.keyCode === $.ui.keyCode.RIGHT) {
|
||||
e.stopImmediatePropagation();
|
||||
}
|
||||
});
|
||||
|
||||
$input.appendTo(args.container);
|
||||
$input.focus().select();
|
||||
};
|
||||
|
||||
this.destroy = function () {
|
||||
$input.remove();
|
||||
};
|
||||
|
||||
this.focus = function () {
|
||||
$input.focus();
|
||||
};
|
||||
|
||||
this.loadValue = function (item) {
|
||||
defaultValue = item[args.column.field];
|
||||
$input.val(defaultValue);
|
||||
$input[0].defaultValue = defaultValue;
|
||||
$input.select();
|
||||
};
|
||||
|
||||
this.serializeValue = function () {
|
||||
if ($input.val() === "") {
|
||||
return null;
|
||||
}
|
||||
return parseInt($input.val(), 10) || 0;
|
||||
};
|
||||
|
||||
this.applyValue = function (item, state) {
|
||||
item[args.column.field] = state;
|
||||
};
|
||||
|
||||
this.isValueChanged = function () {
|
||||
return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue);
|
||||
};
|
||||
|
||||
this.validate = function () {
|
||||
if (isNaN($input.val())) {
|
||||
return {
|
||||
valid: false,
|
||||
msg: "Please enter a valid integer"
|
||||
};
|
||||
}
|
||||
|
||||
if (args.column.validator) {
|
||||
var validationResults = args.column.validator($input.val());
|
||||
if (!validationResults.valid) {
|
||||
return validationResults;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
valid: true,
|
||||
msg: null
|
||||
};
|
||||
};
|
||||
|
||||
this.init();
|
||||
}
|
||||
|
||||
})(jQuery);
|
||||
|
@ -12,7 +12,8 @@
|
||||
"Formatters": {
|
||||
"JsonString": JsonFormatter,
|
||||
"Numbers": NumbersFormatter,
|
||||
"Checkmark": CheckmarkFormatter
|
||||
"Checkmark": CheckmarkFormatter,
|
||||
"Text": TextFormatter,
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -41,9 +42,13 @@
|
||||
}
|
||||
|
||||
function NumbersFormatter(row, cell, value, columnDef, dataContext) {
|
||||
if (value == null || value === "") {
|
||||
return "";
|
||||
} else {
|
||||
if (_.isUndefined(value) || value === null) {
|
||||
return "<span class='pull-right'>[null]</span>";
|
||||
}
|
||||
else if (value === "") {
|
||||
return '';
|
||||
}
|
||||
else {
|
||||
return "<span style='float:right'>" + value + "</span>";
|
||||
}
|
||||
}
|
||||
@ -55,4 +60,13 @@
|
||||
return value ? "true" : "false";
|
||||
}
|
||||
|
||||
function TextFormatter(row, cell, value, columnDef, dataContext) {
|
||||
if (_.isUndefined(value) || value === null) {
|
||||
return "<span class='pull-left'>[null]</span>";
|
||||
}
|
||||
else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
})(jQuery);
|
||||
|
@ -630,7 +630,7 @@ define(
|
||||
: Slick.Editors.ReadOnlyJsonText;
|
||||
options['formatter'] = Slick.Formatters.JsonString;
|
||||
} else if(c.cell == 'number') {
|
||||
options['editor'] = is_editable ? Slick.Editors.Text
|
||||
options['editor'] = is_editable ? Slick.Editors.CustomNumber
|
||||
: Slick.Editors.ReadOnlyText;
|
||||
options['formatter'] = Slick.Formatters.Numbers;
|
||||
} else if(c.cell == 'boolean') {
|
||||
@ -640,6 +640,7 @@ define(
|
||||
} else {
|
||||
options['editor'] = is_editable ? Slick.Editors.pgText
|
||||
: Slick.Editors.ReadOnlypgText;
|
||||
options['formatter'] = Slick.Formatters.Text;
|
||||
}
|
||||
|
||||
grid_columns.push(options)
|
||||
|
Loading…
Reference in New Issue
Block a user