Displaying a separate page upon clicking the add button in jqGrid: Tips and Tricks

I am a beginner when it comes to MVC, jQuery, and jqGrid. I have been struggling for the past couple of days trying to figure out how to redirect to another page when the user clicks the add button. Additionally, I need to perform the same action when the edit button is clicked, but first, I must call a JavaScript function to check if the requested row can be edited.

Any assistance would be greatly appreciated.

Below is the code snippet I used to build the jqGrid:

var jqDataUrl = "Account/LoadBaseData";
        $(document).ready(function () {
            // Setting up the jquery grid
            $("#ListTable").jqGrid({
                // Ajax configurations
                url: jqDataUrl,
                datatype: "json",
                mtype: "POST",
                // Column names
                colNames: ["Bank","Account No","Account Name"],
                // Column settings
                colModel: [ { name: "BankReference", index: "BankReference", width: 40, align: "left"}, { name: "AccountNo", index: "AccountNo", width: 40, align: "left"}, { name: "AccountName", index: "AccountName", width: 40, align: "left"}],
                // Grid dimensions
                width: 850,
                height: 400,
                // Pagination
                toppager: true,
                pager: $("#ListTablePager"),
                rowNum: 14,
                viewrecords: true,
                // Grid caption
                caption: "BankAccount List",
                editurl: "/GridDemo/GridSave",
            }).navGrid("#ListTablePager",
            { refresh: true, add: true, edit: true, del: true},
                { }, // edit settings
                {}, // add settings
                {}, // delete settings
                {sopt: ["cn"]}  
            );
        });

Your help in this matter would be highly valuable.

Answer №1

To customize the add and edit buttons in jqgrid for redirecting based on a condition, you can use custom buttons instead of the default ones provided. It's a simple process to implement this customization.

$("#ListTable").jqGrid({
  ...
})
.navGrid("#ListTablePager", {edit:false, add:false, del:false,search:false })
.navButtonAdd("#ListTablePager", { // custom add button
   caption:"Add",  
   buttonicon:"ui-icon-add", 
   onClickButton: function(){ 
     var grid = $("#grid_name"); // your jqgrid
     var rowid = grid.jqGrid('getGridParam', 'selrow'); // get the selected rowid

     // Retrieve cell value of the row with:
     // grid.jqGrid('getCell', rowid, 'rowName');

     if(your condition)
        window.location = ...
   }, 
   position:"last"
})
.navButtonAdd('#ListTablePager',{ // custom edit button
   caption:"Edit", 
   buttonicon:"ui-icon-edit", 
   onClickButton: function(){ 
      ...
   }, 
   position:"last"
});

If you are unsure how to determine whether a requested row can be edited or not, you can access the current selected row within the onClickButton event using:

var grid = $("#grid_name");

var rowid = grid.jqGrid('getGridParam', 'selrow');

You can obtain a specific cell value from the selected row by:

grid.jqGrid('getCell', rowid, 'rowName');

Utilize this information to create the necessary condition for setting window.location to perform the desired redirection.

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

I'm curious if it is possible to retrieve the dates of actions on websites using Selenium with Python

Is there a way to retrieve the date of data, like the date of comments, using HTML codes? I need assistance on how to achieve this through selenium in Python. Can Jquery be used as a solution? ...

What steps should I take to design and implement this basic search form?

Essentially, I have a three-page setup: - One page containing all possible search results such as: 'search result 1' 'search result 2' 'search result 3' - Another page with a search form and enter button. - And finally, a res ...

Comparing the functionality of MVC 3's AjaxHelper with Ajax.ActionLink versus Ajax.RouteLink, as well as the differences between Ajax.BeginForm and Ajax

It is my understanding that: Ajax.ActionLink - Creates a link to a specific action within the current controller Ajax.RouteLink - Generates a link based on the RouteData provided to the helper However, after using MVC 3, I have noticed that Ajax.ActionL ...

Encasing extracted content in <p> tags

On my Wordpress Site, I have extracted images and text separately to use in different sections of my single.php file. Utilizing HTML within the Bootstrap 3 framework: <div class="row"> <div class="col-md-12"> <?php ...

Issue with PHP causing Jquery alert to trigger twice rather than once

I am currently working with jQuery and PHP. I have a button labeled "Edit" but whenever I click on it, the alert message pops up twice instead of just once. Below is my HTML code within the PHP tags: <?php $PostComment2='<div class="button1 ...

Only one bootstrap collapse is visible at a time

Currently, I am using Bootstrap's collapse feature that displays content when clicking on a specific button. However, the issue I am facing is that multiple collapses can be open at the same time. I want to ensure that only one collapse is visible whi ...

Configuring Visual Studio Publish settings for Single Page Applications deployed on Azure

I have set up my Azure App Service and downloaded the publish profile settings to use FTP for publishing my single page app to Azure. However, I am interested in configuring this process in Visual Studio similar to how one would publish a .Net application ...

Complete Form Validation Issue Unresolved by Jquery Validation Plugin

I'm facing an issue with the jQuery validation plugin while attempting to validate a form. Strangely, it only works for one input field. Can anyone suggest a solution? <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.valid ...

Using jQuery on a pair of events

I want to enhance this jQuery function so that it executes both on page load and when the window is resized. jQuery(document).ready(function() { Maybe something along the lines of: jQuery(document).ready OR $(window).resize(function() { ...

Using jquery to transition an image to fade out and then reappear in a different position

There is an image in a div with the highest z-index. Upon clicking on something, the image should fade out and then fade in at a specified position below another image with the class "aaa". The current approach involves using jQuery to fade out the image ...

Tips on serializing two arrays into JSON format and incorporating them in an AJAX request

I have a task where I need to retrieve all the names and details associated with a specific reference number. To accomplish this, I am using a while loop. However, I am unsure of how to encode these values in JSON format so that I can use them in AJAX for ...

What is the best way to deactivate div elements once an overlay has been applied to them?

My goal is to place an overlay on my form to prevent users from accessing the content. Even though I have added an overlay, users can still interact with input fields. How can I prevent that? .overlay { background: rgba(0, 0, 0, .75); text-align: ce ...

Utilizing JQuery to transmit form data to several functions

I'm working on a form with multiple steps that collects data on each step. However, when I submit the form using an ajax request, only the data from the last step is being sent. What's the best approach to solve this issue? <script type="text ...

Narrowing down the area of influence for a jQuery script to only include direct parent

I have crafted a script to identify <fieldset> elements with a child that contains gfield_error, and then apply the class wow-error to those specific <fieldset> tags. The issue I am facing is that the script is being too effective, as it not o ...

Facing issues with Uploadify: while trying to upload large files, encountering IO and HTTP errors

My current situation involves utilizing uploadify for the purpose of uploading large files, with a maximum size of 60MB. An issue I am currently facing is receiving an HTTP error when attempting to upload a 17MB file after it has been successfully uploade ...

What is the most efficient way to load a PHP page once the webpage has completely finished loading?

I'm relatively inexperienced when it comes to PHP and JQuery, so please bear with me if my knowledge is limited. Currently, I'm facing an issue where loading a small HTML table that contains information queried from game servers (like shown in t ...

What is the process of incorporating HTML into a jQuery program in order to immerse the world in an element?

I am looking to utilize HTML with the value in a checkbox, After adding a shortcode: <label class="HCheck">(this is val 1 )</label> and incorporating jQuery to change it to: <label class="HCheck">(this is val 1 ) ...

Ways to retrieve json_decode() information following ajax transmission

Currently experimenting with an ajax script to enhance my skills in ajax. I am using the serializeArray() method to submit a form via ajax, however, although I can successfully pass the data, I am unable to perform any operations on it. var submit_post = ...

Utilize jQuery to export HTML or JSON data to an Excel spreadsheet

I am looking for a way to export json or html data to an excel sheet using only html and jquery, without involving any server code. I have found some fiddles that allow me to download the excel sheet successfully, but unfortunately, none of them work in In ...

What is the process of nesting widgets in kendoui with the PHP wrapper?

I have created a code using treeview in manual mode. div id="treview-back"> <?php $treeview = new \Kendo\UI\TreeView('treeview'); // function to create TreeViewItem with imageUrl function ImageTreeViewItem($text) { ...