The jqGrid colModel is failing to execute a function as expected

Within the given code snippet, the function attrSetting is invoked. However, when I modify it to

{"name":"A", "index":"0", "cellattr":attrSetting}
, the code executes smoothly. But here lies the issue - cellattr interprets it as a string rather than a function.

let gridData = {"list":[{"A":"abc", "B":"def", "C":"IRIS", "D":"Testing","E":"17-12-2012","F":"Test", "attr":{"A":{"rowspan": 3}}},{"A":"abc", "B":"def", "C":"IRIS", "D":"Testing","E":"17-12-2012","F":"Test", "attr":{"A":{"display":"none"}}},{"A":"abc", "B":"def", "C":"IRIS", "D":"Testing","E":"17-12-2012","F":"Test", "attr":{"A":{"display":"none"}}}]};
$(document).ready(function(){
prepareGrid();
});
function prepareGrid(colModel) {
$("#grid").jqGrid({
    datatype    :   'local',
    contentType :   'application/json',
    data        :   gridData.list,
    loadtext    :   "Loading...",
    colNames    :   ['TB Element','GL Element', 'Company Name', 'Status', 'Date', 'User'],
    colModel    :   [
                     {"name":"A", "index":"0", "cellattr":"attrSetting" },
                     {name:"B", index:1 },
                     {name:"C", index:2},
                     {name:"D", index:3},
                     {name:"E", index:4},
                     {name:"F", index:5}
                    ],
    width       :   '500px',
    height      :   '200px',
    rownumWidth :   30,
    scrollrows  :   true,
    shrinkToFit :   false,
    rownumbers  :   true,
    viewrecords :   true,
});
};
function attrSetting(rowId, val, rawObject, cm) {
   let attr = rawObject.attr[cm.name], result;
   if (attr.rowspan) {
      result = ' rowspan=' + '"' + attr.rowspan + '"';
   } else if (attr.display) {
      result = ' style="display:' + attr.display + '"';
   }
   return result;
};

Answer №1

If you consider making a modification,

"cellattr": attrSetting

is the way to go.

Furthermore, it's crucial to be cautious when utilizing jqGrid options. Your current code is riddled with various issues. Here are just a few examples:

  • If you opt for datatype: "local", make sure to eliminate any index properties from your colModel or ensure they match the values in the name property precisely. Failure to adhere to this rule will render the columns unsortable and impede the searching/filtering of local data.
  • You have not utilized the pager or toppager options of jqGrid. I highly recommend specifying the rowNum option with a sufficiently large value such as rowNum: 10000. The default rowNum value is 20 (refer to the "Default" column in the table outlining the available options). Thus, jqGrid will only display the initial 20 rows from the provided array gridData.list.
  • The values for width and height must be numerical figures like 500 or 200, rather than strings like '500px' and '200px'. You can use the string "auto" or "%100" for the height value.
  • An essential option missing from your code is contentType.
  • I suggest enabling the gridview: true and autoencode: true options.

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

Converting JSON to XML in Java while preserving attributes

When utilizing the org.json json library, converting from XML to JSON is straightforward. However, the conversion back to XML results in JSON attributes being converted into XML nodes: import org.json.JSONObject; import org.json.XML; public class Test { ...

What is the best way to design a footer that remains fixed at the bottom of the screen but becomes unfixed when the user scrolls down?

Currently, I am working on creating a footer that remains fixed at the bottom of the user's screen regardless of the screen size. However, I also want the header to transition from a fixed position to being part of the page when the user scrolls down. ...

Tips for resolving an issue with an overflowing Uber React-Vis multicolor bar chart

I am trying to create a multi-colored bar chart using Uber's react-vis library. However, I am encountering an issue where the leftmost bar of the chart is overflowing below the YAxis instead of remaining contained to the right of it. You can view the ...

Transmitting JSON information using post method through .ajax(), as well as receiving a JSON reply

Seeking assistance with debugging a registration page I am currently coding. I have hit a roadblock for the past 48 hours and would greatly appreciate any help in resolving the issues. CHALLENGE I am utilizing JavaScript to validate form inputs for error ...

What is the best way to calculate the number of squares required to completely fill a browser window in real-time?

I am creating a unique design with colorful squares to cover the entire browser window (for example, 20px by 20px repeated both horizontally and vertically). Each square corresponds to one of 100 different colors, which links to a relevant blog post about ...

Encode data in JSON format using Javascript and then decode it using PHP

In my coding journey, I decided to create an object using Javascript to pass it as an argument to a PHP script. var pattern = new Object(); pattern['@id'] = ''; pattern['@num'] = ''; pattern.cprop = new Object(); // ...

Changing webpage content without using asynchronous methods once authentication has been completed using Google Firebase

My goal is to create a website where users can sign in with Google by clicking a button, and then be redirected to a new HTML page. However, I am facing an issue where the Google sign-in window pops up briefly and closes immediately, causing the HTML page ...

Every time I attempt to submit the login form on the Ionic and Angular page, instead of capturing the values of the form group, the page simply refreshes

Struggling with submitting the login form in Ionic and Angular? When attempting to submit, the page reloads instead of capturing the form group values. I am utilizing angular reactive forms and form builder within the ionic framework. Need assistance in id ...

How can you obtain values from a nested JSON object and combine them together?

I have a javascript object that is structured in the following format. My goal is to combine the Name and Status values for each block and then store them in an array. { "datatype": "local", "data": [ { "Name": "John", ...

What steps should be taken to properly assess an AngularJS provider setup?

My provider definition looks like this: (function(angular) { angular.module('myModule', []) .provider('myService', function () { var service = {}; service.configureSomething = function () { }; service.$get = function () { ...

What could be the reason for a querySelector returning null in a Nextjs/React application even after the document has been fully loaded?

I am currently utilizing the Observer API to track changes. My objective is to locate the div element with the id of plTable, but it keeps returning as null. I initially suspected that this was due to the fact that the document had not finished loading, ...

Is the variable leaping to a superior scope?

A few days back, I encountered a strange bug in my code that has left me puzzled. It appears that a variable declared within a narrower scope is somehow leaking into a broader one. Any insights into what might be going wrong here? Here's a simplified ...

Observing the element with a directive

I am working with the following code: angular.module("Test", []) .directive("testDirective", function () { return { restrict: "A", scope: { model: "=" } link: function(scope, element) { scope.$watch(function () { elem ...

Subsequent modal forms do not trigger Ajax responses

I have a list of items displayed on a page, with each item having a link that opens a unique modal. Inside this modal, there is a text field for entering a username and an AJAX call to display usernames when "@" is typed (pretty cool, right?). The issue I& ...

Fixing the "Cannot find name" error by targeting ES6 in the tsconfig.json file

I recently started learning AngularJS through a tutorial. The code repository for the tutorial can be accessed at this link. However, upon running npm start using the exact code provided in the tutorial, I encountered the following error: Various TS2304 e ...

A guide on utilizing the JsonProperty for accessing nested properties in JSON data

In attempting to deserialize a JSON code using Json.net, I encountered an issue with the cast property being nested within the credits property. To address this, I created a class named Actor to represent the cast members and included a list of this actor ...

Issue with Laravel ReactJs: My changes in the ReactJs file are not being reflected on the website

I've been utilizing Reactjs within Laravel. Recently, I made some modifications to my React Component and upon refreshing my browser, the changes did not reflect. Here are the files involved: resources/views/welcome.blade.php <!doctype html&g ...

How to pass arguments to the `find` method in MongoDB collections

I've been attempting to pass arguments from a function to the MongoDB collection find method. Here's what I have so far: async find() { try { return await db.collection('users').find.apply(null, arguments); } catch(err) { c ...

How to handle an undefined index issue when passing data from AJAX POST to PHP

Greetings to all! As a newcomer to this online community and someone just beginning their journey with programming, I find myself faced with a challenge involving the posting of JSON data using AJAX to another PHP file. Here is my current code snippet: -- ...

An unexpected error occurred with React JS stating: "Parse Error: Line 27: Unexpected token . at http://localhost/PHP-React-Demo/ajax_call.html return {cell.data.row}"

I need help in showing data from a database in a React JS application. Encountered error in React JS: Error: Parse Error: Line 27: Unexpected token . at http://localhost/PHP-React-Demo/ajax_call.html return {cell.data.row} This is my code: Index.p ...