Jqgrid自定义格式化程序按钮单击不起作用

时间:2014-01-20 23:14:56

标签: javascript jquery html asp.net-mvc-4 jqgrid

我创建了一个网格,我的一个列在每一行都有一个自定义按钮。当我单击该按钮时,我的click事件不会被调用。

我的jqgrid:

$('#QuoteLineTable').jqGrid({
        url: $('#url').val(),
        datatype: 'json',
        type: 'POST',
        postData: { Id: $("#Id").val() },
        colNames: ['Id', 'Quote Number', 'Valid Until Date','View Line Item'],
        colModel: [
            { name: "QuoteLineId", index: "QuoteLineId", hidden: false, hidedlg: true },
            { name: 'QuoteNumber', index: "QuoteNumber" },
            { name: 'ValidUntil', formatter: "date", formatoptions: { newformat: "d/m/Y" }, width: '100px' },
            { name: 'View Line Item', formatter: viewLineBtn }

        ],
        multiselect: true,
        emptyrecords: "No Quote Line to view",
        gridview: true,
        autoencode: true,
        loadtext: "Loading...",
        loadonce: true,
        rowNum: 3,
        rowList: [10, 20, 30],
        pager: '#LinePager',
        height: '100%',
        caption: "Quote List",
        autowidth: true,
        sortname: 'QuoteNumber',
        ajaxGridOptions: { type: 'POST', contentType: "application/json; charset=utf-8" },
        jsonReader: {
            repeatitems: false,
            root: "rows",
            page: "page",
            total: "totalPages",
            records: "totalRecords",
            id: "QuoteLineId"
        },
        serializeGridData: function(postData) {
            return JSON.stringify(postData);
        },
        onCellSelect: function(rowid,e) {

                alert("rowid=" + rowid );

        },

        ondblClickRow: function(rowid) {

            var $model = $('#LineItemMyModal');

            $.ajax({
                type: "GET",
                url: $('#urlItemDetails').val(),
                data: { LineId: rowid },
                success: function(r) {
                    $model.html(r);
                    $model.modal('show');
                }
            });


        }
    }).navGrid('#QuoteLinePager', { edit: false, add: false, del: false, search: true });


    function viewLineBtn(cellvalue, options, rowObject) {
        return "<button class=\"viewLineItem\">View Line Item</button>"
    };


$('.viewLineItem').click(function (rowId) {
   alert("hi");
    alert(rowId);
});

基本上我不知道如何为button class = viewLineItem调用click事件。

我尝试使用onCellSelect或beforeSelectRow事件,但我还需要使用ondblClickRow来填充模式。所以我在不使用oncellSelect的情况下寻找其他选项。

2 个答案:

答案 0 :(得分:1)

尝试这样的事情。

    $('#QuoteLineTable').jqGrid({
            url: $('#url').val(),
            datatype: 'json',
            type: 'POST',
            postData: { Id: $("#Id").val() },
            colNames: ['Id', 'Quote Number', 'Valid Until Date','View Line Item'],
            colModel: [
                { name: "QuoteLineId", index: "QuoteLineId", hidden: false, hidedlg: true },
                { name: 'QuoteNumber', index: "QuoteNumber" },
                { name: 'ValidUntil', formatter: "date", formatoptions: { newformat: "d/m/Y" }, width: '100px' },
                { name: 'View Line Item', formatter: viewLineBtn }

            ],
            multiselect: true,
            emptyrecords: "No Quote Line to view",
            gridview: true,
            autoencode: true,
            loadtext: "Loading...",
            loadonce: true,
            rowNum: 3,
            rowList: [10, 20, 30],
            pager: '#LinePager',
            height: '100%',
            caption: "Quote List",
            autowidth: true,
            sortname: 'QuoteNumber',
            ajaxGridOptions: { type: 'POST', contentType: "application/json; charset=utf-8" },
            jsonReader: {
                repeatitems: false,
                root: "rows",
                page: "page",
                total: "totalPages",
                records: "totalRecords",
                id: "QuoteLineId"
            },
            serializeGridData: function(postData) {
                return JSON.stringify(postData);
            },
            onCellSelect: function(rowid,e) {

                    alert("rowid=" + rowid );

            },

            ondblClickRow: function(rowid) {

                var $model = $('#LineItemMyModal');

                $.ajax({
                    type: "GET",
                    url: $('#urlItemDetails').val(),
                    data: { LineId: rowid },
                    success: function(r) {
                        $model.html(r);
                        $model.modal('show');
                    }
                });


            }
        }).navGrid('#QuoteLinePager', { edit: false, add: false, del: false, search: true });


        function viewLineBtn(cellvalue, options, rowObject) {
            var rowid= options.rowid;
            var button = "<button class=\"viewLineItem\" id="+ rowid+">View Line   Item</button>"
         $('#' + rowid).die();
         $('#' + rowid).live('click', function (rowId) {
            alert("hi");
            alert(rowId);
         });
       };

答案 1 :(得分:0)

它对我有用:

 $('#QuoteLineTable').jqGrid({
    ....
    colModel: [
        ...
        { name: 'View Line Item', formatter: viewLineBtn }
    ]
    loadComplete: function (data) {
        console.log(data); // You can view the object
        let rowDataArray = data.rows;
        for (let i = 0, j = rowDataArray.length; i < j; i++)
        {
            let temp = rowDataArray[i];
            $("#btn_" + temp.id).click(() => {
                 m_alert(temp.content);
            });
        }
    }
    });

    function viewLineBtn(cellvalue, options, rowObject) 
    {
        return "<button id='btn_" + rowObject.id + "'>View Line Item</button>"
    }