I hope you have a good morning.
I'd like to use jqGrid in my Asp.Net app. So far, I've been producing an html table and converting it to a grid, but I'd like to try a better approach: loading the data via an AJAX call as it's supposed with the grid. I found out how to fetch data, but I'd like to have the "Edit" and "Delete" columns in addition to the data columns. How do I achieve it?
I'd rather not craft the html for the buttons in my controller. The ideal solution would be to send just the data, and have jqGrid add the required columns based on some client-side templates and the id values. I can't do it manually since I can't add columns to the existing grid. So, what do I do?
Update. I don't need just to achieve edit/delete functionality. What I need is to add a column with HTML based on some template and the "id" value, like [More details]
Sending pure data back from the server is the correct way if you use jqGrid.
To implement Row editing like you want look at the jqGrid Demo and choose on the left part "Row Editing" and then "Custom Edit". I personally prefer to use so named "inline editing" (choose on the same demo "Input types" under "Row Editing"). You implement switching in editing mode by double-click instead of selecting the row (see jqGrid - edit only certain rows for an editable column as an example). To delete the rows you can use additionally "Delete" button from the "form editing". To use it you should add Navigator with respect of navGrid method and choose with the corresponding parameters the buttons in the toolbar which you need. To see this on the demo choose "Live Data Manipulation" and then "Navigator".
UPDATED: In the jqGrid Demo, see "Row Editing" and then "Custom Edit" you can see how you can use setRowData inside of gridComplete or loadComplete handle to set ANY HTML code fragment. Why do you don't like the method? You can also use predefined showlink formatter to display a link or use custom formatter and custom unformatter to display any HTML contain in a jqGrid cell.
You probably figured it out by now, but for whatever is worth here is my answer using a custom formatter. The Action column is rendered using a custom formatter that shows a button that calls a simple javascript function.
$(document).ready(function () {
$("#all-errors-list").jqGrid({
url: '/Error/AllErrors/',
datatype: 'json',
mtype: 'GET',
colNames: ['Id', 'Error','Actions'],
colModel: [
{ name: 'Id', index: 'Id', width: 100, align: 'left', editable: true},
{ name: 'ErrorDetails', index: 'ErrorDetails', width: 350, align: 'left' },
{ name: 'ActionId', width:400, formatter: actionFormatter}
],
pager: '#all-errors-pager',
rowNum: 10,
rowList: [10, 20, 50],
sortname: 'Id',
sortorder: 'asc',
viewrecords: true,
imgpath: '<%=Html.ImagePath()%>',
caption: 'Open Errors',
height: "100%",
width: "100%",
gridComplete: function () { $("button").button(); }
})
function actionFormatter(cellvalue, options, rowObject) {
return "<button onclick=\"alert('" + cellvalue + "')\">Details</button>" ;
}
I hope that helps.
Related
I'm using UI-Grid 3.0 unstable on an angular page that is loading data via Restangular. So far most of it seems to be ok. I am using the selection module along with it and I'm having difficulty with pages that are loading the {enableFiltering: true} gridOptions in that most of the pages are not showing the input box on the grid.
I invoke the ui-grid on the html page using:
<div class="gridStyle" ui-grid="gridOptions" ui-grid-selection></div>
and my gridOptions in my controller looks like this.
$scope.gridOptions = {
enableFiltering: true,
data: 'data',
columnDefs: [
{field: 'name', displayName: 'Name'},
{field: 'date', displayName: 'Date', cellFilter: 'date:\'MM-dd-yyyy\''}
],
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
}
};
I honestly can't figure out why its not appearing on the page.
I don't understand 100% the problem, but I could go around it by overriding some css. The height in css were 100% and I changed them to auto.
.ui-grid-header-cell{
height:auto;
}
.ui-grid-cell-contents {
height: auto;
}
I hope this would be helpful.
This an awful hack, but I always use the filters, so I just tracked the container that was the wrong height. I had lost hours to this trying to find the cause and using all the suggested fixes.
The container turned out to have the class "ui-grid-header-canvas", so I just set the height to 54px after the grid had sized itself.(after I set the data)
$(".ui-grid-header-canvas").css("height","54px")
Sorry, but it works and I have a deadline..
I am using jqgrid, and added one subgrid inside it, which looks like as below,
As you can see,
Rows with columns having 11 and 13 are main grid rows
And every row has subgrid having interest,
The Add Record element shows Add Pop up for Subgrid
Here is the code for subgrid looks like,
subGridRowExpanded: function(subgrid_id, row_id) {
var subgrid_table_id, pager_id;
subgrid_table_id = subgrid_id+"_t";
pager_id = "p_"+subgrid_table_id;
$("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>");
jQuery("#"+subgrid_table_id).jqGrid({
url:"shops?q=2&ShopID="+row_id,
datatype: "xml",
colNames: ['Interest'],
colModel: [
//{name:"Id",index:"ShopID",width:80,editable:false,editoptions:{readonly:false,size:40}}, //Shop ID not required
{name:"id",index:"id",editable:true,edittype:"select",editoptions:{dataUrl:'shops?q=3&ShopID='+row_id},editrules:{required:true}}
],
rowNum:10,
pager: pager_id,
width: '100%',
height: '100%',
scrollOffset: 0,
sortname: 'num',
sortorder: "asc",
height: '100%',
editurl:'shops?q=5&ShopID='+row_id
});
jQuery("#"+subgrid_table_id).jqGrid('navGrid',"#"+pager_id,{edit:false,add:true,del:true})
},
subGridRowColapsed: function(subgrid_id, row_id) {
// this function is called before removing the data
//var subgrid_table_id;
//subgrid_table_id = subgrid_id+"_t";
//jQuery("#"+subgrid_table_id).remove();
}
Problem is , when there are more than one element in subgrid, i can select that (I use latest version of Chrome) but when there is only single element in subgrid surprisingly I can select it ( if you notice the color difference please see subgrid element 'Gifts' -selected below row 13 ) and once select that 'Gifts' can be deleted.
UPDATE:
In firefox and IE , only first row gets selected from subgrid
Is there something wrong in the code ? why can't i select single element when there are more than one elements in subgrid ?
Appreciate your time, thanks
I suppose that you have problem with id duplicates. HTML don't allow to use id attributes on the same HTML pages with the same values. The values of all id attributes must be unique. I recommend you to verify that you have the problem by usage of Developer Tools of Chrome/IE or using Firebug. You need just examine the id attributes which you have now on <tr> elements of grid and subgrids.
On the other side all rows (all <tr> elements) of jqGrid become an id attribute assigned. Typically you have to fill id in the server side in the response of url. The problem is that one use typically the values from id from the database, but you have typically unique id only over one table of the database and not over all tables on the database. So you can easy have the scenario where multiple jqGrids (or grid with subrird) get rows with id duplicates.
The most easy way to fix the problem is to use idPrefix option which was introduced in jqGrid after my suggestion. The main advantage is that you can continue to use the original id values which you have in the database and use id attributes in jqGrid which will be unique because of building id attributes from the id values returned from the server, but with the usage of prefixes. In the answer. So I recommend you to use different idPrefixes for all subgrids. For example you can use idPrefix: 's' + row_id + '_' in subgrid (see the answer and this one).
I'm facing an issue with jqgrid multiple search (php back-end). The search dialog does not show the "+" (add filter) unless I click on the Reset button...
This is the code:
jQuery("#poi_grid").jqGrid({
url:'php/retrieve_pois.php',
datatype: "json",
colNames:['Name', 'Region', 'Type','Website','PDF','Lat','Lon'],
colModel:[
{name:'name',index:'name', width:150},
{name:'region',index:'region', width:70},
{name:'type',index:'type', width:70},
{name:'website',index:'website', width:90,sortable:false,search:false},
{name:'pdf',index:'pdf', width:150,align:"right",sortable:false,search:false},
{name:'lat',index:'lat', width:60, sortable:false,search:false},
{name:'lon',index:'lon', width:60, sortable:false,search:false},
],
pager:'#pager',
rowNum:20,
rowList:[20,30,50],
sortname: 'name',
viewrecords: true,
sortorder: "asc"
});
//Pager
jQuery("#poi_grid").jqGrid('navGrid','#pager',
{edit:false,add:false,del:false,search:false,refresh:false}
)
//Custom buttons
.navButtonAdd('#pager',{
caption:"",
title:"Search",
buttonicon:"ui-icon search",
onClickButton: function(){
jQuery("#poi_grid").jqGrid('searchGrid',
{sopt:['eq','ne','cn','bw','bn']},
{multipleSearch:true}
);
},
position:"last"
});
This is what I get when I open the search dialog (searching works fine but I have no possibilities to add another filter):
When I click on the rest button in the lower left corner of the dialog (even without performing a search before) I see the "correct" structure of the dialog... and multiple filters search works:
How to solve and obtain the complete dialog without clicking on reset?
Thank you in advance
You made an error in the call of searchGrid. Instead of
jQuery("#poi_grid").jqGrid('searchGrid',
{sopt:['eq','ne','cn','bw','bn']},
{multipleSearch:true}
);
you should use
jQuery("#poi_grid").jqGrid('searchGrid',
{sopt:['eq','ne','cn','bw','bn'], multipleSearch:true}
);
The problem which you describe with "Reset" button is just a bug in single field searching of jqGrid (see more here). Because of wrong usage of searchGrid the multipleSearch:true option will be ignored and the Searching dialog will be used as single field searching.
See this one.
jqGridObj = $('#poi_grid');
jqGridObj.navButtonAdd('#' + jqGridObj[0].id + '_toppager_left',{
caption:"",
title:"Search",
buttonicon:"ui-icon-search",
onClickButton: function(){
jqGridObj.jqGrid('searchGrid',
{sopt:['eq','ne','cn','bw','bn'], multipleSearch:true, showQuery: true}
);
},
position:"last"
});
I have an MVC project, and am working on a page using JQGrid. I have a flyout menu with lots of checkboxes (one for each column of the grid), and I am trying to attach a script to hide/show a column on click.
I've read lots of solutions, and tried using the hide/show methods from the wiki.
$("ShippingListGrid").hideCol("Open");
"Open" is the name of the column in the colModel
{ name: 'Open', index: 'Open', width: 120, align: 'left', editable: true,sortable: true,hidden: false,Key: false, edittype: 'text', editoptions: { size: 20, maxlength: 30}},
This doesn't seem to work though, when I call the function my grid does not change. I've tried reloading my grid and using the column id instead of the name, and the columns still don't change. I've got the latest download. Does anyone know why the function might not be working?
Check your selector, if your grid is a table ID, you will need the # symbol. Maybe try: $("#ShippingListGrid").hideCol("Open");
So, in my ASP.NET MVC 3 app, I've got pages with jqGrids on them. I've customized the search operations on a per-column basis like so:
colModel: [
{ name: 'IceCreamName', index: 'IceCreamName', align: 'left',
searchoptions: {sopt: ['eq', 'ne', 'cn']} },
.
.
.
{ name: 'InitialDate', index: 'InitialDate', align: 'left',
searchoptions: {sopt: ['eq', 'ne', 'gt', 'lt', 'ge', 'le']} },
{ name: 'Volume', index: 'Volume', align: 'left',
searchoptions: {sopt: ['eq', 'ne', 'gt', 'lt', 'ge', 'le']} }
]
And that'll give me, for this example, Equal, NotEqual, Contains on the name, and Equal, NotEqual, GreaterThan, LessThan, GreaterThanOrEqual and LessThanOrEqual on the date and volume columns. Wonderful. That customization is great.
What I'd particularly like to do though is customize the strings displayed in the search operation dropdown for those columns. For instance, for the InitalDate column, I'd like the 'gt' to display "after" instead of "greater" (makes more sense to the user) and something like "on or before" instead of "less or equal".
I see that I can modify those globally in the grid.locale-en.js (or whatever locale is correct) but that is at a global level. I'd like the date and volume columns to have strings that are specific to those columns.
Any way to do that? Perhaps I missed something in the documentation on how to accomplish that.
You don't wrote which version of jqGrid you use, so I suppose that you use the last 4.0.0 version of jqGrid.
There are no jqGrid option of couse which can make searching dialog like you as want. I find your question very interesting, so I extended the code of my this and this old answers so that it do wat you need.
The demo uses jqGrid where 'invdate' column ('Date' column) has as the searchoptions the following:
searchoptions:{sopt: ['eq', 'ne', 'gt', 'lt', 'ge', 'le'],
optDescriptions: {eq:'my eq', gt:'after', le:'on or before'}}
The optDescriptions is my extension. I use onInitializeSearch method of the searching dialog where I overwrite reDraw and onchange method of the filter dialog so that the new optDescriptions property will be used. Additionally in the demo like in the previous demos I don't permit to remove the first searching rule and set the focus on the last input field.
I recommend you to place new feature request in the trirand forum to make the feature are implemented in jqGrid per default.