Showing a unique boolean value in an Angular ui-grid

Hey there! I'm diving into the world of angular and angular ui-grid. I've set up my project using angularjs(v1.4) with angular-ui-grid(v3.0.7).

Here's how I've defined my grid:

seec.gridOptions = {};
  seec.gridOptions.rowEditWaitInterval = -1;
  seec.gridOptions.onRegisterApi = function (gridApi) {
    $scope.gridApi = gridApi;
    gridApi.rowEdit.on.saveRow($scope, $scope.saveRow);
  };

seec.gridOptions.columnDefs = [
    {name: 'pouch', displayName: 'Pouch', enableCellEdit: false, enableHiding: false, width: 250},
    {name: 'content', displayName: 'Content', enableHiding: false, width: 150},
    {
      name: 'units',
      displayName: 'Number of Items',
      type: 'number',
      enableHiding: false,
      width: 150
    },
    {name: 'active', displayName: 'Status', type: 'boolean', enableHiding: false, width: 150}
  ];

The controller fetches data from an http call and populates the grid.

if (response.status === 200) {
    seec.gridOptions.data = angular.copy(seec.data);
}

Currently, the boolean values in the grid are displayed as 'true' or 'false', with a checkbox appearing when double-clicked.

I want to show 'active' for true and 'inactive' for false. Is there a way to achieve this in angular ui-grid?

Answer №1

There is definitely a way to achieve this! One method you can try is using a cellTemplate and assigning different values to your rows.

I have put together a demonstration on Plunkr to show you how it can be implemented.

Here are the two necessary steps. Firstly, insert a cellTemplate into your column:

cellTemplate: "<div ng-bind='grid.appScope.mapValue(row)'></div>"

Please note that instead of ng-bind, you could also utilize

"<div>{{grid.appScope.mapValue(row)}}</div>"
if that is more comfortable for you.

The second step involves defining your mapping function, like so:

appScopeProvider: {
  mapValue: function(row) {
    // console.log(row);
    return row.entity.active ? 'active' : 'inactive';
  },
}

Answer №2

Hey @CMR, I appreciate you sharing the Plunkr with me. Upon reviewing it, I noticed that the mapValue function may be unnecessary in this scenario.

I found that the following code worked for me:

cellTemplate: "<div class='ui-grid-cell-contents'>{{row.entity.active ? 'active' : 'inactive'}}</div>"

(I included the class to align with the other cells). However, I must admit that this solution still feels a bit makeshift to me.

It raises the question of using a function as the field itself: In ui-grid, I want to use a function for the colDef's field property. How can I pass in another function as a parameter to it?

Personally, I would prefer to see an answer that incorporates the logic directly into the columnDefs.

Answer №3

To apply an Angular filter to a specific column, you need to specify it in your columnDef like this:

cellFilters : 'yourfiltername:args'
.

The "args" can either be a variable or a value. If it's a string, make sure to use the correct quoting like this:

cellFilters : 'yourfiltername:"active"'

Your filter can be a direct function or a filter name. Check out this example on Plunkr for more information.

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

`The value of an element within an array changes automatically`

In my current setup, I have a traditional array where each element represents an HTML element. The issue arises when I manipulate these elements within the array; any changes made to the HTML element also reflect in the array automatically. However, I pref ...

This combination of technologies includes jQuery, Shadowbox, and AJAX

Looking to utilize AJAX along with Shadowbox for loading content dynamically. If JavaScript is disabled, I want the user to be redirected to a specific page instead. Here's what I aim to achieve: Prevent the default click event so that users do ...

The value of ng-repeat list in AngularJS does not update when its value is changed by an ajax call

I am completely perplexed. Why doesn't my ng-repeat update when there is an ajax call that changes its value? I have searched through many questions and answers here, but none of them address the issue with the ajax call. Here is the HTML: <div c ...

Transforming ASP.NET MVC IEnumerable view model into a JSON array of elements

My goal is to develop a dynamic calendar in ASP.NET MVC that pulls event data from a database to populate it. Right now, the calendar is set up to read a json array of objects, but I am facing an issue with converting my ViewModel data into a format that t ...

Not all comparisons between two tables are guaranteed to be successful

Looking to compare records within two arrays and apply a style if they are equal. Here is my approach: var json = ["VIP Section","Master Section","Press Section","God Section"]; var sections = ["VIP Section","Master Section"]; if ( sections.length &g ...

Issue with Node.js MongoDb query results in the retrieval of all documents instead of the specific

Example of Json Structure: {"address": {"building": "1007", "coord": [-73.856077, 40.848447], "street": "Morris Park Ave", "zipcode": "10462"}, "borough": "Bron ...

"The reactivity of VueJs array of objects is not triggering properly when there is a change in

My state includes an array of objects structured like this: data() { return { users: [{id: 1, name: 'bob'}, {id: 2, name: 'bill'}] } } After modifying the data as shown below: this.users[0].name = 'Mary' The watc ...

Box2dweb: Interaction Point of Collision

Currently working with box2dweb for a game development project. Need assistance in determining the contact point between a "Circle" and "Box". Aware that this can be achieved through b2ContactListener and the Post-Solve Event. Seeking guidance on impleme ...

Implement React-intl into the designated placeholder within the object

Currently facing an issue with the const inputProps in my code. I attempted to integrate React-intl into the react-autosuggest placeholder input, but now the placeholder text displays as: [Object object]. The getStepContent function is defined as follow ...

Backend data displayed on carousel presents either all images or none at all

I am currently working on a Django project that involves displaying a list of images in a Carousel format within my template. I have encountered an issue with setting the active class for the Carousel items. When I include the "carousel-inner active" clas ...

The function driver.switchTo.frame() does not exist

I am a beginner with Selenium and facing an issue with a task I need to accomplish: Go to https://pastebin.com Paste "Hello from WebDriver" Set the paste expiration to 10 Minutes //Struggling with this step Set the paste title as "helloweb" I am using Ja ...

What steps should be taken to resolve the Error encountered while testing Angular Js in netbeans?

After trying to implement the steps outlined in the link below I keep encountering the following error: Exception in thread "main" java.lang.NoClassDefFoundError: com/google/jstestdriver/hooks/TestListener at java.lang.Class.getDeclaredMethods0(Nativ ...

What are the best practices for iterating through asynchronous generator functions?

Suppose we have an asynchronous generator: exports.asyncGen = async function* (items) { for (const item of items) { const result = await someAsyncFunc(item) yield result; } } Can we apply mapping to this generator? In essence, I am attempting ...

struggling with utilizing ajax for outputting data using PHP and POST variables

Currently, I am attempting to execute a PHP script that retrieves data from a database by simply clicking a button. My intention is to implement AJAX in order to prevent the page from refreshing. While testing with traditional post/submit methods and enc ...

Passing events between sibling components in Angular 2Incorporating event emission in

Having some trouble emitting an event from one component to another sibling component. I've attempted using @Output, but it seems to only emit the event to the parent component. Any suggestions? ...

Finding the IP address from a hostname in Node.js: A step-by-step guide

I am looking for a solution to resolve a hostname defined in the hosts file to its corresponding IP address. Take, for instance, my hosts file located at "/etc/hosts": 127.0.0.1 ggns2dss81 localhost.localdomain localhost ::1 localhost6.localdomain ...

Obtain the appropriate selection in the dropdown based on the model in Angular

I am working on a dropdown menu that contains numbers ranging from 1 to 10. Below is the HTML code for it: <div class="form-group"> <label>{{l("RoomNumber")}}</label> <p-dropdown [disab ...

What steps should be taken to transform this client-side code into server-side code?

I recently encountered a situation where I needed to convert client-side code into server-side code. Here is an example of what I did: strHTMLGrid = strHTMLGrid + "<link rel='shortcut icon' href='/EVServer/Images/favicon.ico'/> ...

Encountering issues with HMR after relocating files to the /app directory

Greetings, I am currently in the process of learning how to utilize express with webpacks HMR feature. However, every time I make changes and save a file, I encounter the following error: "The following modules couldn't be hot updated: (Full reload n ...

Sending chosen choice to controller method

I'm working with a table that contains inputs and angular models. <td> <select class="form-control" id=""> <option ng-model="mfrNo" ng-repe ...