1

I'm using Guriddo jqGrid 5.2.1. I've defined a jsonReader to handle the non-standard format of the data being returned by the URL:

jsonReader : {
            root:"payload.rows",
            page: "payload.page",  // the current page
            total: "payload.total",  // the total # of pages
            records: "payload.records"  // the total # of records
        },

The grid also has loadonce set to true, so that all of the data is loaded once. This makes it possible for me to let the grid handle sorting instead of requesting sorted data from the server. One of the input fields in the rows is a date represented in milliseconds. I defined a beforeProcessing function to handle converting the date in each row to be a value that can be formatted properly:

beforeProcessing: function (data) {

            for(var i = 0; i < data.payload.rows.length; i++) {
                var d = new Date(data.payload.rows[i].ihw);
                data.payload.rows[i].ihw = d;
            }

        },

This worked fine when the data is initially retrieved. However, I noticed that beforeProcessing is called whenever I click on a column to sort, even though loadonce is set to true. I can change the code to handle formatting the date using a formatter like in this post. However, I have other data manipulation needs where, for example, I'd like add a new field to each row that is a concatenation of three other fields in the row. When jsonReader is used, the incoming data appears to be transformed into the standard jqgrid format, so that the next time beforeProcessing is executed (upon a sort, for example), the beforeProcessing code errors because the data is now in a different format than before. Is there an event I can use that only runs once, after the data is initially retrieved from the server? Or do I need to set a JavaScrpt variable in beforeProcessing that indicates that the data has been processed once, and skip processing in subsequent calls?

Michael Sobczak
  • 1,045
  • 1
  • 24
  • 45
  • It seems that the problem is specific for commercial [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334), which you use. Neither [free jqGrid](https://github.com/free-jqgrid/jqGrid) nor old jqGrid (in versions <=4.7) should has the problem. – Oleg Aug 01 '17 at 18:26
  • If you want to continue to use commercial Guriddo jqGrid JS then I'd recommend you to insert `if (this.p.datatype === "json") {/*the for loop*/}` in the code of `beforeProcessing`. It allows you to make some modifications of the data returned from *the server* before the data will be processed by jqGrid. – Oleg Aug 02 '17 at 09:35

1 Answers1

0

The old code does not take in account beforeProcessing event when the datatype was local - that was the reason to fix this bug and add beforeProcessing to the all datatypes.

Since the option loadonce is set to true you datatype is changed from json to local, but to the beforeProcessing is passed different data as you mentioned.

One possible solution is to set a flag to execute this code only once or set the same event to null in the event

beforeProcessing: function (data) {

        for(var i = 0; i < data.payload.rows.length; i++) {
            var d = new Date(data.payload.rows[i].ihw);
            data.payload.rows[i].ihw = d;
        }
        this.p.beforeProcessing = null;

    },
Tony Tomov
  • 3,122
  • 1
  • 11
  • 18