What could be causing the issue with outerWidth not functioning properly in jqgrid rows and columns?

I came across a helpful post on adjusting Jqgrid Column width According to Its Content, and decided to implement it in my project.

Jqgrid Column width According to Its Content

The adjustment takes place after the load complete event of jqgrid.

loadComplete : function () {

$("#list").bind("jqGridAfterLoadComplete", function () {
    var $this = $(this), iCol, iRow, rows, row, cm, colWidth,
        $cells = $this.find(">tbody>tr>td"),
        $colHeaders = $(this.grid.hDiv).find(">.ui-jqgrid-hbox>.ui-jqgrid-htable>thead>.ui-jqgrid-labels>.ui-th-column>div"),
        colModel = $this.jqGrid("getGridParam", "colModel"),
        n = $.isArray(colModel) ? colModel.length : 0,
        idColHeadPrexif = "jqgh_" + this.id + "_";

    $cells.wrapInner("<span class='mywrapping'></span>");
    $colHeaders.wrapInner("<span class='mywrapping'></span>");

    for (iCol = 0; iCol < n; iCol++) {
        cm = colModel[iCol];
        colWidth = $("#" + idColHeadPrexif + $.jgrid.jqID(cm.name) + ">.mywrapping").outerWidth() + 25; // 25px for sorting icons
        for (iRow = 0, rows = this.rows; iRow < rows.length; iRow++) {
            row = rows[iRow];
            if ($(row).hasClass("jqgrow")) {
                colWidth = Math.max(colWidth, $(row.cells[iCol]).find(".mywrapping").outerWidth());
            }
        }
        $this.jqGrid("setColWidth", iCol, colWidth);
    }
});

}

While implementing this code, I encountered an issue where the column width was returning as zero at specific lines.

colWidth = $("#" + idColHeadPrexif + $.jgrid.jqID(cm.name) + ">.mywrapping").outerWidth()+25;

This problem seemed to be related to special characters in the column headers even though autoencode was set to true. The column width was not being calculated correctly. Is there a need to parse these columns?

list_Code [HEAVY] [DUTY] [50]

Similarly, I noticed that the row outerwidth was also resulting in zero at this line,

colWidth = Math.max(colWidth, $(row.cells[iCol]).find(".mywrapping").outerWidth());

As a consequence, the width of all my rows remained fixed at 25 pixels.

Why am I unable to obtain the outerwidth() values for columns and rows in these scenarios?

If I use .html(), the Div content is displayed correctly like this,

var spans = $( "span" );
$(row.cells[iCol]).find(spans).html();

HTML DIV Trace :

TH

<th id="list_Code [HEAVY] [DUTY] [50]" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr" style="width: 65px;">
<span class="ui-jqgrid-resize ui-jqgrid-resize-ltr" style="cursor: col-resize;">&nbsp;</span>
<div id="jqgh_list_Code [HEAVY] [DUTY] [50]" class="ui-jqgrid-sortable">
<span class="mywrapping">Code [HEAVY] [DUTY] [50]<span class="s-ico" style="display:none">
<span sort="asc" class="ui-grid-ico-sort ui-icon-asc ui-state-disabled ui-icon ui-icon-triangle-1-n ui-sort-ltr">
</span><span sort="desc" class="ui-grid-ico-sort ui-icon-desc ui-state-disabled ui-icon ui-icon-triangle-1-s ui-sort-ltr"></span></span></span></div></th>

TD

<td role="gridcell" style="text-align:left;" title="FE" aria-describedby="list_Code [HEAVY] [DUTY] [50]"><span class="mywrapping"><div style="max-height: 100px">FEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEF... 

It seems that the only issue lies with the widths...

Column Model Formatter:

formatter : function(v){ return '<div style="max-height: 100px">' + v + '</div>'; },

All my column names contain special characters. Could this be the reason for the width calculation problems? If so, why are the row/cell widths also coming out as zero?

I have tried using width/inner/outer methods but none seem to work. Can someone provide some insights into this issue?

Answer №1

It appears that you are utilizing name: "Code [HEAVY] [DUTY] [50]" and using the same text in colNames, or you are using

label: "Code [HEAVY] [DUTY] [50]"
. You have not included the JavaScript code that you are using, so I am left to make assumptions. Additionally, it is unclear which version of HTML dialect you are using on the page (which <!DOCTYPE html ...> declaration is used). It is crucial to note that jqGrid utilizes the name property of colModel to construct id elements for certain internal HTML structures within the grid. This can be observed in the provided HTML snippets. HTML5 does not permit spaces within id attributes (see here for reference). The limitations for ids according to HTML4 can be found here. Furthermore, the characters [ and ] are also incorrect, especially when using older versions of jqGrid like 4.4.1. Therefore, the value name: "Code [HEAVY] [DUTY] [50]" results in generating incorrect HTML code dynamically. To address this issue, you should modify the values while maintaining the same text displayed for the user (defined either by colNames or by the label property of colModel). For instance, you could use

name: "Code_HEAVY_DUTY_50",
label: "Code [HEAVY] [DUTY] [50]"

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

Cypress test never finishes when an unforeseen alert is triggered in the system during the test

When a web application unexpectedly throws an error using an alert box, the Cypress test becomes stuck indefinitely. The test will not complete or fail until the user manually closes the browser or clicks on the OK button within the alert box. https://i.s ...

How to execute a JavaScript function within a Jinja for loop

I am currently working on an HTML page where the variable schedule contains a series of sequential decimal numbers representing seconds. My goal is to develop a function in JavaScript/jQuery that can convert these decimal numbers into time format. However ...

Is it possible to keep content visible in Bootstrap tab by adding CSS?

Having an issue with the "formbox" class that is causing unexpected behavior when applied to a form. When removed, everything works as expected. I've tried narrowing down the issue by removing each CSS rule individually and testing it out but haven&ap ...

How can Angular JS and HTML5 be used to dynamically create controls?

I have designed a unique project to demonstrate dynamic control creation using angularjs and html5. The project includes an xml file with a set of controls, complete with property and event attributes that can be generated dynamically. Initially, all the ...

Discover the technique for choosing an element within an IF statement using the keyword (this)

I am currently implementing bootstrap validation in my project. Here is the JavaScript code I am using: My goal is to achieve the following: $("#create-form").submit(function(e) { if ($("input").val() = "") { $(this).addClass("is-invalid"); ...

Can you show me the correct way to incorporate hashes into JSON?

I am in the process of building a webpage that includes two forms for users and owners. Each form allows users to input comma-separated names, which can then be submitted. In the past, I experimented with JSON using only strings, without relying on a Perl ...

Getting started with CSS and HTML: Tips and Tricks for Beginners

Seeking advice on how to enhance my web designing skills, particularly in starting point and techniques. Currently, I am familiar with HTML and CSS, but have been primarily using pre-made templates for building websites. I aspire to be able to transform ...

How can I make a div's height match that of another div?

I am curious about adjusting the height of a div based on its content. In this case, I have the following HTML structure: div.Pantalla { background-image: url("https://cdn.pixabay.com/photo/2015/07/11/23/02/plane-841441_1280.jpg"); background-size ...

What strategies can be implemented to increase the number of backlinks?

<script type="text/javascript"> $(document).ready(function() { var linkas = $("#button").attr("value"); $('#button').click(function(){ $.get(linkas, function(data){ $(' ...

Looking for a solution to the problem: Module 'import-local' not found

internal/modules/cjs/loader.js:596 throw err; ^ Error: Cannot find module 'import-local' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15) at Function.Module._load (internal/modules/cjs/loader.js:520:25) Encoun ...

How come this JavaScript isn't triggering a confirmation box to display?

I've inherited a project and I'm currently fixing some bugs. There's a snippet of JavaScript that is supposed to highlight certain boxes and prompt a confirmation box. However, what actually happens is the boxes change color, there's a ...

Tips on emphasizing all div elements and incorporating navigation to each of them

I am seeking a method to enhance a div with highlighting when there is a click or mouseover event. For instance, changing or adding a border color using JavaScript on click or mouseover is straightforward. Recently, I have been contemplating the idea of a ...

The attempt to establish a WebSocket connection to 'ws://localhost:4000/graphql' was unsuccessful:

Encountering the Websocket failed to Connect error on both the client and server sides (shown in image below) has left me puzzled for the past 2 days. I have not made any other Websocket configurations apart from the one specified in the apollo client. Any ...

Avoid using the FONT tag with the execCommand function

Is there a way to hide the next element using execCommand configuration? Font Tag <font color="xxxxxxx"></font> I am facing an issue while creating a simple editor for a project. I am struggling with CSS formatting and the font tag is not he ...

Unable to communicate with the server-side controller using Angular

Encountering issues when attempting to call a server-side controller using AngularJS, these two error messages are being displayed. The parameters dictionary contains a null entry for parameter 'problemId' of non-nullable type 'System.Guid& ...

Trouble with the width of the message container in CSS styling

I need to display a message at the top-center of my webpage. I have created a simple example for this: http://jsbin.com/eniwax/3/edit The issue I am facing is with the width of the message container. I want the container width to adjust dynamically based ...

Issue with Internet Explorer 7 regarding jQuery, Ajax, and Cascading Style Sheets

When loading new HTML into a div using load(url) in a simple web page, most browsers apply the CSS styles of the page to the newly loaded content. However, IE7 seems to behave differently. Any suggestions on how to address this issue? Ken Update: The ne ...

Utilizing AngularJS to interpret and process JSON data

Struggling to properly parse this JSON chunk, as my attempts have not been successful. The structure of the JSON is outlined below. I am aiming to use ng-repeat on all attributes in order to display "Content" and "Title" within a div. JSON: { "Hits": ...

filling the dropdown menu with selectable choices

I'm facing an issue with populating the select box with options. Despite receiving JSON data from an AJAX call, the select box remains empty. I can't seem to figure out what's causing this problem. Below is my attempt: $.getJSON('che ...

Simultaneously triggering two separate onchange events with two different functions

I am searching for a solution to trigger two function events onchange with my first option. Despite looking at examples on various websites, none of them seem to work. My desired outcome is to have both showSub(Dogs) and showSize(Dogs) functions open simul ...