Performing action a button click event, button being placed inside dojox.grid.DataGrid.
Asked Answered
F

1

0

I have a dojox.grid.DataGrid. In this a set of values are being displayed along with last 2 columns being filled up with buttons which are created dynamically according to data being retrieved from database using formatter property in gridStruture. Now i am getting the my grid fine. Buttons are also coming up fine. What i need to do now is when i click on a particular button on that button click event i redirect it to a new URL with a particular value(A) being passes as a query string parameter in that URL. And i don't want my page to be refreshed. Its like when a button is clicked it performs action on some other JSP page and displays message alert("Action is being performed").

My java script code where i have coded for my data grid ::

<script type="text/javascript">
function getInfoFromServer(){
        $.get("http://localhost:8080/2_8_2012/jsp/GetJson.jsp?random=" + new Date().getTime(), function (result) {
        success:postToPage(result),
        alert('Load was performed.');
            },"json");
    }


    function postToPage(data){
    alert(data);    
    var storedata = {
        identifier:"ActID",
        items: data
        };
    alert(storedata);

    var store1 = new dojo.data.ItemFileWriteStore({data: storedata}) ;
    var gridStructure =[[

                          { field: "ActID",
                                name: "Activity ID",

                                classes:"firstName"
                          },
                          {
                              field: "Assigned To",
                              name: "Assigned To",

                              classes: "firstName"
                          },
                          { field: "Activity Type",
                                name: "Activity Type",

                                classes:"firstName"
                          },
                          {
                              field: "Status",
                              name: "Status",

                              classes: "firstName"
                          },
                          {
                              field: "Assigned Date",
                              name: "Assigned Date",

                              classes: "firstName"
                          },
                          {
                              field: "Assigned Time",
                              name: "Assigned Time",

                              classes: "firstName"
                          },
                          {
                              field: "Email",
                              name: "Send Mail",
                              formatter: sendmail,
                              classes: "firstName"

                          },
                          {
                              field: "ActID",
                              name: "Delete",
                              formatter: deleteact,
                                classes: "firstName"
                          }

                    ]
              ];
    //var grid = dijit.byId("gridDiv");
    //grid.setStore(store1);

    var grid = new dojox.grid.DataGrid({

        store: store1,
        structure: gridStructure,
        rowSelector: '30px',
        selectionMode: "single",
        autoHeight:true,
        columnReordering:true

        },'gridDiv');

    grid.startup();
    dojo.connect(grid, "onRowClick", grid, function(){
                var items = grid.selection.getSelected();
                dojo.forEach(items, function(item){
                    var v = grid.store.getValue(item, "ActID");
                    getdetailsfordialog(v);

                    function showDialog() {
                        dojo.require('dijit.Tooltip');
                        dijit.byId("terms").show();
                    }

                    showDialog();
                    }, grid);

            });
}

function sendmail(item) {
    alert(item);
      return "<button onclick=http://localhost:8080/2_8_2012/jsp/SendMailReminder.jsp?Send Mail="+item+"'\">Send Mail</button>";

    }
function deleteact(item) {
    alert(item);
      return "<button onclick=http://localhost:8080/2_8_2012/jsp/DeleteActivity.jsp?Activity ID="+item+"'\">Delete</button>";
    }
</script>

I am getting grid data using $.get call. In the above code field Email and ActID are actually buttons being created when each time function sendmail and deleteact are being called up in formatter. Grid is displayed. Also the value of alert(item) in both functions are coming up right that is there respective values. Like for alert(item) in Delete i am getting ActID and alert(item) in sendmail getting "[email protected]" Now i want that on a particular button click(button in Sendmail column) my page

http://localhost:8080/2_8_2012/jsp/SendMailReminder.jsp?Send Mail="+item+"'

and button click in Delete column this page

http://localhost:8080/2_8_2012/jsp/DeleteActivity.jsp?Activity ID="+item+"'\"

opens up with value of items being retrieved from database. I have applied a rowClick event also which is also causing problem as when i click u button my Rowclick event fires instead of button click event. How to do this. I thought of applying click event to each button on grid. But there ID i don't know. Please help me on this one. Thanks..

Frolicsome answered 21/4, 2012 at 11:2 Comment(3)
What do you mean by performs action on some other JSP page? Do you want to open this other JSP in popup window, or send ajax request to perform some server-side action or something else?Brewer
On that JSP SendMailReminder.jsp. I have an object of a class Send mail. This object calls a particular function of class that sends a mail to the value recived from item in query string. So basically i need to pass this value of item as a parameter to the called function in my SendMailReminder.jsp. Or what i can do is call that method on this same page only without redirecting it to SendMailReminder.jsp. If second one is possible please suggest me a way to do that ? ThanksFrolicsome
And Sir i keep on receiving an error "Tried to register an id="something" but that id is already registered" . I know its a pretty common error and gt something to do with removing the id from dojo registery or something. But i dnt understand clearly what it is. And how to overcome this. Thanks.. And i am not receving this error in my current page(Ques being asked). But keeps on getting it on some other pages.Frolicsome
B
1

I think, what you need is adjusting your server-side code to handle ajax post requests for sending mail and use dojo.xhrPost method when user clicks button. Your JS code may look like this:

  function sendMailHandler(evt, item) {
    dojo.xhrPost({
      url: "/2_8_2012/jsp/SendMailReminder.jsp",
      content: {
        'SendMail': item
      },
      error: function() {
        alert("Sent failure");
      },
      load: function(result) {
        alert("Email sent with result: " + result);
      }
    });

    dojo.stopEvent(evt);
  }

  function sendmail(item) {
    return "<button onclick='sendMailHandler(arguments[0], \"" + item +  "\")'>Send Mail</button>";
  }

Note that dojo.stopEvent(evt); in sendMailHandler is used to stop event bubbling and prevents RowClick raising.

There is also dojo.xhrGet with similar syntax to perform ajax GET requests, which you can use instead of jQuery's $.get. You can also use dojo.xhrGet instead of dojo.xhrPost in my example, because there is chance that it will work with your back-end without tweaking, but POST (or ajax form submission) would be more semantically correct.

And about "Tried to register an id="something", you should adjust your code to avoid IDs duplication. Or show your code causing errors.

Brewer answered 23/4, 2012 at 7:25 Comment(1)
That worked fine, in the first attempt.. :) Thanks a lot sir.. Can u help me with this link also, New to dojo Getting probs.. Thanks again ... https://mcmap.net/q/1329179/-refreshing-dojogridFrolicsome

© 2022 - 2024 — McMap. All rights reserved.