2

I have following json-data, after an ajax call to struts2 action-class:

{"jsonData":[{"age":1,"name":"Aifa"},{"age":2,"name":"Afraa"},{"age":27,"name":"Zafar"}],"searchId":0,"searchName":""}

function setAjaxOutput()    {
        if(httpObject.readyState == 4)  {
            document.getElementById('intValueDiv').innerHTML = httpObject.responseText;

            var jsonString = httpObject.responseText;
            alert(jsonString);
            var jsonString2 = JSON.parse(jsonString);            
            alert(jsonString2.jsonData[2].name);           
          }

     }

The json data will be in httpObject.responseText.

Now using jqgrid, it looks like, the above code is not required but I could not yet figure out how to make ajax call using jqgrid, recevie json data, and display in table.

I'm trying to display, only jsonData-array, returned from action-class, in grid-format, using jqgrid. The code is below.

[{"age":1,"name":"A"},{"age":2,"name":"Ab"},{"age":3,"name":"abc"}]

I can call click(), but would like to call, 'jQuery("#list2").jqGrid().

Could you please let me know, how am I suppose to make changes, so I could :

1) send ajax request with :

url: 'callAJax.action?start=1&t='+Math.random()+ 'testFlag=1'

Should single quotes be replaced with double quotes? Please let me know.

Also, how do I find, whether divId1 or divId2, is clicked below.

  var $target = $(event.target);
  if( $target.is("div") ) { // div is tag, how to check for id.

    testFlag=1/2;
  }

2) And, when json data is displayed in grid, I'd like to add button, to each row, which when clicked, I'd like to, read row data, and do an Ajax call to action-class url.

Please let me know, how can this be achieved.

<%@taglib prefix="s" uri="/struts-tags" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>

        <head>
            <title>AJAX JSP PAGE</title>
            <link rel="stylesheet" href="styles/jqx.base.css"
            type="text/css" />
            <link rel="stylesheet" href="styles/jqx.classic.css" type="text/css" />
            <script type="text/javascript" src="jquery-1.7.2.min.js"></script>
            <script type="text/javascript" src="jqxcore.js"></script>
            <script type="text/javascript" src="jqxbuttons.js"></script>
            <script type="text/javascript" src="jqxscrollbar.js"></script>
            <script type="text/javascript" src="jqxmenu.js"></script>
            <script type="text/javascript" src="jqxgrid.js"></script>
            <script type="text/javascript" src="jqxdata.js"></script>
            <script type="text/javascript">
                var start = 0;
                var idAndName = "";
                var rowId = 0;
                var searchId = 0;
                var searchName = "";


                $(document)
                    .ready(function () {


                    alert("document loaded.");


                    $("#editgrid")
                        .jqGrid({

                        url: 'callAJax.action?t=1',
                        datatype: "json",
                        myType: 'GET',
                        colNames: ['name', 'age'],

                        colModel: [

                        {
                            name: 'name',
                            index: 'name',
                            width: 80,
                            editable: true,
                            editoptions: {
                                size: 10
                            }
                        },

                        {
                            name: 'age',
                            index: 'age',
                            width: 90,
                            editable: true,
                            editoptions: {
                                size: 25
                            }
                        }

                        ],

                        rowNum: 10,
                        rowList: [10, 20, 30],
                        pager: '#pagered',
                        sortname: 'name',
                        viewrecords: true,
                        sortorder: "desc",
                        caption: "Editing Example",
                        editurl: "index.jsp"

                    });

                    $("#bedata")
                        .click(function (event) {

                        alert("Inside click function.");
                        jQuery("#editgrid")
                            .jqGrid('getGridParam', 'selrow');

                    });

                });
            </script>
        </head>

        <body>
            <center>
                 <h1>
                Use Ajax in Struts2
            </h1>

                <br/>
                <br/>
                <table id="editgrid"></table>
                <div id="pagered"></div>
                <input type="button" id="bedata" value="Edit Selected" />
            </center>
            <br/>
        </body>

    </html>

Whats is happening, when I run?

Answer:

When I remove $("#editgrid")..jqGrid({}) completely, I see, message 'document loaded'.

When I click button, I see, message 'Inside click function'.

But, when I add partially or completely, the $("#editgrid")..jqGrid({}) block, I only see 'document loaded', but on button click, message 'Inside click function' isn't showing up.

Please let me know, if you would like to have, more information. As always, Thank you!!

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Zafar
  • 105
  • 1
  • 2
  • 13
  • 1
    Not related, but if you're using jQuery, why not use the jQuery Ajax stuff and make your life significantly easier? – Dave Newton Dec 26 '12 at 17:33
  • Thanks. I am looking into jQuery Ajax now. – Zafar Dec 26 '12 at 17:45
  • 2
    See struts2-jquery-plugin http://code.google.com/p/struts2-jquery/. It has grid support also. – Aleksandr M Dec 26 '12 at 19:54
  • @Aleksandr M, I see very little benefit to that plugin and a lot of down side, one of them being the many unanswered questions on here. While an equivalent json/jQuery question is usually quickly addressed. OP: See jQuery.getJSON function for a good way to make the calls and handle the call back. – Quaternion Dec 28 '12 at 00:10
  • @Quaternion: One of the benefits is that you can use tags instead of long javascript code. And for unanswered questions at SO it is not really a down side, at least to me. :) – Aleksandr M Dec 29 '12 at 00:52
  • If there is a script block for the page it is just as short to use a jQuery expression, learning how to use the expression is pretty trivial and pays it self back many times. From there the developer has unlimited flexibility. Now consider the tag library, many developers try to use it to avoid learning JS but the cost is that there is no flexibility, and it impedes understanding. Even if a developer made a very bad attempt with the JS/jQuery, the many good jQuery people on here can salvage even poor examples and produce working results, however using those tags resolution is terrible. – Quaternion Dec 29 '12 at 01:09
  • @Quaternion: And one can create MVC application without using any frameworks. – Aleksandr M Dec 29 '12 at 22:34
  • @AleksandrM I don't understand your point, it just seems like a straw man argument of _using a Java web framework vs programming from JEE first principals_ which has nothing to do with my issues. My point is using XML tags as a replacement for a programming language isn't a particularly far sighted strategy. It has few benefits and many drawbacks. I greatly suspect that someone who took as much time understanding the jQuery documentation would be able to produce results nearly as quickly and have great growth potential. The tag library hinders development in a number of ways: – Quaternion Dec 30 '12 at 23:37
  • 1) Blocks access to a very fast and very responsive jQuery community. As they can't tell what jQuery objects back the tag with any degree of ease. Trivial questions that would be addressed in minutes as are result are often never answered. 2) Hinders flexible development, simple things that the tag library is designed to handle are simple but not particularly any more simple than using jQuery outright and what the tag library was not designed to handle will require writing JS, as a result code is now kept in two locations. 3) Future maintainability, Web UI developers must understand JS... – Quaternion Dec 30 '12 at 23:47
  • they wont likely understand these tags. The tags also are not particularly readable. jQuery often is able to follow the unobtrusive JavaScript practice but these tags encourage a heavy use of parameters which goes against such practice. X) Most of my reasoning comes down to hiding details which should not be hidden produces more harm than good. In other words ajax tag libraries in their current form are examples of how encapsulation isn't always a good thing. – Quaternion Dec 30 '12 at 23:53
  • Thanks everyone!! This is has been resolved. I used Jqxgrid plugin. – Zafar Jan 07 '13 at 02:53

0 Answers0