Eliminate the JSON object within jqGrid's posted data

The web page I'm working on features Filters with two buttons that load data for jqGrid when clicked.

Clicking 'Filter' generates a postData json object and sends it to the server, which is working perfectly.

However, I'm facing an issue when the user clicks on 'Clear'. I want to reset the filter and reload all data from jqGrid, but I'm unsure how to do this.

https://i.sstatic.net/bA0fD.png

This is how the filters are sent to the server. https://i.sstatic.net/G9lLc.png

The jQuery code for the button clicks is shown below:

    $('#btnFilter').click(function (event) {

        var filterObject = { 
            RecordID: $('#RecordID').val() 
            , Student: $('#Student').val() 
        }

        $("#jqGrid").jqGrid().setGridParam({ postData: { Filters: filterObject }, page: 1 }).trigger("reloadGrid");
    });

    $('#btnClear').click(function (event) {         

        var filterObject = {};
        $("#jqGrid").jqGrid().setGridParam({ postData: { Filters: filterObject }, page: 1 }).trigger("reloadGrid");
    });

Despite setting a null JSON object for the filterObject using { }, the browser caches the Filters value and I'm unable to remove them. The screenshot below shows the Ajax Post and objects sent to the server when the 'Clear' button is clicked.

https://i.sstatic.net/8xZGj.png

I need help in figuring out how to remove the Filters { } objects when 'Clear' is clicked. If there's a better way to achieve my goal, I am open to suggestions.

Answer №1

Understanding that jqGrid holds all parameters in one internal object makes it very simple to work with. You can access all options of jqGrid by using

var p = $("#jqGrid").jqGrid("getGridParam");

You can directly modify the properties within this object. The postData can be accessed through p.postData. If you only need to change the postData, you can do so by using

var postData = $("#jqGrid").jqGrid("getGridParam", "postData");

This will give you direct access to the postData property of the internal postData object (p.postData). This means you do not have to use setGridParam to make changes to the data. In order to remove the Filters property that you previously set using

$("#jqGrid").jqGrid("setGridParam", { postData: { Filters: filterObject }, page: 1 });

You can instead use

var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
delete postData.Filters;

Answer №2

If you're ever unsure, passing null will effectively void out the prior JSON object. Check out the code snippet below for reference.

$("#myGrid").jqGrid().setGridParam({ data: { Filters: null}, page: 1 }).trigger("reloadGrid");

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

Develop an "Import Interface" using TypeScript

I have a large project with many files and I believe using an import object would be beneficial. For instance, consider having menu.ts at the top level that every program will refer to: import router from "./router/index"; import controllers from ...

Creating dynamic content with Express.js: Using variables in EJS within the request handler

I am looking for a way to include additional variables to be utilized by EJS during the rendering of a view for every request, similar to adding them in the render function: res.render('view', {data: {my: 'object'}}); I have implement ...

Leveraging jQuery or PHP to lessen the workload

As I set up a registration page, I am relying mainly on PHP to validate the data. Realizing that incorporating client-side validation will lessen the server's load, I'm interested in utilizing jquery for this purpose. Specifically, I am seeking ...

What would be the best way to structure this workflow as a JavaScript data format?

I have a complex workflow that I need to represent as a JavaScript data structure. This workflow involves a series of questions and answers where the response to one question determines the next one asked. Here is a basic example of what this workflow migh ...

The LatinSquare.js script has exceeded the maximum call stack size limit

In my current project, I am utilizing the latin-square library for node.js within a loop to search for a specific pattern. However, I encountered an error after running the script for 2 minutes: RangeError: Maximum call stack size exceeded var latin ...

Tips for parsing a JSON object efficiently

Javascript var obj = { "name" : ["alex","bob","ajhoge"], "age" : [30,31,33] }; To display the value "alex," you can use: document.write(obj["name"][0]) But how can we filter through 'obj' to retrieve all data like this? html <ul ...

Nodejs application encountering a problem with the findUser method in Active Directory

I have encountered an issue while using the activedirectory npm package with Nodejs v16.18.1. The code snippet below is used for authentication and finding user details: Could someone please assist with this? async authUserActiveDirectory(usernameAD: stri ...

Altering the properties of every item within a v-for loop's array

I'm currently exploring Vue's approach to writing JavaScript. Let's consider this situation: In the .vue template <button v-on:click="biggerFont()" class="btn btn-s btn-default" type="button" name="button">A</button> < ...

Leveraging Toaster Notifications in AngularJS for Custom Exception Management and Logging

Implemented the use of AngularJS Toaster for effective notification handling. To customize exception handling, set up in index.html as shown below <toaster-container toaster-options="{'time-out': 3000, 'position-class': 'toast ...

Is the jQuery callback executing too quickly?

I am utilizing ASP.NET MVC C# I have integrated a jQuery function that removes a book, and upon successful deletion, triggers a function to update the list of books. function removeBook(id) { var confirmDelete = confirm("Deletion cannot be undone ...

What strategies can be implemented to decrease the value of the writing box by 2.5%?

I would like to decrease the value in the input box by 2.5%. Here is the HTML code snippet: <div class="ZakatCalc"> <div class="calcimg"> <img src="" alt="" srcset="" class=" ...

The C# MVC Controller is having difficulty retrieving decimal or double values from an Ajax POST request

Having trouble sending decimal or double values via ajax to my C# MVC Controller. The values always come through as null, even though they work fine when sent as strings or integers. Why is this happening? When checking the client's request, the corre ...

Utilizing a JavaScript function to toggle the Bootstrap dropdown without the need for manual clicking

I've configured a Bootstrap dropdown in my site's mini cart that includes a lightbox effect to grey out the background content when the dropdown is activated. Here's the code snippet: $(".dropdown").on('show.bs.dropdown hide.bs.dropdow ...

Guide on grabbing characters/words typed next to # or @ within a div element

Within a div element, I have enabled the contenteditable property. My goal is to capture any text input by the user after typing '#' or '@' until the spacebar key is pressed. This functionality will allow me to fetch suggestions from a ...

What is the reason for receiving a JSX warning even though JSX is not being utilized in the code?

In my Vue.js component file using the Quasar framework, I have the following code block inside the <template>: <q-btn color="green" label="save & continue editing" @click="saveCase()" /> This snippet is par ...

Turning Node.js timestamp into MySQL format

Currently, I am using Node (Express.js) to update a MySQL database with the current date. While it is functional, my code seems to be repetitive. let newDate = new Date(); let yearNow = newDate.getFullYear(); let monthNow = newDate.getMonth(); let dayNow ...

PHP not receiving values when making an AJAX POST request

My AJAX file below is where I pass all the values from my text boxes and post them to edu.php. In edu.php, I intend to update two tables - details and test. However, nothing seems to be updating in my database. When I checked the values with var_dump, the ...

Looking for a break in your Jquery Ajax call?

I'm encountering an issue with calling an AJAX command. The problem arises after the AJAX has successfully called the PHP page, inserted the data into the database, and the success of the AJAX call is supposed to redirect the page to somewhere else. H ...

Why does my Observable remain perpetually unfulfilled?

I recently started learning javascript and came across the Angular 2 Documentation where I discovered that Promises can be replaced with Observables. While experimenting with a simple code, I noticed that in addition to the expected result, I am also getti ...

Leveraging the power of the .includes() method

Here is a question regarding the issue at hand. To tackle this problem, create a function called checkForPlagiarism with two parameters: an array containing responses from a specific individual and a string representing an external source. The function sh ...