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.

This is how the filters are sent to the server.

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.

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

Can you explain the distinction between using angular.copy() and simply using an assignment (=) to assign values

When a button is clicked, I want to assign certain values using the event parameter. Here is the code: $scope.update = function(context) { $scope.master = context; }; The $scope.master has been assigned the values of user. Recently, I came across th ...

Locate the final element within an array using JavaScript

Provided with a file path like new/lib/java.exe, I am looking to eliminate the root folder 'new' and structure the new path as lib/java.exe. Challenge: After my attempts, I am left with the path as lib/java.exe/ which includes an unwanted "/". I ...

What is the best way to initiate a Post/Get Request to NodeJS without causing a page refresh?

Struggling to run a NodeJS function after clicking a button? You're not alone. Ajax requests haven't been working for me either when it comes to running NodeJS functions. I've got a bash script that generates a JSON file which I want to par ...

A step-by-step guide on how to parse JSON using Swift 4

I have tried countless tutorials, but the issue persists. Take a look at the JSON file: { "header":[ "Academic Year", "Scheme Type", "Student Type", "Institution", "Deadline", "Remark" ], "rows":[ [ ...

Querying with mongodb using the $or operator yields a single result

I need help querying my MongoDB using JavaScript as I am only getting one result when I should be utilizing the $or operator. The frontend sends a string, which I then split by spaces to search for matches in the database based on skills, location, and na ...

Tips for creating a useState variable within the render() function in reactjs

I'm completely new to reactjs, and I've been trying to define useState variables within the render() method of a reactjs component. However, I keep running into an error: "Invalid hook call." class ProductDefinition extends Component { construc ...

Automatically notifying users via email about console errors in JavaScript

My exploration on this question and useful links: How to send console messages and errors to alert? jQuery AJAX form using mail() PHP script sends email, but POST data from HTML form is undefined The coding example: function handleError(evt) { if (ev ...

Troubleshooting a problem with Angular routing when trying to access a specific URL using

My main objective is to enable users to view products by clicking on the item itself. For each product item displayed in main.html, the URL format is like this... <a href="/products/{{ product.id }}">{{ product.title }}</a> For instance, when ...

Utilizing Express.js for reverse proxying a variety of web applications and their associated assets

I am looking to enable an authenticated client in Express to access other web applications running on the server but on different ports. For instance, I have express running on http://myDomain and another application running on port 9000. My goal is to re ...

Guide on how to retrieve a response from an API Route and integrate it into the client side of the app router within Next.js

Transitioning from Next.js 12 to 13 has been quite perplexing, especially when it comes to handling JSON data. Every time I attempt a fetch request, I find myself needing to refer back to documentation due to the confusion surrounding JSON. Is there anyone ...

React TypeScript - creating a component with a defined interface and extra properties

I'm completely new to Typescript and I am having trouble with rendering a component and passing in an onClick function. How can I properly pass in an onClick function to the CarItem? It seems like it's treating onMenuClick as a property of ICar, ...

How do I retrieve and display all the locally stored variables in Angular 2/JavaScript and calculate the total number of keys present in the localStorage?

Currently, I'm developing a project in Angular2 that involves the storage of user-added items to a shopping cart. To accomplish this, I have opted to utilize local storage to temporarily save the items. Each category (such as shoes, clothes, etc.) is ...

Utilize AngularJS to parse JSON data and dynamically populate a dropdown selection list with the corresponding information

As a newcomer to Angularjs, I am looking for guidance on how to retrieve data from a JSON file and display it based on the selection in a dropdown list. For instance, if a specific "Region" is chosen from the dropdown, only the data corresponding to that r ...

Deactivate text/input field in React and Next.js based on Dropdown Selection

As a newcomer to React, I am facing an issue with disabling and enabling an input field based on the selected value in a dropdown. My tech stack includes React, Next Js, React-bootstrap, and formik. function Input(){ const [disabled, setDisabled] = useStat ...

Sending a JSON object using Java sockets – step by step guide

Can anyone provide guidance on successfully sending Json objects through sockets, preferably using the ObjectOutputStream class in Java? Here is my current code: s = new Socket("192.168.0.100", 7777); ObjectOutputStream out = new ObjectOutputStrea ...

Guide on adding pictures to an ExpressJS server

Working on this project has been quite a challenge for me as I delve deeper into web development. As a relatively new developer, creating a one-page application with image upload functionality has proven to be quite the task, especially considering this is ...

Waiting for response in AngularJS Controller and setting callback

I have developed an AngularJS application with controllers.js and factories.js that I am excited about. However, I am facing a challenge when trying to manipulate the values within the controller (retrieved from the factories). The issue is that these val ...

Obtaining the accurate offsetTop and offsetLeft values for an element following a CSS3 rotation

Is there a method to accurately determine the offsetTop and offsetLeft values of an element post-transform rotation? Are there any lesser-known element properties that could be helpful in this scenario? Attached is an image that can provide further clari ...

Comparison of loading time between loader gif and browser's loading image

Recently, I've come across a code snippet where I implemented a 'loader' gif to show while PHP retrieves data from an SQL database. Everything seemed to be working fine when testing on my localhost. However, upon closer observation, I notice ...

Storing a temporary value within an ng-repeat iteration in AngularJS

A unique and interesting query arises when dealing with random value generation. Here is a snippet of code that showcases how to achieve this: function randomize() { return function (input) { if (input !== null && input !== undefined & ...