Performing action a button click event, button bei

2019-09-02 02:41发布

问题:

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 "shan@gmail.com" 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..

回答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.