Reputation: 21
I have a jqgrid with some data. When two of the rows are selected, I'd like to add a record in the database (in a different table than the one of the jqgrid). What I do is to create a new button in the navigation bar with the following code:
jQuery("#tabla").navButtonAdd('#navegacion',
{
caption: "Crear tramo", buttonicon: "ui-icon-extlink", cursor:"pointer", title: "Crear tramo",
onClickButton: $(function() {
var selectedrows = $("#tabla").jqGrid('getGridParam','selarrrow');
var er1=$("#tabla").jqGrid('getRowData',selectedrows[0]);
var er2=$("#tabla").jqGrid('getRowData',selectedrows[1]);
$.ajax({
dataType: 'json',
mtype: 'POST',
url: 'json/operaciones.jsp',
postData: { oper:'add', id1:er1, id2:er2},
success: function(data) {
alert(data);
}
})
})
});
And what I have in the file operaciones.jsp is:
<%
String salida="";
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
Connection con = DriverManager.getConnection("my", "Connection", "Parameters");
try {
System.out.println("Parámetros enviados:");
Enumeration e=request.getParameterNames();
while (e.hasMoreElements()){
String par=(String)e.nextElement();
System.out.println(par+":"+request.getParameter(par));
}
String operacion=request.getParameter("oper");
String id1=request.getParameter("id2");
String id2=request.getParameter("id2");
String comentarios="prueba";
if (operacion.equals("add")) {
PreparedStatement stmt=con.prepareStatement(
"INSERT INTO TRAMOS "+
"(COMENTARIOS,ELEMENTOS_REGISTROCODIGO,ELEMENTOS_REGISTROCODIGO2, ENTIDADESCIF) "+
"VALUES (?,?,?,?,?)");
stmt.setString(1,comentarios);
stmt.setString(2,id1);
stmt.setString(3,id2);
stmt.setString(4,"Q1818002F");
stmt.executeUpdate();
stmt.close();
salida="{\"correcto\":true,\"clave\":\""+id1+"|"+id2+"\",\"mensaje\":\"OK\"}";
}
}
catch (Exception e) {
String cadenaError=e.toString().substring(e.toString().indexOf(":")+2);
cadenaError=cadenaError.replace('\n',' ');
cadenaError=cadenaError.replace("\"","\\\"");
salida="{\"correcto\":false,\"clave\":\"\",\"mensaje\":\""+e.toString()+"\"}";
}
finally {
con.close();
}
System.out.println("salida:"+salida);
response.setContentType("application/json");
%>
<%=salida%>
But it's not working, what I get is "java.lang.NullPointerException". Where is the mistake? How can I send the data to the file operaciones.jsp? Thanks in advanced, Natalia
Upvotes: 2
Views: 594
Reputation: 21
It was a stupid error... What finally worked:
jQuery("#tabla").navButtonAdd('#navegacion',
{
caption: "Crear tramo", buttonicon: "ui-icon-extlink", cursor:"pointer", title: "Crear tramo",
onClickButton: function() {
selectedrows = $("#tabla").jqGrid('getGridParam','selarrrow');
if(selectedrows.length==2){
er1=$("#tabla").jqGrid('getRowData',selectedrows[0]);
er2=$("#tabla").jqGrid('getRowData',selectedrows[1]);
$.ajax({
datatype:'json',
type:'GET',
url:'json/operacionesTramos.jsp',
data:{oper:'add',id1:er1.codigo,id2:er2.codigo},
success: function(data) {
alert("Tramo creado");
}
})
}
else{
alert("Seleccione dos arquetas, por favor");
}
}
});
Thanks Oleg!
Upvotes: 0
Reputation: 221997
One clear error in the code which you use is the usage of postData
instead of data
and mtype
instead of type
as parameters of jQuery.ajax.
I recommend you additionally to write more safe code. For example you use selectedrows[0]
and selectedrows[1]
inside of the onClickButton
before verifying that selectedrows
is not null
and that selectedrows.length > 1
. If the user click for example on the button before at least two lines are selected one get exception in JavaScript code.
Upvotes: 1