Giter VIP home page Giter VIP logo

ej2-angular-dynamic-theme-switch's Introduction

Themeswitcher

This project was generated with Angular CLI version 6.0.3.

Create an Angular application

To create an angular application, refer to getting started document.

Add Syncfusion Components

Add the below code in app.component.html file.

<h2> Theme Switcher </h2>
<div>
  <ejs-dropdownlist id='ddlelement' (change)='onChange($event)' #theme switcher [fields]='fields' [dataSource]='data1' placeholder='Select a theme'>
  </ejs-dropdownlist>
  <br>
  <br>

  <ejs-grid [dataSource]='data' [allowPaging]="true" [allowSorting]="true" [pageSettings]="pageSettings">
    <e-columns>
      <e-column field='OrderID' headerText='Order ID' textAlign='Right' width=90></e-column>
      <e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
      <e-column field='Freight' headerText='Freight' textAlign='Right' format='C2' width=90></e-column>
      <e-column field='OrderDate' headerText='Order Date' textAlign='Right' format='yMd' width=120></e-column>
    </e-columns>
  </ejs-grid>
  <hr/>
  </div>

The onChange function in the dropdownlist component gets triggered, whenever the theme is changed.

Add Style Tag

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Ej2 - Themeswitcher</title>
  <base href="/">
  <link href="https://cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">

  // adding style tag
  <style id="theme"></style>
  
</head>
<body>
  <app-root></app-root>
</body>
</html>

Dynamic theme change

To achieve dynamic theme change, read the stylesheet/CSS file of the selected theme and apply these styles to DOM element using AJAX.

Add the below code in app.component.ts file.

  public onChange(e: any): void {
    if (e && e.value) {
      let ajax: Ajax = new Ajax('assets/styles/' + e.value + '.css', 'GET', true);
      ajax.send().then((result: any) => {
        let styleTag = document.getElementById('theme');
        // styleTag.innerHTML = '';
        styleTag.innerHTML=`/*${e.value}*/` + result;
      });
    }
  }

Whenever the onChange function is triggered, the respective CSS file of the selected theme is applied to the DOM elements.

Run the Application

Use the following command to run the application in browser.

npm start

Further help

To get more help on the Angular CLI use ng help or go check out the Angular CLI README.

ej2-angular-dynamic-theme-switch's People

Contributors

banupriyaselvaraj avatar jamunasundaramsf3699 avatar mydeen-sn avatar sarubala20 avatar vinothkumar-ganesan avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.