簡體   English   中英

AngularJS 1.4.4:ng-click在網格中不起作用

[英]AngularJS 1.4.4: ng-click not working in grid

我在angularJS 1.4.4中有一個簡單的Web UI構建。 我陷入UI網格中的click事件的基本問題。 單擊網格中的眼睛按鈕和文件按鈕時,不會調用控制器中定義的successnotification()函數。 而對於刷新按鈕,單擊按鈕可以正常工作。 下面是我的HTML和Controller代碼

class AccesspointListController {
  /*@ngInject*/
  /* Read me documentation for Grid : http://www.ag-grid.com/documentation.php */
  constructor($rootScope, GridFilters,AccesspointListService, SnapshotController) {
    this.name = 'accesspointlist';

    Object.assign(this, {$rootScope, GridFilters,AccesspointListService, SnapshotController});

    this.columnDefs = [
        {
            headerName: "Sr No.",
            //field: "accessPointDetailsId",
            field: "no",
            width:15,
            filter: 'number', 
            filterParams: { apply: true },
            cellStyle:{'text-align': 'center'},
            unSortIcon:true
        },    
        {
            headerName: "IP",
            //field: "accessPointIP",
             field: "ip",
            filter:'text',
            width:80,
            unSortIcon:true
        },
        {
            headerName: "Actions",
            field: "", 
            width:35,
            suppressSorting: true, 
            suppressMenu:true,
            cellRenderer: function(params) {
                return '<button class="btn primary" ng-click="grid.appScope.successnotification(row)"><i class="fa fa-eye"></i></button>'+
                '<button class="btn primary" ng-click="grid.appScope.vm.successnotification(row)"><i class="fa fa-file-image-o"></i></button>';
            }
        }
    ];
this.allOfTheData = require("json!./favCFIC.json")['items'];
    this.pageSize = '10';
    this.gridOptions = {
        columnDefs: this.columnDefs,
        //rowData: $scope.allOfTheData,//Load data here for static non paging data.
        groupHeaders: false,
        enableColResize: false,
        enableSorting: true,
        suppressRowClickSelection: true,
        headerHeight:40,
        rowHeight:40,
        angularCompileHeaders: true,
        angularCompileFilters: true,
        enableFilter: true,
        icons: {
            // use font awesome for menu icons
            sortAscending: '<i class="fa fa-sort-amount-asc"/>',
            sortDescending: '<i class="fa fa-sort-amount-desc"/>'
        }
    };

  }//end constructor

loadData() {
            let allOfTheData = this.allOfTheData;
                if(allOfTheData.length==0) {
                    $rootScope.alerts.push({
                        type: 'danger',
                        msg: 'There is an issue building the data table.',
                        targetState: 'forms'
                    });
                } else {
                    let dataSource = {
                        rowCount: (this.allOfTheData.length),
                        pageSize: parseInt(this.pageSize), // changing to number, as scope keeps it as a string
                        getRows: function (params) {
                            // this code should contact the server for rows. however for the purposes of the demo,
                            // the data is generated locally, a timer is used to give the experience of
                            // an asynchronous call
                            setTimeout(function() {
                                // take a chunk of the array, matching the start and finish times
                                let rowsThisPage = allOfTheData.slice(params.startRow, params.endRow);
                                // see if we have come to the last page. if we have, set lastRow to
                                // the very last row of the last page. if you are getting data from
                                // a server, lastRow could be returned separately if the lastRow
                                // is not in the current page.
                                let lastRow = -1;
                                if (allOfTheData.length <= params.endRow) {
                                    lastRow = allOfTheData.length;
                                }
                                params.successCallback(rowsThisPage, lastRow);
                            }, 500);
                        }
                    };
                    this.gridOptions.api.setDatasource(dataSource);
                    this.gridOptions.api.sizeColumnsToFit()

    }

successnotification(){
        this.notf1.show('Success! You\'ve clicked the Add button.', "success");
        };
}

export default AccesspointListController;

這是我的html文件。

<section class="container">
    <div class="col-md-10 cf">
    <div ncy-breadcrumb></div>

    <br />
<div id="notificationSpot">
</div>
<br />
<br class="cf"/>
    <div class="col-sm-8">
<div class="panel panel-default" id="content-formatting">
    <div class="panel-heading" align="right">
        <label id="lastUpdated">Last Updated : </label>
        <label id="lastUpdatedValue">19/2/2018 12:20 AM </label>
        &nbsp;&nbsp;
        <button type="button" class="btn 0" ng-click="vm.redirect()"><i class="fa fa-refresh"></i></button>
    </div>
    <div ag-grid="vm.gridOptions" class="ag-fresh" style="clear:both; height: 430px; width:100%;" ng-init="vm.loadData()"></div>

</div>
    </div>
<span kendo-notification="vm.notf1" k-position="{ top: 110}" ></span>
<span kendo-notification="vm.notf2" k-append-to="'#notificationSpot'" k-auto-hide-after="0"></span>
    </div>
</section>

解決方案:按照下面的代碼片段修改單元格渲染器代碼

 cellRenderer: function(params) {
                   return $compile('<a href="" class="btn primary" ng-click="vm.successnotification()"><i class="fa fa-eye"></i></a>')($scope)[0];
                }

您可以在ag-grid中定義和注冊回調(onSelectionChanged):

var gridOptions = {
    columnDefs: columnDefs,
    suppressRowClickSelection: false, // Important! allow to select the rows
    rowSelection: 'single', // option, if you need only one row selected
    onSelectionChanged: onSelectionChanged  // callback
};

每當更改ag-grid的選擇時,都會觸發此回調。 您可以定義回調:

function onSelectionChanged() {
    var selectedRows = gridOptions.api.getSelectedRows();
    var selectedRowsString = '';
    selectedRows.forEach( function(selectedRow, index) {
        if (index!==0) {
            selectedRowsString += ', ';
        }
        selectedRowsString += selectedRow.athlete;
    });
    document.querySelector('#selectedRows').innerHTML = selectedRowsString;
}

該信息取自官方文檔: 鏈接到文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM