Automatically trim the value thats pasted inside a search field

0
Hi all, I have an application with a Datagrid with search fields. When I paste a value (e.g. a postcode) inside one of the search fields, I want the content to be trimmed automatically. Is there a way to achieve this in a datagrid?   Thanks for your help! Kind regards, Tim
asked
2 answers
0

Hi Tim,

This is not supported out of the box with Datagrid. 

You can try workarounds with Javascript.

Regards,

Sharad R K

answered
0

I've used this piece of Javascript to solve it. Add this as a Javascript action in the flow that opens your page.

 

// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
import "mx-global";
import { Big } from "big.js";

// BEGIN EXTRA CODE
// END EXTRA CODE

/**
 * @param {string} classname - Classname of the div(s) containing the inputfields to be trimmed on paste
 * @returns {Promise.<void>}
 */
export async function JavaScript_TrimInputOnPaste(classname) {
	// BEGIN USER CODE

	let fields = document.getElementsByClassName(classname);

	let trimField = function(e) {
  		e.preventDefault();
  		let pastedText = '';
  		if (e.clipboardData && e.clipboardData.getData) {
    		pastedText = e.clipboardData.getData('text/plain').trim();
			e.clipboardData.setData('text/plain', pastedText);
  		} else if (window.clipboardData && window.clipboardData.getData) { // IE
    		pastedText = window.clipboardData.getData('Text').trim();
  		}
  		this.value = pastedText;
	};


	for (let i = 0; i<fields.length; i++) {
		let div = fields[i];
		let childs = div.children;
		for (let j = 0; j<childs.length; j++) {
				let child = childs[j];
				if (child.nodeName == 'INPUT'){				
					child.onpaste = trimField;
				}
		}
	}

	// END USER CODE
}

 

answered