2017-01-31 12 views
3

gridstack.ioを使用して、特定の領域に「ウィジェットを追加する」ボタンを動的に作成したいと考えています。私はグリッドに動的に追加したいコンポーネントを持っています。指示文で新しいgridstackitemを追加するには?

私のグリッドスタックの定義は次のようになります。クリックすると、左上隅に新しいディレクティブが追加されるようなボタンを追加するとどうすればよいですか?

グリッドスタックdirective.ts:

import { Directive, OnInit, Input, ElementRef, Renderer } from '@angular/core'; 
declare var $: any; // JQuery 

@Directive({ 
    selector: '[gridStack]' 
}) 
export class GridStackDirective implements OnInit { 
    @Input() w: number; 
    @Input() animate: boolean; 

    constructor(
     private el: ElementRef, 
     private renderer: Renderer 
    ) { 
     renderer.setElementAttribute(el.nativeElement, "class", "grid-stack"); 
    } 

    ngOnInit() { 
     let renderer = this.renderer; 
     let nativeElement = this.el.nativeElement; 
     let animate: string = this.animate ? "yes" : "no"; 

     renderer.setElementAttribute(nativeElement, "data-gs-width", String(this.w)); 
     if(animate == "yes") { 
      renderer.setElementAttribute(nativeElement, "data-gs-animate", animate); 
     } 

     let options = { 
      cellHeight: 80, 
      verticalMargin: 10 
     }; 

     // TODO: listen to an event here instead of just waiting for the time to expire 
     setTimeout(function() { 
      $('.grid-stack').gridstack(options); 
     }, 300); 
    } 

} 

グリッドスタック項目-directive.ts:

import { Directive, ElementRef, Input, Renderer, OnInit } from '@angular/core'; 

@Directive({ 
    selector: '[gridStackItem]' 
}) 

export class GridStackItemDirective { 
    @Input() x: number; 
    @Input() y: number; 
    @Input() w: number; 
    @Input() h: number; 
    @Input() minWidth: number; 
    @Input() canResize: boolean; 

    constructor(
    private el: ElementRef, 
    private renderer: Renderer 
) { 
    renderer.setElementAttribute(el.nativeElement, "class", "grid-stack-item"); 
    } 

    ngOnInit(): void { 
    let renderer = this.renderer; 
    let nativeElement = this.el.nativeElement; 
    let cannotResize: string = this.canResize ? "yes" : "no"; 
    let elementText: string = '<div class="grid-stack-item-content">' + nativeElement.innerHTML + '</div>'; 
    // TODO: Find the Angular(tm) way to do this ... 
    nativeElement.innerHTML = elementText; 

    renderer.setElementAttribute(nativeElement, "data-gs-x", String(this.x)); 
    renderer.setElementAttribute(nativeElement, "data-gs-y", String(this.y)); 
    renderer.setElementAttribute(nativeElement, "data-gs-width", String(this.w)); 
    renderer.setElementAttribute(nativeElement, "data-gs-height", String(this.h)); 
    if(this.minWidth) { 
     renderer.setElementAttribute(nativeElement, "data-gs-min-width", String(this.minWidth)); 
    } 
    if(cannotResize == "yes") { 
     renderer.setElementAttribute(nativeElement, "data-gs-no-resize", cannotResize); 
    } 
    } 
} 
app.component.html: 

<h1>My First Grid Stack Angular 2 App</h1> 
<section id="demo" class="darklue"> 
    <div class="container"> 
     <div class="row"> 
      <div class="col-lg-12 text-center"> 
       <h2>Demo</h2> 
       <hr class="star-light"> 
      </div> 
     </div> 
     <div gridStack w="12" animate="true"> 
      <div gridStackItem x="0" y="0" w="4" h="2">1</div> 
      <div gridStackItem x="4" y="0" w="4" h="4">2</div> 
      <div gridStackItem x="8" y="0" w="2" h="2" canResize="false" minWidth="2"> 
       <span class="fa fa-hand-o-up"></span> Drag me! 
      </div> 
      <div gridStackItem x="10" y="0" w="2" h="2">4</div> 
      <div gridStackItem x="0" y="2" w="2" h="2">5</div> 
      <div gridStackItem x="2" y="2" w="2" h="4">6</div> 
      <div gridStackItem x="8" y="2" w="4" h="2">7</div> 
      <div gridStackItem x="0" y="4" w="2" h="2">8</div> 
      <div gridStackItem x="4" y="4" w="4" h="2">9</div> 
      <div gridStackItem x="8" y="4" w="2" h="2">10</div> 
      <div gridStackItem x="10" y="4" w="2" h="2">11</div> 
     </div> 
    </div> 
</section> 

index.htmlを

私は、次のディレクティブを持っています:

<html> 
    <head> 
    <title>Angular QuickStart</title> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="styles.css"> 
    <link rel="stylesheet" href="node_modules/font-awesome/css/font-awesome.min.css"> 

    <link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css"> 
    <link href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic" rel="stylesheet" type="text/css"> 
    <link href="https://fonts.googleapis.com/css?family=Indie+Flower" rel='stylesheet' type='text/css'> 

    <!-- 1. Load libraries --> 
    <!-- Polyfill(s) for older browsers --> 
    <script src="node_modules/core-js/client/shim.min.js"></script> 
    <script src="node_modules/zone.js/dist/zone.js"></script> 
    <script src="node_modules/reflect-metadata/Reflect.js"></script> 
    <script src="node_modules/systemjs/dist/system.src.js"></script> 

    <!-- 2. Configure SystemJS --> 
    <script src="systemjs.config.js"></script> 
    <script> 
     System.import('app').catch(function(err){ console.error(err); }); 
    </script> 

    <!-- jquery --> 
    <script src="node_modules/jquery/dist/jquery.js"></script> 
    <script src="node_modules/jquery-ui-dist/jquery-ui.min.js"></script> 
    <script src="node_modules/jquery-ui-touch-punch/jquery.ui.touch-punch.min.js"></script> 
    <script src="node_modules/jquery-easing/dist/jquery.easing.1.3.umd.min.js"></script> 

    <!-- underscore and gridstack --> 
    <script src="node_modules/underscore/underscore-min.js"></script> 
    <script src="node_modules/gridstack/dist/gridstack.js"></script> 
    <link rel="stylesheet" href="node_modules/gridstack/dist/gridstack.min.css"> 
    <link rel="stylesheet" href="node_modules/gridstack/dist/gridstack-extra.min.css"> 
    <link rel="stylesheet" href="app/css/gridstack-demo.css"> 

    <!-- bootstrap --> 
    <script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script> 
    <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.css"> 

    <!-- freelancer stuff --> 
    <script src="app/js/freelancer.js"></script> 
    <link rel="stylesheet" href="app/css/freelancer.css"> 

    </head> 
    <!-- 3. Display the application --> 
    <body> 
    <my-app>Loading...</my-app> 
    </body> 
</html> 

答えて

0

「ルートあなたgridStackディレクティブ内部のR」 - それは、文字列を取得する所望の成分の種類を表し、あなただけが各グリッド項目

に必要なコンポーネントをロードしますそのよう ngIf

<div customGridStackItem1 *ngIf="itemType == 'stackItem1'"></div> 
<div customGridStackItem2 *ngIf="itemType == 'stackItem2'"></div> 
<div anotherGridStackItem *ngIf="itemType == 'anotherStackItem'"></div> 

でそれを読み込みます

関連する問題