Javascript 在按钮单击事件中执行操作,按钮放置在dojox.grid.DataGrid中。

Javascript 在按钮单击事件中执行操作,按钮放置在dojox.grid.DataGrid中。,javascript,jsp,button,dojo,dojox.grid.datagrid,Javascript,Jsp,Button,Dojo,Dojox.grid.datagrid,我有一个dojox.grid.DataGrid。在这种情况下,将显示一组值,最后两列将填充按钮,这些按钮根据使用gridstructure中的formatter属性从数据库检索的数据动态创建。现在我的网格恢复得很好。按钮也很好。我现在需要做的是,当我在按钮点击事件上点击一个特定的按钮时,我将其重定向到一个新的URL,其中一个特定的值(a)作为该URL中的查询字符串参数传递。我不希望我的页面被刷新。就像单击按钮时,它会在其他JSP页面上执行操作,并显示消息警报(“正在执行操作”) 我为数据网格编码

我有一个
dojox.grid.DataGrid
。在这种情况下,将显示一组值,最后两列将填充
按钮
,这些按钮根据使用
gridstructure
中的
formatter
属性从数据库检索的数据动态创建。现在我的网格恢复得很好。按钮也很好。我现在需要做的是,当我在按钮点击事件上点击一个特定的按钮时,我将其重定向到一个新的URL,其中一个特定的值(a)作为该URL中的查询字符串参数传递。我不希望我的页面被刷新。就像单击按钮时,它会在其他JSP页面上执行操作,并显示消息
警报(“正在执行操作”)

我为数据网格编码的java脚本代码:

<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>
然后单击本页
删除
列中的
按钮

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

打开时显示从数据库检索的项的值。我还应用了一个
rowClick
事件,这也会导致问题,因为当我单击u按钮时,我的
rowClick
事件会触发,而不是按钮单击事件。如何做到这一点。我想把点击事件应用到网格上的每个按钮上。但我不知道。请帮我做这个。谢谢

我认为,您需要调整服务器端代码以处理发送邮件的ajax post请求,并在用户单击按钮时使用
dojo.xhrPost
方法。您的JS代码可能如下所示:

  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>";
  }
函数sendMailHandler(evt,项){
dojo.xhrPost({
url:“/2_8_2012/jsp/sendmailrementer.jsp”,
内容:{
“SendMail”:项目
},
错误:函数(){
警报(“发送失败”);
},
加载:函数(结果){
警报(“发送结果为:+结果的电子邮件”);
}
});
dojo.stopEvent(evt);
}
发送邮件功能(项目){
返回“发送邮件”;
}
注意,
dojo.stopEvent(evt)sendMailHandler
中的code>用于停止事件冒泡并防止
RowClick
引发

还有类似语法的
dojo.xhrGet
来执行ajax GET请求,您可以使用它来代替jQuery的
$.GET
。在我的示例中,您还可以使用
dojo.xhrGet
而不是
dojo.xhrPost
,因为它有可能在不调整的情况下与后端一起工作,但是
POST
(或ajax表单提交)在语义上更为正确


关于“试图注册一个id=”什么,您应该调整代码以避免ID重复。或者显示导致错误的代码。

在其他JSP页面上执行操作是什么意思?您想在弹出窗口中打开另一个JSP,还是发送ajax请求以执行某些服务器端操作或其他操作?在该JSP sendmailrementer.JSP上。我有一个发送邮件类的对象。此对象调用类的特定函数,该函数将邮件发送到从查询字符串中的项接收的值。因此,基本上我需要将item的这个值作为参数传递给sendmailrementer.jsp中被调用的函数。或者我能做的就是只在同一个页面上调用该方法,而不将其重定向到sendmailrementer.jsp。如果第二个是可能的,请建议我一个方法来做到这一点?谢谢,先生,我一直收到一个错误“试图注册一个id=”但该id已经注册“。我知道这是一个非常常见的错误,与从dojo注册表中删除id或其他相关。但我不清楚那是什么。以及如何克服这个问题。谢谢我在当前页面中没有收到此错误(询问问题)。但在其他一些页面上不断出现。在第一次尝试中,效果很好:)非常感谢,先生。。你能帮我做一下这个链接吗,dojo新手遇到的问题。。再次感谢。。。
  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>";
  }