RadGrid batch edit original value blank validation exception

1 Answer 86 Views
Grid
Michael
Top achievements
Rank 1
Michael asked on 09 Nov 2023, 02:41 PM

I have a RadGrid using BatchEdit mode. The automated validation is working fine, but I want to program in an exception: blank values are not allowed for fields, but the database is full of records with existing blank values. I want the validation to accept a blank value if the existing record already has a blank value.

Here is my validation method. Note that the attribute OriginalValue doesn't exist; it's part of my trying to find the original value, but adding it turned out to be problematic.

        function BlankFieldValidator(sender, args) {
            if (args.Value.length == 0) {
                if (sender.getAttribute("OriginalValue").length != 0) {
                    args.IsValid = false;
                }
            }
        }

In debugging the page, I can examine the sender and args objects, but I see nothing that contains the original value in it. I conclude it must be in the Telerik data structure somewhere, since one can still click Cancel Changes to revert all changes to the RadGrid.

What is a good method for retrieving the original value to compare to the edited value?

1 Answer, 1 is accepted

Sort by
0
Vasko
Telerik team
answered on 14 Nov 2023, 09:51 AM

Hello Michael,

To achieve the desired behavior, I suggest trying the following approach:

  • In the Grid markup, add the onBatchEditOpened and onBatchEditCellValueChanged client-side events: 
    // Omitted for clearance
        <ClientSettings>
            <ClientEvents OnBatchEditOpened="onBatchEditOpened" OnBatchEditCellValueChanged="onBatchEditCellValueChanged" />
        </ClientSettings
    
  • In the <script> tag, add the following logic in order to compare the original and edited values: 
    <script>
        var originalValue // Declare the original and edited values of the cell
        var editedValue
    
        var originalValues = {} // Store the values in objects so we can access them later
        var editedValues = {}
    
        function onBatchEditOpened(sender, args) {
            var rowId = args.get_row() // Assuming you have a way to uniquely identify each row, such as an ID
            var cell = args.get_cell(); // Get the cell being edited
            var textConttent = $(cell)[0].textContent.trim("/")  // Get the cell's text content
    
            var columnUniqueName = args.get_columnUniqueName(); // Get the clicked column's unique name
    
            if (!originalValues[rowId]) {
                originalValues[rowId] = {}; // Store the original value
            }
            originalValues[rowId][columnUniqueName] = textConttent
    
            originalValue = originalValues[rowId][columnUniqueName]
    
            console.log(originalValue)
        }
    
        function onBatchEditCellValueChanged(sender, args) {
            var rowId = args.get_row() // Do the same thing, but for the new value 
            var cell = args.get_cell(); 
            var textConttent = $(cell)[0].textContent.trim("/") 
    
            var columnUniqueName = args.get_columnUniqueName();
    
            if (!editedValues[rowId]) {
                editedValues[rowId] = {};
            }
            editedValues[rowId][columnUniqueName] = textConttent
    
            editedValue = editedValues[rowId][columnUniqueName]
    
            console.log(editedValue)
    
            compareValues() // Call the function whenever you have changed the value of the cell
        }
    
        function compareValues() {
            if (originalValue && editedValue) {
                // Your logic here
            }
        }
    </script>

Give the aforementioned method a try and tell me if it's of use to you.

Kind regards,
Vasko
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources
Michael
Top achievements
Rank 1
commented on 14 Nov 2023, 01:28 PM

It seems ridiculous to make an additional in-memory copy of all of the data when the data is already in the grid. I'm a big fan of data normalization.

I ended up with a different solution which is still not ideal, but seems to work so far in testing: using the OnBatchEditCellValueChanging method of ClientEvents, with a much shorter Javascript function:

        function CheckForBlankValue(sender, args) {
            var columnName = args._columnUniqueName;
            if (args.get_editorValue().length == 0 && columnName != "Weight") { //Weight has separate validation, and blank turns into the minumum value
                if (args._cellValue.length > 0) {
                    args.set_cancel(true);
                    // Show error message for blank
                    alert(`${columnName} is not allowed to be blank.`);
                    return;
                }
            }
It would be nicer to pop up "Required!" in red by validation, but otherwise the functionality works for the use case.
Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Vasko
Telerik team
Share this question
or