Search code examples
javascriptphpjquery-datatables-editor

datatables.js Editor - Replace character in string on create/update


Using datatables.js Editor, I want to replace a character, a semicolon, with &#x3b; when data is submitted to the database.

I am told that this can be done with the setFormatter function:

https://editor.datatables.net/manual/php/formatters

How would I do this? Example would be great.

NOTE: The editor is the PHP API version


Solution

  • The documentation you linked to is for the PHP API, but your question is not tagged with PHP. Assuming you are indeed using PHP you can use str_replace to replace one character with another.

    Field::inst( 'your_field_name' )->setFormatter( function ( $fieldValue, $rowData ) {
        return str_replace(';','&#x3b;',$fieldValue); //replace semicolon with html escape sequence
    } )
    

    Change your_field_name as needed.