Search results

Templates in JavaScript Dialog control

30 Jul 2021 / 3 minutes to read

In Dialog the template support is provided to the header and footer sections. So any text or HTML content can be appending in these sections.

The Dialog header content can be provided through the header property, and it will allow both text and any HTML content as a string. Also in header, close button is provided as built-in support, and this can be enabled through the showCloseIcon property.

The Dialog footer can be enabled by adding built-in buttons or providing any HTML string through the footerTemplate.

The buttons and footerTemplate properties can’t be used at the same time.

Content

The Dialog content can be provided through the content property, and it accepts both text and HTML string as content.

The below example demonstrates the usage of header, footer and content templates in the Dialog control.

Source
Preview
app.ts
index.html
styles.css
Copied to clipboard
import { Dialog } from '@syncfusion/ej2-popups';
import { Button } from '@syncfusion/ej2-buttons';
/**
 * Dialog template sample
 */

let icontemp: string = '<button id="sendButton" class="e-control e-btn e-primary" data-ripple="true">' + 'Send</button>';
let headerimg: string = '<img class="img2" style="display: inline-block;" src="https://ej2.syncfusion.com/products/typescript/dialog/template/images/1.png" alt="header image">';
let sendbutton: Button = new Button();
let proxy: any = this;

// Initialize Dialog component
let dialog = new Dialog({
    // Enables the header with template content
    header: headerimg + '<div class="dlg-template" title="Nancy" class="e-icon-settings"> Nancy </div>',
    // Enables the footer with template content
    footerTemplate: ' <input id="inVal" class="e-input" type="text" placeholder="Enter your message here!"/>' + icontemp,
    // Dialog content
    content: document.getElementById("dlgContent"),
    // Enables the close icon button in header
    showCloseIcon: true,
    // The Dialog shows within the target element
    target: document.getElementById("container"),
    //Dialog width
    width: '400px',
    height: '250px',
    beforeOpen: onBeforeopen
});
// Render initialized Dialog
dialog.appendTo('#dialog');
sendbutton.appendTo('#sendButton');

// Sample level code to handle the button click action
document.getElementById('targetButton').onclick = (): void => {
    // Call the show method to open the Dialog
    dialog.show();
}

(document.getElementById('sendButton')as HTMLElement).onkeydown = (e: any) => {
    if (e.keyCode === 13) { updateTextValue(); }
};

(document.getElementById('inVal')as HTMLElement).onkeydown = (e: any) => {
    if (e.keyCode === 13) { updateTextValue(); }
};

document.getElementById('sendButton').onclick = (): void => {
    updateTextValue();
};

function onBeforeopen(): void {
    document.getElementById('dlgContent').style.visibility = 'visible';
}

function updateTextValue () : void {
    let enteredVal: HTMLInputElement = document.getElementById('inVal') as HTMLInputElement;
    let dialogTextElement: HTMLElement = document.getElementsByClassName('dialogText')[0] as HTMLElement;
    let dialogTextWrap : HTMLElement = document.getElementsByClassName('dialogContent')[0] as HTMLElement;
    if (enteredVal.value !== '') {
        dialogTextElement.innerHTML = enteredVal.value;
        enteredVal.value = '';
    }
}
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 Dialog with template support</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="TypeScript UI Components" />
    <meta name="author" content="Syncfusion" />
    <link href="styles.css" rel="stylesheet" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-inputs/styles/material.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
    <div id='loader'>LOADING....</div>
    <div id='container'>
        <button class="e-control e-btn" id="targetButton" role="button" e-ripple="true">Open Dialog</button>
        <div id='dialog' class='custom-template'></div>
        <div id="dlgContent" style="visibility: hidden" class="dialogContent">
            <span class="dialogText">
            Greetings Nancy! When will you share me the source files of the project?
            </span>
        </div>
    </div>
</body>
</html>
Copied to clipboard
html,
body,    
#container {
    height: 100%;
    overflow: hidden;
    width: 100%;
}

#dialog .e-icon-settings::before { /* csslint allow: adjoining-classes */
    padding: 3px;
    font-size: 15px;
}

#loader {
  color: #008cff;
  height: 40px;
  left: 45%;
  position: absolute;
  top: 45%;
  width: 30%;
}

#sendButton { 
    top: 5px;
    position: relative;
    right: 7px;
}

.custom-template.e-dialog .e-dlg-header-content { /* csslint allow: adjoining-classes */
    border-bottom: none;
    padding: 11px;
}

.e-dialog .e-footer-content { /* csslint allow: adjoining-classes */
    right: 7px;
}

.e-dialog .e-dlg-header-content { /* csslint allow: adjoining-classes */
    background-color: #007DD1;
    padding: 10px;
}

.e-dialog .e-dlg-header-content .e-btn.e-dlg-closeicon-btn { /* csslint allow: adjoining-classes */
    top: 5px;
    left: -11px;
}

.e-dialog .e-dlg-header { /* csslint allow: adjoining-classes */
    position: relative;
}

.e-dialog .e-dlg-content { /* csslint allow: adjoining-classes */
    padding: 0;
}

 #sendbtn {
    margin-right: -17px;
    margin-top: -2%;
}

.e-open-icon::before { /* csslint allow: adjoining-classes */
    content: '\e782';
}

.e-dialog .e-dlg-header > .img2 { /* csslint allow: adjoining-classes */
    height: 36px;
    width: 36px;
    border-radius: 50%;
    vertical-align: middle;
}
.e-dialog .e-dlg-header .dlg-template { /* csslint allow: adjoining-classes */
    display: inline-block;
    padding: 0 10px;
    vertical-align: middle;
    height: 30px;
    line-height: 30px;
    color: #fff;
}
.e-dialog .e-dlg-header-content .e-btn .e-btn-icon { /* csslint allow: adjoining-classes */
    margin-top: -3px;
}
.custom-template .e-input { /* csslint allow: adjoining-classes */
    width: 75%;
    float: left;
    height: 18px;
}

.e-icon-settings.e-icons { /* csslint allow: adjoining-classes */
    float: left;
    position: relative;
    left: 16%;
    top: -33px;
}

.e-icon-settings.e-icons { /* csslint allow: adjoining-classes */
    top: -37px;
}

.e-icon-settings.e-icons { /* csslint allow: adjoining-classes */
    top: -35px;
}
.dialogContent .dialogText { /* csslint allow: adjoining-classes */
    font-size: 13px;
    padding: 5%;
    word-wrap: break-word;
    border-radius: 6px;
    text-align: justify;
    font-style: initial;
    display: block;
}

.dialogContent .dialogText { /* csslint allow: adjoining-classes */
    background-color: #f5f5f5;
}

.custom-template.e-dialog .e-footer-content { /* csslint allow: adjoining-classes */
    border-top: 0.5px solid #949494;
    padding-left: 31px;
}

.dialogContent {
    display: block;
    font-size: 15px;
    word-wrap: break-word;
    text-align: center;
    font-style: italic;
    border-radius: 6px;
    padding: 3%;
    position: relative;
    top: 25px;
}

.e-dialog .dialogContent { /* csslint allow: adjoining-classes */
    top: 20px;
}

.control-wrapper .e-control.e-dialog { /* csslint allow: adjoining-classes */
    width: 30%;
}

.e-dialog .e-dlg-header-content .e-icon-dlg-close { /* csslint allow: adjoining-classes */
  color: #fff;
}

.e-dialog .e-btn.e-dlg-closeicon-btn:hover span{ /* csslint allow: adjoining-classes */
    color: #8ECBFF;
  }
/* csslint ignore:start */
.e-dialog .e-dlg-header-content .e-btn.e-dlg-closeicon-btn:hover {
    background-color: rgba(255,255,255, 0.10);
}
.e-dialog .e-dlg-header-content .e-btn.e-dlg-closeicon-btn:focus {
      background-color: rgba(255,255,255, 0.10);
  }
/* csslint ignore:end */  
.e-dialog .e-dlg-header-content .e-btn.e-dlg-closeicon-btn:active .e-icon-dlg-close { /* csslint allow: adjoining-classes */
    color: #fff;
}
.e-dialog .e-dlg-header-content .e-btn.e-dlg-closeicon-btn:focus .e-icon-dlg-close { /* csslint allow: adjoining-classes */
    color: #fff;
}
.e-dialog .e-dlg-header-content .e-btn.e-dlg-closeicon-btn:hover .e-icon-dlg-close { /* csslint allow: adjoining-classes */
      color: #fff;
  }

See Also