Setting up dgrid cells to show the complete width of the information

I am developing an application that will generate a dgrid with variable column numbers and widths determined by user input. Please refer to the screenshots below for examples. The first screenshot displays a dgrid with only a few select fields, rendering neatly across the screen horizontally. In contrast, the second screenshot shows a dgrid with numerous select fields, resulting in cluttered data as it tries to fit everything on one screen. It's important to note that the dgrid is contained within a dijit BorderContainer.

Screenshot 1 (Small SELECT fieldset, renders correctly)

Screenshot 2 (Large SELECT fieldset, renders poorly)

While there are several issues at play here, my primary question is:

  1. Is there a CSS rule or another method, such as a dgrid function or event, that can be used to ensure cells expand to accommodate their content without cutting off any data (i.e., no overflow)? This would require the grid to display a horizontal scrollbar. I attempted using
    .dgrid-cell { white-space:nowrap; }
    , but this approach was ineffective. It seems as though a span element may need to be added inside each cell with the specified CSS rule?
  2. Additionally, determining when to apply the above rule versus allowing the table to use 100% width when the data fits within the screen may present a secondary challenge. Any suggestions on how to address this issue?

Thank you.

Answer №1

Below is my method for setting a grid cell width based on data. This approach is specifically for a datagrid object, but may require some adjustments for a dgrid object.

Here are the steps involved:

1) Start by creating a <div> tag in your HTML with the id test:

<div id="test"></div>

2) Iterate through the data for each column in the grid and determine the largest width of the data for that column using JavaScript, like so:

var fnt_sz = '9pt';
var tst_item = window.document.getElementById("test");
tst_item.style.fontSize = fnt_sz;
var widthPX = 45; //default pixel width
for (var i = 0; i < columns.length; i++) {
  tst_item.innerHTML = columns[i]; //columns array represents data to be displayed for the column
   widthPX = (tst_item.clientWidth + 1); //Calculate the width in pixels of the data, add padding as needed
}

Key considerations here include setting the appropriate font size (fnt_sz), iterating through all the column data to find the largest width in pixels for the column. Utilize the largest widthPX value to set the column width in your layout object for the datagrid.

3) When constructing the layout JSON data for your datagrid, specify the width for each column (using the maximum data widthPX for the column). For example:

var build_layout = "    var layout = [[{'name': 'ID', 'field': 'id', 'width': '17px'},";
for (var i = 0; i < cols.length; i++) {
  build_layout += "\n{'name': '" + cols[i] + "', 'field': 'col" + (i+1) + "', 'width': '" + widths[i] + "px'},"; 
}
build_layout += "]];";
eval(build_layout);

/* Create a new grid */
var grid = new DataGrid({
  id: 'grid',
  store: store,
  structure: layout, //Utilize layout JSON data with specified width: xxpx; for each column
  rowSelector: '20px',
  style: 'font-size:9pt',
  rowsPerPage: 1000,
  columnReordering: true,
  autoWidth: true,
  autoHeight: autoH});

Upon displaying the grid, it should appear well in any browser with all data visible, as the column width PX value exceeds the largest data item for that column. It's recommended to set a maximum pixel value for each column to avoid excessive width.

While this process can be cumbersome, ideally such functionality should be inherent to the Dojo datagrid object...

Here is the outcome after following these steps:

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

Is it possible for a Titanium application to utilize the iOS sharing functionalities?

I am looking to integrate a share feature in my app that allows users to easily share content by clicking on a share icon. I want the iOS share page to appear from the bottom of the screen, giving users options to share via message, mail, Twitter, and Face ...

Having trouble with setting up the next image configuration for graphcms' images

I've been using graphcms solely for my image assets and trying to integrate them into my Next JS (v12.0.1) frontend. However, I keep getting the error message hostname not configured in next.config.js, even though I have already specified it in my nex ...

The methods in AngularJS Factory are accessed through the returning object

Trying to utilize the this keyword in an AngularJS factory can be a bit tricky. When you return an object with functions as properties and attempt to call one function from another using this, it may result in errors. Upon examining the this keyword thro ...

Tips for resetting the form input fields in Vue.js after the user has successfully submitted the form

I am facing an issue with my registration page. After the user enters some values and successfully submits the form, I want to clear all the fields. To achieve this, I am using a predefined function called reset() inside the script section. However, the ...

Retrieving dynamically generated form components upon submission in a React application

I am facing an issue with a modal containing a dynamically rendered form in my React component. I want to gather all the user-entered field values and send them to the backend. Below is the code for rendering the dynamic form fields: import React from &ap ...

Retrieve the id of a new Rails item using JSONresponseData

Currently in XCode for iOS, I am interacting with a Rails database by utilizing JSON. To achieve this, I have set up specific actions to retrieve information from the database as well as to input new data. My main goal is to insert a record into the datab ...

"Exploring the world of custom middleware in NextJs on a localhost

Question regarding nextjs page middleware: the documentation states that the request object should include geo, IP, ua, cookies, and nexturl properties. Visit the link for more information. I am attempting to retrieve the user's location using page m ...

Conceal a row in a table after successfully executing a delete query using Ajax

Currently, I am facing an issue with hiding rows within a while loop. My goal is to hide an entire row once the delete query has been successful. The deletion query itself is functioning correctly, but I require assistance in properly hiding the row. Any h ...

Is this JavaScript code accurately matching the height of all three elements?

What I aim to achieve https://i.sstatic.net/aNCWV.png I attempted to create a condition-like statement: [ Comparing the heights of the 3 elements ] → When all are smaller than the height of content: Stretches to the full height of content. (jus ...

Is it advisable to consolidate all of my JavaScript files into a single file?

Is it necessary for me to combine all my JavaScript files into one single file and then include that file in a folder? I currently have 10 separate JS files within a "javascript" folder - is this bad for the website, or is it okay? P.S. I am new to web de ...

Ajax fails to function within a loop

I'm looking to implement Ajax in a loop to retrieve data sequentially. This is what I have in my JavaScript function: var resultType = $("input[name='resultType']:checked").val(); var finalResult = ""; var loadingMessage = "<img src=&ap ...

Guide to sending a DateTime parameter to a Web API 2 endpoint

I've encountered an issue with a controller I'm working on: [RoutePrefix("api/Example")] public class ExampleController : ApiController { [Route("Foo")] [HttpGet] public string Foo([FromUri] string startDate) { return "Th ...

Exploring nested dictionaries in Swift: accessing a dictionary inside an object contained within another

Utilizing the Yellow Pages API, I am able to access business listings based on the phone's location. The results are returned in JSON format and I have successfully parsed it: do { let searchResultsData = try NSJSONSerialization.JSONObjectWithDa ...

Building a connection between AngularJS and Node.js: A step-by-step guide

I am inquiring about creating a correlation between different frameworks – specifically, how can I effectively communicate Angular with Node? The reason for my question is that I currently have a Firebase project utilizing Ionic/Angular. However, I no l ...

Tracker.autorun subscription triggers repeated firing of publish callback

Working on a ReactJS and Meteor project, I encountered an unexpected behavior that I would like to discuss here: Within the code, there is a block with Tracker.autorun containing a call to Meteor.subscribe. On the server side, there is a corresponding Met ...

Iterate over an array of objects to showcase the property values within an HTML tag using JavaScript

I am a beginner in JavaScript and I am currently working on an exercise. My goal is to iterate through an array of objects within another object, map the index values from one object to the id values in another object, and based on that, perform a certain ...

Setting up Apollo Server for efficient server-side full response caching

Attempting to establish a cache based on this specific guide for a single costly query that experiences infrequent changes, typically once a day. The query itself lasts around 7-8 seconds, with the majority of time spent post-DB retrieval due to substantia ...

Having trouble getting an element by id in Vue/Nuxt on initial load? You're not alone. It seems like the element

I am currently utilizing VueScroll as a container for a carousel. However, I have observed that when a user hovers their mouse over the VueScroll element and scrolls using the wheel, it zooms in/out on the elements inside VueScroll. I am looking to target ...

Data is not found in req.body when making a fetch request

I have been attempting to send a fetch request to my server, but I am consistently receiving an empty req.body. Here is the client-side script: const form = document.getElementById('form1') form.addEventListener('submit', (e) => ...

Converting a blob to base64 and then back to a blob in Node.js may result in varying sizes between the two

I am currently implementing this code in a Node.js application. I have a blob that I am converting to a base64 string and then back to a blob using the following method: // Converting Blob to base64 string via Buffer const buffer = Buffer.from(await myBlob ...