سؤال

I found myself in need of what i guess should be a trivial requirement. i have a jqGrid in which i have added a custom button in each row. now i am able to associate a client side click event with it but i want to know the key value (Id) in my case of the row whose custom button was clicked, so that i can proceed with this id and do whatever i want to do.

My code for jqGrid is as below

jQuery("#JQGrid").jqGrid({
        url: 'http://localhost:55423/JQGrid/JQGridHandler.ashx',
        datatype: "json",
        colNames: ['', 'Id', 'First Name', 'Created Date', 'Member Price', 'Non Member Price', 'Complete', 'customButton'],
        colModel: [
                    { name: '', index: '', width: 20, formatter: "checkbox", formatoptions: { disabled: false} },
                    { name: 'Id', index: 'Id', width: 20, stype: 'text', sortable: true, key: true },
                    { name: 'FirstName', index: 'FirstName', width: 120, stype: 'text', sortable: true },
                    { name: 'CreatedDate', index: 'CreatedDate', width: 120, editable: true, sortable: true, hidden: true, editrules: { edithidden: true} },
                    { name: 'MemberPrice', index: 'MemberPrice', width: 120, editable: true, sortable: true },
                    { name: 'NonMemberPrice', index: 'NonMemberPrice', width: 120, align: "right", editable: true, sortable: true },
                    { name: 'Complete', index: 'Complete', width: 60, align: "right", editable: true, sortable: true },
                    { name: 'customButton', index: 'customButton', width: 60, align: "right" }
                  ],
        rowNum: 10,
        loadonce: true,
        rowList: [10, 20, 30],
        pager: '#jQGridPager',
        sortname: 'Id',
        viewrecords: true,
        sortorder: 'desc',
        caption: "List Event Details",
        gridComplete: function () {
            jQuery(".jqgrow td input", "#JQGrid").click(function () {
                //alert(options.rowId);
                alert("Capture this event as required");
            });
        }
    });

    jQuery('#JQGrid').jqGrid('navGrid', '#jQGridPager',
               {
                   edit: true,
                   add: true,
                   del: true,
                   search: true,
                   searchtext: "Search",
                   addtext: "Add",
                   edittext: "Edit",
                   deltext:"Delete"
               },
        {/*EDIT EVENTS AND PROPERTIES GOES HERE*/ },
        {/*ADD EVENTS AND PROPERTIES GOES HERE*/},
            {/*DELETE EVENTS AND PROPERTIES GOES HERE*/},
        {/*SEARCH EVENTS AND PROPERTIES GOES HERE*/}
 ); 

Help or any pointers would be much appreciated.

هل كانت مفيدة؟

المحلول

The main solution of your problem in the following: you need include parameter, for example e, in the callback of click handle. The parameter has Event object type which contain target property. Alternatively you can use this in the most cases. Having e.target you can goes to the closest parent <tr> element. It's id is the value which you need:

jQuery(this).find(".jqgrow td input").click(function (e) {
    var rowid = $(e.target).closest("tr").attr("id");
    alert(rowid);
});

Additionally you should make some other modifications in your code to fix some bugs. The usage of

name: '', index: ''

is wrong in colModel. You should specify any non-empty unique name. For example name: 'mycheck'.

Next I recommend you to remove all index properties from colModel. If you use loadonce: true you have to use index properties with the same values as the corresponding name values. If you don't specify any index properties you will have smaller and better readable code. The corresponding values of index properties will be copied by jqGrid internally from the corresponding name values. In the same way you can remove properties like stype: 'text', sortable: true which values are default values (see Default column of the documentation)

The next problem is that you include probably HTML data in the JSON response from the server. (One can't see any formatter for customButton for example). It's not good. In the way you can have problems if the texts of the grid contains special HTML symbols. I find better to use pure data in JSON response from the server. One can use formatters, custom formatters etc on the client side. In the case one can use autoencode: true option of jqGrid which make HTML encoding of all texts displayed in the grid. In the way you will have more safe code which will don't allow any injection (for example no including of JavaScript code during editing of data).

Next remark: I don't recommend you to use gridComplete. The usage of loadComplete is better. See my old answer about the subject.

The last important remark. To handle click events on the buttons placed inside of grid one don't need to bind separate click handle to every button. Instead of that one can use one beforeSelectRow or onCellSelect callback. The answer and this one describe this. The answers use <a> in custom formatter, but <input> works exactly in the same way.

نصائح أخرى

Another approach that can be used to retrieve the id of the row for which the custom button is clicked is by adding formatter to your custom button cloumn

{ name: 'customButton', index: 'customButton', width: 60, align: "right", 
  formatter: function ( cellvalue, options, rowObject ) {

    return "<input type='button' class='custom-button' id='custom"+ rowObject.id +"'/>";
  } 
}

Now every button has the row id

$('input[id^="custom"]').click(function() {
  var id = this.attr('id').replace("custom", ""); // required row ID
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top