Is it possible to generate grid options dynamically for various tables within AngularJS using ui-grid?

I have developed a service for ui-grid table functionality. Currently, I am able to use this service on a single page but now I want to extend its usage to multiple pages, each with different table data. How can I pass grid options and JSON data for multiple tables? Any assistance would be greatly appreciated.

Service:

(function(){
"use strict"; 
angular.module('nApp').controller('SearchResultsController', ['$scope', '$http', '$timeout', '$interval', 'uiGridConstants', 'uiGridGroupingConstants', function ($scope, $http, $timeout, $interval, uiGridConstants, uiGridGroupingConstants) {
    var searchResults = this;
    searchResults.loadPOHeaderView = loadPOHeaderView;
    
    function loadPOHeaderView() {
        if(dataFactory.getSelectedPOHeader().length) {
            $state.go("^.poheader");
        };
    }

    $scope.gridOptions = {
    enableHorizontalScrollbar : uiGridConstants.scrollbars.NEVER,
    enableVerticalScrollbar   : uiGridConstants.scrollbars.NEVER,
    paginationPageSizes: [25, 50, 75],
    paginationPageSize: 10,
    useExternalPagination: true,
    useExternalSorting: true,
    enableRowSelection: false,
    enableCellSelection: false,
    enableFiltering: false,
    enableCellEdit: false,
    enableColumnResizing: true,
    enableColumnMenus: false,
    enableGridMenu: false,
    showGridFooter: false,
    showColumnFooter: false,
    fastWatch: true,
    enablePaging: true,
    showFilter: true,
    rowHeight: 45,
    onRegisterApi: function(gridApi) {
        $scope.gridApi = gridApi;
        $scope.gridApi.core.on.sortChanged($scope, function(grid, sortColumns) {
            if (sortColumns.length == 0) {
                paginationOptions.sort = null;
            } else {
                paginationOptions.sort = sortColumns[0].sort.direction;
            }
                getPage();
        });
        gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
            paginationOptions.pageNumber = newPage;
            paginationOptions.pageSize = pageSize;
            getPage();
        });
    },
    columnDefs : [
    { name:'Test1', displayName:'Test1'},
    { name:'Test2', displayName:'Test2'},
    { name:'Test3', displayName:'Test3'},
    { name:'Test4', displayName:'Test4'},
    { name:'Test5', displayName:'Test5'},
    { name:'Test6', displayName:'Test6'},
    { name:'Test7', displayName:'Test7'}     
    ]
    }; 
      var paginationOptions = {
    pageNumber: 1,
    pageSize: 10,
    sort: null
    }; 
      var getPage = function() {
var url;
switch(paginationOptions.sort) {
  case uiGridConstants.ASC:
    url = 'common/service/pogriddata.json';
    break;
  case uiGridConstants.DESC:
    url = 'common/service/pogriddata.json';
    break;
  default:
    url = 'common/service/pogriddata.json?page='+paginationOptions.pageNumber;
    break;
}

$http.get(url)
.success(function (data) {
  $scope.gridOptions.totalItems = 30;
  var firstRow = (paginationOptions.pageNumber - 1) * paginationOptions.pageSize;
  $scope.gridOptions.data = data.slice(firstRow, firstRow + paginationOptions.pageSize);
   }); }; getPage();  }]);})();

View page:

<div ng-controller="SearchResultsController" class="col-md-12">
<div  ui-grid="gridOptions"  ui-grid-cellNav ui-grid-edit ui-grid-resize-columns ui-grid-pinning ui-grid-move-columns ui-grid-importer ui-grid-exporter ui-grid-pagination class="grid"></div>
</div>

Answer №1

app.directive('gridWrapper', ['$http',function ($http) {
   return {
        restrict: 'EA',
scope:{
enableFilter:'@',
enableSorting:'@',
enableColResize:'@',
ngModel: '=',
getHeader: '=',
getData: '=',
},
     
        template:'<div ag-grid="gridOptions" class="ag-fresh" id="gridView" ></div>',
controller:function($scope){ 

          $scope.gridOptions = {
            columnDefs: $scope.getHeader,
            rowSelection: 'single',
            rowData: $scope.getData,
            enableFilter: $scope.enableFilter,
            enableSorting: $scope.enableSorting,
            enableColResize: $scope.enableColResize
          };

}
    };
 
}]);
<grid-Wrapper enable-Filter="{{true}}" enable-Sorting="{{true}}" enable-Col-Resize={{true}}  ng-model="model"   get-header="gridheader" get-data="rowData" ></grid-Wrapper>

<grid-Wrapper enable-Filter="{{false}}" enable-Sorting="{{false}}" enable-Col-Resize={{false}}  ng-model="model1"   get-header="gridheader1" get-data="rowData1" ></grid-Wrapper>

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

Presenting a dynamic selection of files from a database in a dropdown menu with looping functionality using Laravel

I have a dropdown menu that I want to use to display input files from the database based on the selection made. Here is the scenario: When a dropdown option is chosen, it should show input files derived from the "loop_atachment" table (based on the select ...

Simple method for converting pixel values to em units

I'm searching for a simple method to incorporate a line of code into one of my plugins in order to convert specific pixel values into em values. My project's layout requires the use of ems, and I'd prefer not to rely on external plugins for ...

Utilizing AJAX in a Bootstrap modal for form submission within a Django environment

I want my modal Bootstrap form to utilize AJAX when the submit button is clicked. It would be ideal if I could submit the form and stay on the modal page, receiving a thank you message upon successful registration or an error message handled by parsleyfy i ...

Tips on how to ensure that an onClick JS function only runs when radio buttons are selected

I have implemented the formslider library for a form on my website. In the demo, the slide transitions to the next set of questions based on a click event triggered by radio buttons (answers). However, when I attempted to include checkboxes for users to s ...

Exploring the ancestors of an element

JS <script> $('.btn').click(function(e){ target = e.target; parent = target.parentNode.parentNode; console.log(parent); }); </script> HTML <div class="card" sty ...

Implementing a similar approach to ngClassEven and ngClassOdd for ngOptions

Can I apply styling to ngOptions using ngClassEven and ngClassOdd? I understand that these are typically used with ngRepeat, but for now, I am unable to switch to ngRepeat. I came across this solution on Stack Overflow, but it doesn't quite meet my re ...

"Experiencing a problem with Next.js 13 where the User Context is not functioning properly outside of _app

When using Next.js 13 and the user context, I've noticed that it doesn't function properly outside of _app.tsx. Any idea why? src>context>UserPositionContext.tsx import { createContext, useContext, useState } from "react"; const ...

Issue with onClick event not firing when using option tag in React

onClick event seems to have an issue with the <option> tag. How can we successfully use the onClick event with the select option tags while assigning different parameters to each option? async function setLanguage(language) { ...

`In HTML, trigger blocks based on the number chosen by the user`

I am working on creating a web page where users can select the number of friends they have, and based on that input, a corresponding number of invisible boxes will be triggered. For example, if a user selects 3 friends, 3 boxes will appear for them to ente ...

Generate a new cookie only when it is not currently present

My objective is to: Determine if a cookie named "query" exists If it does, take no action If not, create a cookie called "query" with a value of 1 Please note: I am working with jQuery version 1.4.2 and utilizing the jQuery cookie plugin. Any recommend ...

How can I turn off autocomplete in MUI textfields?

Currently, I am working with the latest version of mui. Within my user contact info form, there is a zip code field that I do not want to be auto completed if the value is null. However, despite my efforts, it continues to autocomplete with the email saved ...

What is the best way to transform this SQL query into Sequelize syntax?

As I work on a SQL Query, I've come across this issue: select min(date_part('year', "date")) from "Arts" a I need to convert it into a sequelize query. Any assistance would be much appreciated. Art.findOne({ attributes: [[sequelize.fn(&ap ...

What is the best way to reference a div link from a different PHP file?

Struggling to access a page div from another php file, I've tried calling it using (found online) admin.php#changepass Here's an example of my HTML code: <div id="changepass" class="w3-container city" style="display:none"> <form name ...

Implement a React Component as a unique OverlayView within the Google Maps application

Utilizing the Google Maps JavaScript API V3, I have created a map with clustered and draggable map markers. Instead of relying on React libraries that interact with the google maps API, we chose to build our own solution due to limitations in drag function ...

Insert Authentication Token Post AJAX Request

Currently, I am developing a web application using Angular.JS and Node.JS for the back-end. When it comes to the login page, an AJAX call is utilized to handle the login process. Upon a successful login attempt, my goal is to redirect the user's brow ...

The controller element in AngularJS becomes undefined when invoked within a directive

Presented below is a snippet of my controller: myApp.controller('WizardController', function ($scope, $http) { $scope.user = { addressline1: null, cobuyer: null, validate: null, cobuyerfirstname: null, cobuyerlastname: null, ...

A step-by-step guide on connecting an event listener to the search input of Mapbox GL Geocoder in a Vue application

I've encountered a challenge trying to add an event listener to the Mapbox GL Geocoder search input within a Vue application. It appears to be a straightforward task, but I'm facing difficulties. My objective is to implement a functionality simi ...

Utilizing Route Parameters in Node.js

frontend.jade doctype html html head meta(charset='utf-8') //if lt IE 9 script(type='text/javascript', src='http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js') // [if gte IE 9] <! scr ...

Guide to ensuring every request in an Express.js application contains a cookie

Currently, I am in the process of developing a CRUD application using both React and Node. As part of my development, it is crucial for me to validate whether the cookie is present or not for each request. app.all("*", (req,res) => { // If the cookie ...

I am experiencing an issue with my d3 force directed graph where the links are not

I am relatively new to d3 and have limited experience with web frontend development. In my current web application project, I am attempting to create a force directed graph. Despite spending several hours trying to make it work, I have been unable to displ ...