I have included my code (so far). I am trying to use jqGrid in conjunction with ASP.NET MVC. The post and get methods all work fine. There is still lots to improve but the multiple posting after the first submission is a major bug at the moment . Also the:
$("#list").trigger("reload
does not work in firfox but in IE. But that's another story I guess.
I have used JavaScript on and off so please be gentle with me ...
C
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" media="screen" href="../../Content/jqGridthemes/steel/grid.css" />
<link rel="stylesheet" type="text/css" media="screen" href="../../Content/jqGridthemes/jqModal.css" />
<script type="text/javascript" src="../../Scripts/jquery.jqGrid.js"></script>
<script src="../../Scripts/jqGrid/jqModal.js" type="text/javascript"></script>
<script src="../../Scripts/jqGrid/jqDnR.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.form.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#indicator").hide()
$("#list").jqGrid({
height: 'auto',
width: 700,
url: '../../Test/Data?nd=+ new Date().getTime(),
datatype: 'json',
mtype: 'GET',
colNames: ['pk', 'col1', 'col2', 'col3'],
colModel: [
{ name: 'pk', index: 'pk', width: 55, resizable: true, sorttype: "int" },
{ name: 'col1', index: 'col1', width: 120, editable: true, edittype: "text", resizable: true, sorttype: "text" },
{ name: 'col2', index: 'col2', width: 120, editable: true, edittype: "text", resizable: true, sorttype: "text" },
{ name: 'col3', index: 'col3', width: 120, editable: true, edittype: "text", resizable: true, sorttype: "text" }
],
pager: $('#pager'),
rowList: [10, 20, 30],
rowNum: 10,
userdata: 'userdata',
editurl: "../../Test/getColumnValues",
sortname: 'pk',
sortorder: 'desc',
viewrecords: true,
imgpath: '../../Content/jqGridthemes/steel/images',
editurl: "../../Test/EditData",
caption: 'My first grid',
multiselect: true,
toolbar: [true, "top"],
// display any userdata returned from the server
loadComplete:
function() {
var userdata = $("#list").getUserData();
appendMessage(userdata);
},
loadError:
function(xhr, st, err) {
$("#success_message").append("Type: " + st + "; Response: " + xhr.status + " " + xhr.statusText + "<br\>");
}
}).navGrid('#pager', { edit: false, add: false, del: true, search: true, refresh: true, searchtext: "Filter", recordtext: "Records", addtext: "Add", deltext: "Delete", edittext: "Edit", refreshtext: "Refresh" }
)
.navButtonAdd("#pager", { caption: "Search", title: "Toggle Search", buttonimg: '../../Content/jqGridthemes/steel/images/find.gif',
onClickButton: function() {
if (jQuery("#t_list").css("display") == "none") {
jQuery("#t_list").css("display", "");
} else {
jQuery("#t_list").css("display", "none");
}
}
})
.navButtonAdd('#pager', { caption: "Edit", buttonimg: '../../Content/jqGridthemes/steel/images/row_edit.gif',
onClickButton: function() {
var pk = jQuery("#list").getGridParam('selrow'); // returns id of selected row
if (pk) {
$.getJSON('../../Test/Item', { pk: pk }, function(data) {
// insert data into form
$('#pk').val(data.item.pk);
$('#col1').val(data.item.col1);
$('#col2').val(data.item.col2);
$('#col3').val(data.item.col3);
});
} else {
alert("Please select row!")
}
}
})
jQuery("#savedata").click(function() {
$("form#item_form").submit(function() {
hijack(this, CUOperation, "json");
return false;
});
});
jQuery("#clear").click(clearForm);
jQuery("#t_list").height(200).hide().filterGrid("list", { gridModel: false, gridToolbar: false, gridNames: true, enableClear: true, formtype: "vertical", enableSearch: true,
filterModel:
[
{ label: 'col1', name: 'col1', stype: "select", surl: "../../Test/getColumnValues?table=test1&column=col1" },
{ label: 'col2', name: 'col2', stype: "select", surl: "../../Test/getColumnValues?table=test1&column=col2" },
{ label: 'col3', name: 'col3', stype: "select", surl: "../../Test/getColumnValues?table=test1&column=col3" }
]
});
$.extend($.jgrid.search, { caption: "Column filter", Find: 'Search', width: '400', checkInput: true });
});
function commonSubmit(data, params) {
var userdata = eval("(" + data.responseText + ")"); //Convert returned string into JSON
appendMessage(userdata);
return true;
}
function hijack(form, callback, format) {
$("#indicator").show();
$.ajax({
url: form.action,
type: form.method,
dataType: format,
data: $(form).serialize(),
completed: $("#indicator").hide(),
success: callback
});
}
function CUOperation(result) {
$("#success_message").append("Item added. <br />");
clearForm();
$("#list").trigger("reloadGrid");
}
function appendMessage(userdata) {
if (userdata.TYPE == "error") {
$("#error_message").append(userdata.MSG + "<br />");
} else {
$("#success_message").append(userdata.MSG + "<br />");
}
}
function clearForm() {
$(':input', item_form).each(function() {
var type = this.type;
var tag = this.tagName.toLowerCase(); // normalize case
// it's ok to reset the value attr of text inputs,
// password inputs, and textareas
if (type == 'text' || type == 'password' || tag == 'textarea')
this.value = "";
// checkboxes and radios need to have their checked state cleared
// but should *not* have their 'value' changed
else if (type == 'checkbox' || type == 'radio')
this.checked = false;
// select elements need to have their 'selectedIndex' property set to -1
// (this works for both single and multiple select elements)
else if (tag == 'select')
this.selectedIndex = -1;
});
}
</script>
</head>
<body>
<div id="srccontents"></div>
<p id="success_message" style="color:green;">
</p>
<p id="error_message" style="color:red;">
</p>
<form method="post" name="item_form" id="item_form" action="../Test/Add" style="width:350px;margin:0px;">
<fieldset>
<legend>A simple form</legend>
<table>
<tbody>
<tr>
<td> pk:</td>
<td><input type="text" name="pk" readonly=readonly id="pk"/></td>
</tr>
<tr>
<td> col1:</td>
<td><input type="text" name="col1" id="col1" /></td>
</tr>
<tr>
<td> col2:</td>
<td><input type="text" name="col2" id="col2" /></td>
</tr>
<tr>
<td> col3:</td>
<td><input type="text" name="col3" id="col3" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" id="savedata" value="Save" /></td>
</tr>
<tr>
<td> </td>
<td><input type="button" id="clear" value="Clear" /></td>
</tr>
</tbody>
</table>
</fieldset>
</form>
<img id="indicator" src="../../Content/jqGridthemes/steel/images/loading.gif" />
<!-- the grid definition in html is a table tag with class 'scroll' -->
<table id="list" class="scroll" cellpadding="0" cellspacing="0" width="100%">
</table>
<!-- pager definition. class scroll tells that we want to use the same theme as grid -->
<div id="pager" class="scroll" style="text-align: center;"></div>
</body>
</html>
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234:





by: quincydudePosted on 2009-03-26 at 18:01:15ID: 23997244
can u give the code on how to invoke hijack()?
the problem may be there