jquerygrid with asp.net mvc razor view engine - razor

I'm trying to follow this example on Phil haak's blog using the razor viewengine.
Except my view renders the json as text instead of displaying the grid.
Here is the code for my view :
#{
ViewBag.Title = "Home Page";
}
<h2>#ViewBag.Message</h2>
<script src="../../Scripts/jquery.jqGrid.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery("#list").jqGrid({
url: '/Home/DynamicGridData/',
datatype: 'json',
mtype: 'GET',
colNames: ['Id', 'Votes', 'Title'],
colModel: [
{ name: 'Id', index: 'Id', width: 40, align: 'left' },
{ name: 'Votes', index: 'Votes', width: 40, align: 'left' },
{ name: 'Title', index: 'Title', width: 400, align: 'left'}],
pager: jQuery('#pager'),
rowNum: 10,
rowList: [5, 10, 20, 50],
sortname: 'Id',
sortorder: "desc",
viewrecords: true,
imgpath: '/scripts/themes/coffee/images',
caption: 'My first grid'
});
});
</script>
<table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
<div id="pager" class="scroll" style="text-align:center;"></div>
The rendered page looks like this :
{"total":3,"page":1,"records":22,"rows":[{"i":37,"cell":["37","1","What does AcceptVerbs do?"]},{"i":36,"cell":["36","1","Can I change the default model binder?"]},{"i":35,"cell":["35","1","How do I do custom paging?"]},{"i":34,"cell":["34","1","Writing a custom ControllerFactory"]},{"i":33,"cell":["33","65","How do I defend against CSRF attacks?"]},{"i":32,"cell":["32","2","If there was a random question generator, would you use it?"]},{"i":30,"cell":["30","51","Is there a random question generator?"]},{"i":29,"cell":["29","2","How many questions are in this table?"]},{"i":28,"cell":["28","7","How do I use the SelectList?"]},{"i":27,"cell":["27","50","How do I use jQuery grid with MVC"]}]}

Related

jqGrid: JSON Returned by Web Service Won't Bind to Subgrid

I'm trying to add subgrids to an existing jqGrid.
When I expand a parent row, the URL is called and the JSON response looks good (to my eyes) but it is not being bound to the sub grid rows.
If I call the web service directly by pasting the URL in a browser, the response looks good.
This is the full jqGrid definition:
/* +++ Group Grid Definition +++ */
$("#<%= id %>_groupTable").jqGrid({
loadonce: true,
datatype: 'local',
//sortable: true,
gridview: true,
editurl: "clientArray",
autowidth: true,
rowNum: 12000,
height: 150,
multiselect: false,
hidegrid: false,
pgbuttons: false,
pginput: false,
sortname: "Date",
sortorder: "desc",
jsonReader: {
root: "inbox",
page: "page",
total: "total",
records: "records",
id: "id",
repeatitems: false,
subgrid: { repeatitems: true }
},
toolbar: [true, "bottom"],
colModel: [
{
name: 'userGroupId',
label: '{{VIEW_LABEL}}',
index: 'userGroupId',
width: 50,
fixed: true,
hidden: true,
//sortable: false,
align: 'center'
}, {
name: 'desc',
label: '{{NAME_LABEL}}',
index: 'desc',
width: 130,
align: 'center',
sorttype: 'text'
}],
hidegrid: false,
gridComplete: function () {
$('#<%= id %> .loading').remove();
$('#<%= id %>_groupTable').jqGrid('setSelection', 1);
},
loadComplete: styleGroupRows,
afterInsertRow: styleGroupRows,
subGrid: true,
subgridtype: 'json',
subGridUrl: '/ws/users/groupusers',
subGridModel: [
{
name: ['Name'],
width: [200],
params: ['userGroupId']
}]
}).jqGrid('bindKeys', {
'scrollingRows': true
});
This is the JSON response from the web service:
{'rows':['User 1312','User 1316','User 1286','User 1149']}
I'm expecting a single column of user names in the sub grids but only the column headers appear.
Your subGrid response does not match the rules.
Try with the following response:
{"rows":[{"id":"1","cell":["User 1312"]},{"id":"2","cell":["User 1316"]}]}

Populate Combo box with JSON file, ExtJs 4.x.x

Ext.define('Form', {
extend: 'Ext.data.Model',
fields: [
{name:'type', type:'String'}
]
});
var store = Ext.create('Ext.data.ArrayStore', {
model: 'Form',
autoLoad: true,
proxy: {
type: 'ajax',
url: '/test.json',
reader: {
type : 'json',
root: 'types'
}
}
});
Ext.define('DForms', {
extend: 'Ext.panel.Panel',
bodyPadding: 12,
layout : 'auto',
autoScroll : true,
items: [{
xtype:'selectcombo',
queryMode:'local',
emptyText: 'Select Condition',
store:store,
displayField: 'type',
valueField: 'type',
width : 200,
typeAhead : true
}],
});
When this loads, the selectcombo is empty, nothing gets loaded, i have searched through many sites, and can't find anything to help. Any suggestions would be great
The xtype you look for is combo, the store type is JsonStore because the ArrayStore will not interpret the json from the types root as you might expect. I can not simulate the ajax request here though.
Ext.onReady(function() {
Ext.define('Form', {
extend: 'Ext.data.Model',
fields: [{
name: 'type',
type: 'String'
}]
});
var store = Ext.create('Ext.data.Store', {
model: 'Form',
data: [{
type: 'Aaaaaaa'
}, {
type: 'Bbbbbb'
}, {
type: 'Ccccccccccc'
}, {
type: 'Ddddddd'
}]
});
Ext.define('DForms', {
extend: 'Ext.panel.Panel',
bodyPadding: 12,
layout: 'auto',
autoScroll: true,
items: [{
xtype: 'combo',
queryMode: 'local',
emptyText: 'Select Condition',
store: store,
displayField: 'type',
valueField: 'type',
width: 200,
typeAhead: true
}],
});
Ext.create('DForms', {
renderTo: Ext.getBody()
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/extjs/4.2.1/ext-all-debug.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/extjs/4.2.1/resources/css/ext-all.css" />
I think type in fields is case-sensitive. So try
fields :[{name : 'type', type : 'string'}]
If still not working, as Tejas1991 pointed out check the content of your json.

How to bind JSON data to jqgrid?

I am trying to bind json formatted data to Jqgrid.This is simple website application.I have tried for customized table.Now I want to bind it by using jqgrid.
.aspx or script Code is as follows:
<script src="JS/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: "JQGridBindData.aspx/GetAllDetails",
data:{},
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
success: function (result) {
var data = JSON.parse(result.d);
var array = data.Table;
colNames: ['Country_Code', 'Country', 'ISD_Code']
colModel: [
{name: 'Country_Code', index: 'Country_Code', width: 60, sorttype: "int"},
{name: 'Country', index: 'Country', width: 60, sorttype: "string"},
{name: 'ISD_Code', index: 'ISD_Code', width: 60, sorttype: "string"}
]
},
multiselect: true,
rowNum: 10,
rowList: [5, 10, 20, 50, 100],
pager: jQuery('#pager1'),
sortorder: "desc",
viewrecords: true,
caption: "Manipulating Array Data",
error: function (){
alert(JSON.stringify(Error));
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<table id="jQGridDemo">
</table>
<div id="jQGridDemoPager">
</div>
<br />
<br />
</form>
in my .cs page i am getting the values like this. my .cs side code is
[WebMethod]
public static string GetAllDetails()
{
Generic gn = new Generic();
DataSet ds = gn.ExecuteDataset("Country_master_view", 0);
string data = JsonConvert.SerializeObject(ds,Formatting.Indented);
return data;
}

Combo box and check box in grid for editor in extjs 4?

I design the grid in extjs with editor option. I need the combo box as a column and also need combo box when i edit the grid. The i need delete column as the check box column for both edit and normal grid. Its not working. can any one please help me
Here is the snippet:
this.mcmGridPanel = new Ext.grid.GridPanel({
height: 300,
width: 690,
title: 'Results',
store: store,
multiSelect: true,
x: 0,
y: 170,
columns: [
{ xtype: 'gridcolumn', text: 'FlightNumber', sortable: true, flex: 1, width: 150, dataIndex: 'FlightNumber', hidden: true,
editor: new Ext.form.field.ComboBox({
typeAhead: true,
triggerAction: 'all',
selectOnTab: true,
lazyRender: true,
listClass: 'x-combo-list-small'
})
},
{ xtype: 'gridcolumn', text: 'Origin', sortable: true, width: 150, dataIndex: 'Origin',
editor: {
editor: new Ext.form.field.ComboBox({
typeAhead: true,
triggerAction: 'all',
selectOnTab: true,
lazyRender: true,
listClass: 'x-combo-list-small'
})
}
},
{ xtype: 'gridcolumn', text: 'Destination', sortable: true, width: 150, dataIndex: 'Destination',
editor: {
editor: new Ext.form.field.ComboBox({
typeAhead: true,
triggerAction: 'all',
selectOnTab: true,
lazyRender: true,
listClass: 'x-combo-list-small'
})
}
},
{ xtype: 'datecolumn', text: 'StartDate', width: 80, dataIndex: 'StartAvailability', format: 'Y-m-d',
editor: {
xtype: 'datefield',
allowBlank: false,
format: 'Y-m-d'
}
},
{ header: 'StartTime', text: 'StartTime', width: 60, dataIndex: 'StartAvailabilityTime',
editor: {
xtype: 'timefield',
format: 'H:i',
increment: 15,
allowBlank: false
}
},
{ xtype: 'datecolumn', text: 'EndDate', width: 80, dataIndex: 'EndAvailability', format: 'Y-m-d',
editor: {
xtype: 'datefield',
allowBlank: false,
format: 'Y-m-d'
}
},
{ header: 'EndTime', text: 'EndTime', width: 60, dataIndex: 'EndAvailabilityTime',
editor: {
xtype: 'timefield',
format: 'H:i',
increment: 15,
allowBlank: false
}
},
{
xtype: 'gridcolumn',
text: 'Delete?',
header: 'Delete?',
dataIndex: 'delete',
width: 60,
renderer: function (value, meta, record, rowIndex, colIndex) {
return '<center><input type="checkbox" id="Delete-' + rowIndex + '"/></center>';
},
handler: function() {
},
//disabled: true,
editor: {
xtype: 'checkbox',
cls: 'x-grid-checkheader-editor',
}
}
]
});
I use the following code but Its not working. can any one please help me
I don't see the function that should display the combobox. In order to get this to work automatically, you shoud add the RowEditing plugin.
Try to add this
plugins: [
Ext.create('Ext.grid.plugin.RowEditing', {
clicksToMoveEditor: 1,
autoCancel: false
})
],
Look at this example in the doc. This will help you to get row editing work.
Is your column definition OK?
I got a combobox using this, but I'm aware this is not the full configuration needed:
{
dataIndex: 'name',
flex: 1,
text: 'Name',
field: {
xtype: 'combobox'
},
}
Update
I experienced today a similar problem: my textfields were not rendering.
The culprit was unexpected:
I use a custom template, and the css rules needed for the display were missing. All I had to do was rebuilding the app using sencha cmd sencha app build. This completed the files of the template, and the field of the roweditor displayed correctly.
Chances are that this is also the case with you...

ExtJS Grid doesn't load data

I'm trying to display data in a grid by using JsonStore.
The grid gets rendered, but it doesn't display the data.
The JSON data returned by API.ashx:
[{"Account":{"Username":"root","Password":null,"Enabled":true,"Id":1},"Text":"Hallo Welt!","Id":1},{"Account":{"Username":"root","Password":null,"Enabled":true,"Id":1},"Text":"hihihi","Id":3}]
My code:
Ext.onReady(function () {
var store = new Ext.data.JsonStore({
url: 'API.ashx?type=notes&action=getAll',
root: 'Note',
autoload: true,
fields: ['Text', { name: 'Id', type: 'int'}]
});
var grid = new Ext.grid.GridPanel({
store: store,
columns: [
{
id: 'Note',
header: 'Id',
width: 25,
sortable: true,
dataIndex: 'Id'
},
{
header: 'Text',
width: 160,
sortable: true,
dataIndex: 'Text'
}
],
stripeRows: true,
autoExpandColumn: 'Note',
height: 350,
width: 600,
title: 'Notes',
stateful: true,
stateId: 'grid'
});
store.load();
grid.render('grid-example');
});
I just fixed it myself. I had to remove the option "root" and now it works fine.