在网格的loadComplete中设置单选按钮

时间:2012-08-02 20:45:26

标签: jqgrid

        $("#addressList").jqGrid({
        url: '/Storage/Shipping/GetCustomerAddresses?q=2&Customerid=' + $("#saveCustomerID").val(),
        datatype: "Json",
        jsonReader: {
            root: "Data.rows",
            page: "Data.page",
            total: "Data.total",
            records: "Data.records",
            repeatitems: true,
            userdata: "userdata",
            cell: "cell"
        },
        colNames: ['', 'Line 1', 'Line 2', 'City', 'State'],
        colModel: [
                         { name: 'myradio', width: 30, fixed: true, align: 'center', resizable: false, sortable: false,
                             formatter: function (cellValue, option) {
                                 return '<input type="radio" name="radio_' + option.gid + '" />';
                             } 
                         },
                         { name: 'Line1', index: 'Line1', width: 250 },
                         { name: 'Line2', index: 'Line2', width: 250 },
                         { name: 'City', index: 'City', width: 210 },
                         { name: 'State', index: 'State', width: 75 }
                    ],
        page: 1,
        rowNum: 50,
        rowList: [20, 50, 100],
        pager: '#pager',
        viewrecords: true,
        grouping: false,
        caption: "Addresses",
        mtype: "POST",
        width: "100%",
        height: "100%",
        loadonce: true,
        sortable: false,
        beforeSelectRow: function (rowid, e) {
            var radio = $(e.target).closest('tr').find('input[type="radio"]');
            radio.attr('checked', 'checked');
            $("#saveCustomerAddressID").val(rowid.toString());
            return true; // allow row selection
        },
        loadComplete: function () {
            var grid_ids = $("#addressList").jqGrid('getDataIDs');
            for (var i = 0; i < grid_ids.length; i++) {
                if ($("#saveCustomerAddressID").val() == grid_ids[i]) {
                    {
                        $("#addressList").jqGrid('setSelection', grid_ids[i], true);

                    }
                }
            }
        }
        //, postdata: { CustomerID: $("#saveCustomerID").val() }
    }); 

上面的代码在loadcomplete中正确设置了选择。 $(“#saveCustomerAddressID”)。val()是JSON数据的rowid。 radiobutton在beforeSelectRow中设置。我知道要设置的radiobutton的行号和网格列,但是如何设置radiobutton?

1 个答案:

答案 0 :(得分:0)

如果你有行号和网格列,那就不难了。获取该列的id(css),其中包含单选按钮(请查看开发人员工具)。现在可以说单选按钮的名称是“Demo”

所以我觉得id看起来像是这样的东西

var demo= $("'#'+rowid+'Demo'")//check developer tools for confirmation

demo.attr('checked', 'checked');
$("#saveCustomerAddressID").val(rowid.toString());//continue with you loadComplete code

并且对于集合选择使用jqgrid的setSelection方法之后继续使用loadcomplete的代码