views:

166

answers:

2

Dearest All,

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 <a href="MoreDetails/{id}">[More details]</a>

+1  A: 

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 http://stackoverflow.com/questions/2863874/jqgrid-edit-only-certain-rows-for-an-editable-column/2866685#2866685 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.

Oleg
Thanks a lot for your answer,However, this is not what I need. I already have implemented a custom edit dialog, what I need is a link that invokes it. And in general, I need to add command columns for various reasons, not just deleting or editing.
ulu
@ulu: What do you mean under "link that invokes it"? If you have the custom edit dialog you can add buttons with actions (one button with any icon or text per action) which you need in the Navigator which invoke the dialog. If the user select a row and press a button on the navigator you can invoke your dialog with the data from the selected row. If you want to add action buttons in every **row** you can follow the demo "Row Editing" and then "Custom Edit". You can call invoke your custom dialog instead of `editRow`, `saveRow` and `restoreRow` used in the demo.
Oleg
Oleg,Forget about editing. What I need is an additional column with custom html, which is either a link like "/Some/Other/{id}/page" or a button invoking a parametrized function. Something like a templated column in a GridView.
ulu
@ulu: I updated (appended) my answer. It seems to me that what you want you can implement very easy. Please read look at the examples which I posted before more careful. There contain the solution of your problem, but you should makes some small modifications based on the idea from the examples. If I do misunderstand your question, you should explain your problem in other words.
Oleg
+1  A: 

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.

boca
Yes, as Oleg suggested, I used a custom formatter. Thanks anyway!
ulu