jQWidgets Forums

jQuery UI Widgets Forums Angular JQXGrid with Context menu Sample

This topic contains 1 reply, has 2 voices, and was last updated by  Ivo Zhulev 7 years, 10 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
  • JQXGrid with Context menu Sample #93818

    seanishok
    Participant

    Hi Team,

    I am looking for Angular 2.0 JQXGrid with context menu samples. I can see only sample code for JQuery. Can you please provide me small sample for Angular 2.0 Jqxgrid with context menu on right click of a Row.

    JQXGrid with Context menu Sample #93910

    Ivo Zhulev
    Participant

    Hi seanishok,

    Soon we will have all examples on Angular. For now here is the context menu grid example:

    app.component.ts

    import { Component, ViewChild, AfterViewInit } from '@angular/core';
    
    import { jqxGridComponent } from '../assets/jqwidgets-ts/angular_jqxgrid';
    import { jqxMenuComponent } from '../assets/jqwidgets-ts/angular_jqxmenu';
    import { jqxInputComponent } from '../assets/jqwidgets-ts/angular_jqxinput';
    import { jqxNumberInputComponent } from '../assets/jqwidgets-ts/angular_jqxnumberinput';
    import { jqxWindowComponent } from '../assets/jqwidgets-ts/angular_jqxwindow';
    
    @Component({
        selector: 'app-root',
        templateUrl: './app.component.html'
    })
    
    export class AppComponent implements AfterViewInit {
        @ViewChild('myGrid') myGrid: jqxGridComponent;
        @ViewChild('myWindow') myWindow: jqxWindowComponent;
        @ViewChild('firstName') firstName: jqxInputComponent;
        @ViewChild('lastName') lastName: jqxInputComponent;
        @ViewChild('product') product: jqxInputComponent;
        @ViewChild('quantity') quantity: jqxNumberInputComponent;
        @ViewChild('price') price: jqxNumberInputComponent;
        @ViewChild('myMenu') myMenu: jqxMenuComponent;
    
        ngAfterViewInit(): void {
            document.addEventListener('contextmenu', event => event.preventDefault());
        }
    
        editrow: number = -1;
    
        source: any =
        {
            localdata: generatedata(25),
            datatype: 'array',
            datafields:
            [
                { name: 'firstname', type: 'string' },
                { name: 'lastname', type: 'string' },
                { name: 'productname', type: 'string' },
                { name: 'quantity', type: 'number' },
                { name: 'price', type: 'number' }
            ]
        };
    
        dataAdapter: any = new $.jqx.dataAdapter(this.source);
    
        columns: any[] =
        [
            { text: 'First Name', datafield: 'firstname', width: 200 },
            { text: 'Last Name', datafield: 'lastname', width: 200 },
            { text: 'Product', datafield: 'productname', width: 190 },
            { text: 'Quantity', datafield: 'quantity', width: 90, cellsalign: 'right' },
            { text: 'Price', datafield: 'price', cellsalign: 'right', cellsformat: 'c2' }
        ];
    
        myGridOnContextMenu(): boolean {
            return false;
        }
    
        myGridOnRowClick(event: any): void | boolean {
            if (event.args.rightclick) {
                this.myGrid.selectrow(event.args.rowindex);
                let scrollTop = window.scrollY;
                let scrollLeft = window.scrollX;
                this.myMenu.open(parseInt(event.args.originalEvent.clientX) + 5 + scrollLeft, parseInt(event.args.originalEvent.clientY) + 5 + scrollTop);
                return false;
            }
        };
    
        myMenuOnItemClick(event: any): void {
            let args = event.args;
            let rowindex = this.myGrid.getselectedrowindex();
            if (args.innerHTML == 'Edit Selected Row') {
                this.editrow = rowindex;
                this.myWindow.position({ x: 60, y: 60 });
                // get the clicked row's data and initialize the input fields.
                let dataRecord = this.myGrid.getrowdata(this.editrow);
                this.firstName.val(dataRecord.firstname);
                this.lastName.val(dataRecord.lastname);
                this.product.val(dataRecord.productname);
                this.quantity.decimal(dataRecord.quantity);
                this.price.decimal(dataRecord.price);
                // show the popup window.
                this.myWindow.open();
            }
            else {
                let rowid = this.myGrid.getrowid(rowindex);
                this.myGrid.deleterow(rowid);
            }
        };
    
        SaveBtnOnClick(): void {
            if (this.editrow >= 0) {
                let row = {
                    firstname: this.firstName.val(), lastname: this.lastName.val(), productname: this.product.val(),
                    quantity: parseInt(this.quantity.decimal()), price: parseFloat(this.price.decimal())
                };
                let rowid = this.myGrid.getrowid(this.editrow);
                this.myGrid.updaterow(rowid, row);
                this.myWindow.hide();
            }
        };
    
        CancelBtnOnClick(): void {
            this.myWindow.hide();
        };
    }
    
    

    app.component.html

    <jqxGrid #myGrid (onContextmenu)="myGridOnContextMenu()" (onRowclick)="myGridOnRowClick($event)"
        [width]="850" [source]="dataAdapter" [pageable]="true"
        [autoheight]="true" [columns]="columns">
    </jqxGrid>
    
    <div style="margin-top: 30px">
        <div id='cellbegineditevent'></div>
        <div style="margin-top: 10px" id='cellendeditevent'></div>
    </div>
    
    <jqxWindow #myWindow
        [width]="250" [height]="230" [modalOpacity]="'0.01'" 
        [resizable]="false" [isModal]="true" [autoOpen]="false"> 
        <div>Edit</div>
        <div style="overflow: hidden">
            <table>
                <tbody>
                    <tr>
                        <td align='right'>First Name:</td>
                        <td align='left'><jqxInput #firstName [width]="150" [height]="23"></jqxInput></td>
                    </tr>
                    <tr>
                        <td align='right'>Last Name:</td>
                        <td align='left'><jqxInput #lastName [width]="150" [height]="23"></jqxInput></td>
                    </tr>
                    <tr>
                        <td align='right'>Product:</td>
                        <td align='left'><jqxInput #product [width]="150" [height]="23"></jqxInput></td>
                    </tr>
                    <tr>
                        <td align='right'>Quantity:</td>
                        <td align='left'>
                            <jqxNumberInput #quantity
                                  [width]="150" [height]="23" [decimalDigits]="0" [spinButtons]="true">
                            </jqxNumberInput>
                        </td>
                    </tr>
                    <tr>
                        <td align='right'>Price:</td>
                        <td align='left'>
                            <jqxNumberInput #price
                              [width]="150" [height]="23" [symbol]="'$'" [spinButtons]="true">
                            </jqxNumberInput>
                        </td>
                    </tr>
                    <tr>
                      <td align='right'></td>
                      <td style="padding-top: 10px" align='right'>
                          <jqxButton #SaveBtn style="margin-right: 5px; float: left" (onClick)="SaveBtnOnClick()">Save</jqxButton>
                          <jqxButton #CancelBtn style="float: left" (onClick)="CancelBtnOnClick()">Cancel</jqxButton>
                      </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </jqxWindow>
    
    <jqxMenu #myMenu (onItemclick)="myMenuOnItemClick($event)"
        [width]="200" [height]="58" [mode]="'popup'" [autoOpenPopup]="false">
        <ul>
          <li>Edit Selected Row</li>
          <li>Delete Selected Row</li>
        </ul>
    </jqxMenu>

    app.module.ts

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { FormsModule } from '@angular/forms';
    
    import { AppComponent } from './app.component';
    import { jqxGridComponent } from '../assets/jqwidgets-ts/angular_jqxgrid';
    import { jqxButtonComponent } from '../assets/jqwidgets-ts/angular_jqxbuttons';
    import { jqxMenuComponent } from '../assets/jqwidgets-ts/angular_jqxmenu';
    import { jqxInputComponent } from '../assets/jqwidgets-ts/angular_jqxinput';
    import { jqxNumberInputComponent } from '../assets/jqwidgets-ts/angular_jqxnumberinput';
    import { jqxWindowComponent } from '../assets/jqwidgets-ts/angular_jqxwindow';
    
    @NgModule({
      declarations: [
          AppComponent, jqxGridComponent, jqxButtonComponent, jqxMenuComponent, jqxInputComponent, jqxNumberInputComponent, jqxWindowComponent
      ],
      imports: [
          BrowserModule, CommonModule, FormsModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    
    export class AppModule { }

    Best Regards,
    Ivo

    jQWidgets Team
    http://www.jqwidgets.com/

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.