jqGrid, couldn't get showlink formatter working
Hi I am trying to get showlink formatter working by following this document from trirand.
What I want to achieve is a hyperlink I can click for a edit view to update/edit records. But for some reason, the column are empty where I want show a hyperlink.
Here is my code snippets, and link is the last column:
<script type="text/javascript">
$(document).ready(function () {
$("#grid_products").jqGrid({
jsonReader: {
repeatitems: false,
开发者_StackOverflow社区 id: 'Guid'
},
url: '/Product/jqgridJSON/',
datatype: 'json',
mtype: 'GET',
colNames: ['ProductCode', 'ProductDescription', 'DefaultSellPrice', 'LastCost', 'Edit'],
colModel: [
{ name: 'ProductCode', index: 'Productcode' },
{ name: 'ProductDescription', index: 'ProductDescription' },
{ name: 'DefaultSellPrice', formatter: 'currency', index: 'DefaultSellPrice' },
{ name: 'LastCost', formatter: 'currency', index: 'LastCost' },
{ name: 'MyLink',
edittype: 'select',
formatter: 'showlink',
formatoptions: { baseLinkUrl: '/Product/Update/', idName: 'Guid' }
},
],
pager: '#pager',
rowNum: 10,
rowList: [20, 50, 100, 200],
sortname: 'ProductCode',
sortorder: 'asc',
viewrecords: true,
width: 'auto',
height: 'auto',
caption: 'Products'
}).navGrid('#pager', { edit: true, add: false, del: false });
});
</script>
@{
ViewBag.Title = "JSONGrid";
}
<h2>JSONGrid</h2>
<table id="grid_products"></table>
<div id="pager"></div>
The formatter from jqGrid is working for currency, but for some reason, it just didn't shown for hyperlink.
Update:
Got it working by using custom formatter.
...
{ name: 'MyLink',
formatter: myLinkFormatter,
},
...
function myLinkFormatter (cellvalue, options, rowObjcet) {
return '<a href = "/Product/Edit/' + options.rowId + '">Edit this product</a>';
}
I suppose that you fill no value in JSON input for the 'MyLink'
column. Because of this the hyperlink is empty. If you want to place the link with any fixed text in column I would recommend you to use custom formatter. See the recent answer for an example.
One more possible solution way is to use formatter: 'showlink'
and include jsonmap: function() { return "Edit"; }
to the 'MyLink' column definition. In the case you will not need to include in the JSON data "MyLink":"Edit"
for every row of data. It's important to understand that the trick works only in case of usage jsonReader: {repeatitems: false}
(so it should work for your grid).
If you have another problem you should include in the text of your question the JSON data which you use.
Some small remarks to your current code:
- the usage of
edittype: 'select'
together withformatter: 'showlink'
has no sense. You should remove it if you will do useformatter: 'showlink'
. - the parameter
height: 'atuo'
should beheight: 'auto'
. pager: $('#pager')
is better to replace topager: '#pager'
. If you usepager: $('#pager')
, the jqGrid will replace it internally topager: '#pager'
and the object$('#pager')
will be discarded.- If you use
jsonReader: { id: 'Guid'}
and you don't plan to show the guids to the user you can remove the'Guid'
column from the grid. Theid
(theGuid
in your case) will be used to assign ids of<tr>
elements (table rows) of the grid. So you don't need to hold the same information twice
精彩评论