Search code examples
angularjsui-grid

Getting values of the selected row in uiGrid angular


I have a ui-grid, that allows the user selecting one row. If a row is selected I want to bind some fields of the selected row to input fields. How is this possible?

$scope.gridOptions = {
data : items,
columnDefs: [
  { name: 'ITEMNO', field: 'ITEMNO', displayName: 'Pos.', enableHiding : false, width: 75 },
  { name: 'SERIALN_REQ', field: 'SERIALN_REQ', displayName: 'Serialpflichtig', enableHiding : false, width: 175  }
],
enableScrollbars : false,
enableHorizontalScrollbar : 0,
enableVerticalScrollbar : 0,
enableFullRowSelection : true,
enableRowSelection: true,
enableSelectAll: false,
multiSelect : false,
selectionRowHeaderWidth: 0,
rowHeight: 55,
enablePaginationControls: false,
paginationPageSize: 5

};

  $scope.gridOptions.onRegisterApi = function (gridApi) {
    $scope.gridApi = gridApi;
    $scope.gridApi.grid.modifyRows($scope.gridOptions.data);
    $scope.gridApi.selection.selectRow($scope.gridOptions.data[0]);
    var rows = $scope.gridApi.selection.getSelectedRows();
  }

In my HTML i have the following:

<div class="large-2 columns">
    <input type="text" disabled ng-model={{}}> <!-- here i want to bind a cell of the selected grid row -->
  </div><div ui-grid="gridOptions" ui-grid-selection ui-grid-pagination class="myGrid"></div>

Solution

  • Here is your expected one(get selected row values and bind them to the input elements).

    Use the below code

    $scope.rowtobebinded=undefined;
    $scope.gridOptions.onRegisterApi = function(gridApi) {
            $scope.gridApi = gridApi;
            $scope.gridApi.grid.modifyRows($scope.gridOptions.data);
            $scope.gridApi.selection.selectRow($scope.gridOptions.data[0]);
            var rows = $scope.gridApi.selection.getSelectedRows();
    
            //aravind's code to get the selected row elements is as below
            gridApi.selection.on.rowSelectionChanged($scope,function(row){
                    console.log("selected row is ",row);
    
                   //binding the row's no to the textbox
    
                    $scope.rowtobebinded=row.entity.ITEMNO; 
    
            });
    });
    

    LIVE DEMO