Utilizing ng-grid to pass variables within cellTemplate

My ng-grid is connected to a SharePoint list, but I'm facing an issue where the list returns an ID number instead of the user name when populating a field with a user name.

To solve this issue, I have created a function that converts the ID to a user name:

$scope.getUserName = function (user) {
    var userId = user;
    $Service.getUserById($scope, userId).done(function (data) {
        $scope.userName = data.d.Title;
    })
}

The problem arises when calling the function in a "cellTemplate" as I can't send the ID variable to the function:

$scope.gridOptions = {
    loading: true,
    enableSorting: true,
    enableFullRowSelection: true,
    enableRowHeaderSelection: false,
    showGroupPanel: true,
    columnDefs: [
        { name: "Admin", field: "AdministratorId" },
        { name: "User", field: "ApprovedById" },
        { name: "SuperUser", cellTemplate: '<div>{{grid.appScope.getUserName(<pass the id variable here>)}}{{grid.appScope.userName}}</div>' }
    ],
    data: 'gridData'
};

I am looking for guidance on how to extract the userID from 'gridData' and pass it to my function.

Update: Here is the REST call being used:

function getListItem(listName) {

var deferred = $.Deferred();
// query the list in the host web
var url = SPAppWebUrl + "/_api/SP.AppContextSite(@target)" +
"/web/lists/getbytitle('" + listName + "')/items?" +
"@target='" + SPHostUrl + "'";

$.ajax({
    url: url,
    type: "GET",
    headers: { "Accept": "application/json;odata=verbose" }, // return data format
    success: onGetEntriesSuccess,
    error: onoDataCallFailure
});
function onGetEntriesSuccess(data) {
    deferred.resolve(data.d);
}
function onoDataCallFailure(data, errorcode, errorMessage) {
    deferred.reject("Error: " + errorMessage);
}
return deferred;}

Answer №1

Your solution: cellTemplate:

'<div>{{grid.appScope.getUserName(**<insert the id variable here>**)}}{{grid.appScope.userName}}</div>' 

Input id in this format: row.entity.Id // The id is provided in your raw data.

Updated code snippet:

cellTemplate: '<div>{{grid.appScope.getUserName(row.entity.Id)}}{{grid.appScope.userName}}</div>' 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering difficulties importing the angular datepicker feature into my application

I have been attempting to integrate the angular-ui-bootstrap datepicker into my application but I am encountering difficulty in doing so. As part of this process, I am using ES6 and below is the code structure I currently have set up: map/src/app.js imp ...

AngularJS uiSortable - visually indicating the movement of a table cell when dragging an item with the mouse

Utilizing uiSortable (uiSortable) within my application has been quite successful. However, I have encountered an issue where I am unable to highlight the drop cell before actually dropping the item into it. Below is a snippet of my HTML code: <td ui-s ...

Issue with Protractor locating element even though everything appears to be in order

I'm having trouble locating this element with Protractor. It's really frustrating me. I double-checked in the dev console and the element definitely exists. Any suggestions on why it's not being found? <download path="api/backup_jobs/e ...

What is the best way to change a variable in an AngularJS view?

My Request In my application, I have implemented 3 views, each with its own controller. The first view is the home screen, and from there the user can navigate to view 2 by clicking on a div element. On view 2, the user can then move to view 3 by clicking ...

Angular: Choose the label of the currently selected option

I am working with a form that has a select menu displaying options using AngularJS: <form name="myForm" <select ng-model="myModel" ng-options="..."> </form> The output of the select menu looks like this: <select> <option valu ...

Sharing data between pages in Ionic and Angular with POST requests

Currently, I am utilizing Ionic for developing a mobile application and have decided to incorporate very basic authentication (without any security measures) into the app. The process involves validating user credentials against a database through a POST r ...

How can elements be collapsed into an array using the Reactive approach?

Consider this TypeScript/Angular 2 code snippet: query(): Rx.Observable<any> { return Observable.create((o) => { var refinedPosts = new Array<RefinedPost>(); const observable = this.server.get('http://localhost/ra ...

The audio stops when the ngaudio screen is clicked

When it comes to playing audio using the ngAudio service in AngularJS 1, the following code snippet is commonly used: $scope.audio = ngAudio.load("abc.wav"); $scope.audio.play(); However, some users have reported that after the sound is played, clicking ...

AngularJS - Swipe to update

I've been trying to use the pull to refresh plugin for Angular JS, but unfortunately it's not working for me. Even though I can see the text, when I try to pull nothing happens! I followed all the steps outlined on this GitHub page: https://githu ...

Tips for implementing jQuery .stop() in animations toggling

Just finished a demo of my project. I recommend checking out the JSFiddle to see exactly what's happening. I've also included the code below. HTML: <div ng-app="slideApp" ng-controller="slideCtrl"> <input type="button" value="Slid ...

Validating date inputs with ng-change in AngularJS

I am currently utilizing AngularJS along with AngularJS bootstrap within my webpage. One of the components I have is a date picker directive that is structured like this: <div class="form-group {{dateStatus.class}}"> <p class="input-g ...

Utilize the scope for promise .then() functions when calling a service

I've just started using AngularJS and I have a question about handling promises in controllers. In my controller, I'm calling a service that communicates with a webservice and returns a promise. I want to apply the data from the promise's s ...

Rewrite Rule for Redirecting to index.html or admin.html Using Htaccess

I have been using the routing module of AngularJS for my WebApp and it has been working wonderfully. However, I encountered an issue when trying to add an admin interface. My directory structure looks like this: index.html admin.html Various AngularJS f ...

Dropdown menu not populating with options in AngularJS ngOptions

It's puzzling to me why the dropdown menu is not being populated by ng-options. Despite JSON data being returned from the service and successfully logged in the controller, ng-options seems to be failing at its task. <tr class="info"> <td ...

"Discover the latest feature in the new Angular router: automatic scrolling functionality

The old ui-router and 1.4 angular router used to support autoscroll="true" to automatically scroll the page to the top when navigating to another route. Is there a way to achieve this with the latest new angular router? It seems like ng-outlet does not ha ...

What is the most popular method for namespacing AngularJS modules?

I am new to AngularJS and currently exploring different ways to namespace modules in my application. One challenge I face is the need to integrate my Angular app into a designated placeholder div within a third-party website (which may also use Angular), ...

Ways to conceal the ng-click attribute within the document object model

I am diving into the world of AngularJS and trying to expand my knowledge. I've encountered a roadblock where I need to trigger a click event without using ng-click="myFunction()". Ideally, I want to keep the function being called hidden. <input t ...

Angular 1.4.8 Issue: [$injector:modulerr]

I can't seem to identify the main cause of this error. I consistently see a green underline below the word angular in my javascript file. I'm not sure why. (Using Visual Studio Code) HTML <html ng-app="myapp"> <head> ...

AngularJS Firebase Login Scope Value Not Retained After Refresh

I have stored my unique username in the variable "$scope" and I am trying to access it from different views once the user logs in, using: However, when I refresh the page immediately after the user successfully signs in, the value of "$scope" goes bac ...

npm encounters difficulty in initiating the server

I encountered some errors after running npm start in my angular project directory. It was working fine yesterday, but today when I entered the npm start command in my cmd prompt, it started showing errors: This is how my package.json file looks like: { ...