Skip to content

Editors Signature Editor using SignaturePad

Victor Tomaili edited this page May 3, 2021 · 1 revision

(by Wesley Huang)

How to add a Signature Editor using SignaturePad

  1. Open a command prompt in the project folder, and run

npm install --save signature_pad

  1. In ScriptBundles.json, add

"~/node_modules/signature_pad/dist/signature_pad.umd.js"

  1. Create a TypeScript file called SignatureEditor.ts
namespace YourProjectName {

    @Serenity.Decorators.element('<div />')
    @Serenity.Decorators.registerEditor([Serenity.IGetEditValue, Serenity.ISetEditValue])
    export class SignatureEditor extends Serenity.Widget<SignatureEditorOptions>
        implements Serenity.IGetEditValue, Serenity.ISetEditValue
    {

        private signaturePad: any;

        constructor(div: JQuery, options: SignatureEditorOptions) {
            super(div, options);

            let canvas = $('<canvas />').attr('id', 'sign_' + this.uniqueName);
            this.element.append(canvas);
            
            this.signaturePad = new SignaturePad(canvas[0]);

            let clearButton = $('<button />').attr('id', 'clearsign_' + this.uniqueName).html('<i class="fa fa-eraser"></i>');
            clearButton.on('click', (e) => {
                e.preventDefault();
                this.signaturePad.clear();
            });
            this.element.append(clearButton);
        }

        public get value(): string {
            return this.get_value();
        }

        protected get_value(): string {
            return this.signaturePad.toDataURL();
        }

        public set value(value: string) {
            this.set_value(value);
        }

        protected set_value(value: string): void {
            this.signaturePad.fromDataURL(value);
        }

        setEditValue(source: any, property: Serenity.PropertyItem): void {
            this.value = source[property.name];
        }
        getEditValue(property: Serenity.PropertyItem, target: any): void {
            target[property.name] = this.value;
        }

    }

    export interface SignatureEditorOptions {
    }
}

BTW, Does any one know how to get rid of red tilde under SignaturePad? Please let me know. Thanks

  1. run T4 template.

Usage

  1. Add a column in table with data type NVARCHAR(MAX)
  2. Add the column in your Row.cs file
  3. Add the column in your Form.cs file as below (assume your column is called 'Signature')
[SignatureEditor]
public String Signature { get; set; }

Image 8

That's all.

https://github.com/volkanceylan/Serenity/issues/4533

Clone this wiki locally