0

我通过格式化程序在单元格中设置了列图标。我想在单元格中按条件设置图标。例如,如果列“状态”的值为“ok”,则列“添加”的图标为“ui-icon-icon1”,则列“添加”的图标为“ui-icon-icon2”否则'ui-icon-icon1'

 grid.jqGrid({
            data: mydata,
            datatype: 'local',
            colNames: ['Inv No', 'Date', 'Client', 'Amount', 'Tax', 'Total', 'Notes', '', '', '', ''],
            colModel: [
                { name: 'id', index: 'id', key: true, width: 70, sorttype: "int" },
                { name: 'invdate', index: 'invdate', width: 90, sorttype: "date" },
                { name: 'name', index: 'name', width: 100 },
                { name: 'amount', index: 'amount', width: 80, align: "right", sorttype: "float" },
                { name: 'tax', index: 'tax', width: 80, align: "right", sorttype: "float" },
                { name: 'total', index: 'total', width: 80, align: "right", sorttype: "float" },
                { name: 'note', index: 'note', width: 150, sortable: false },
                {
                    name: 'add', width: 20, sortable: false, search: false,
                    formatter: function () {

                        return "<span class='ui-icon ui-icon-plus'></span>"
                    }
                },
                {
                    name: 'edit', width: 20, sortable: false, search: false,
                    formatter: function () {
                        return "<span class='ui-icon ui-icon-pencil'></span>"
                    }
                },
                {
                    name: 'del', width: 20, sortable: false, search: false,
                    formatter: function () {
                        return "<span class='ui-icon ui-icon-trash'></span>"
                    }
                },
                {
                    name: 'details', width: 20, sortable: false, search: false,
                    formatter: function () {
                        return "<span class='ui-icon ui-icon-document'></span>"
                    }
                }
            ],
            pager: '#pager',
            rowNum: 10,
            rowList: [5, 10, 20, 50],
            sortname: 'id',
            sortorder: 'desc',
            viewrecords: true,
            gridview: true,
            height: '100%',
            rownumbers: true,

在此处输入图像描述

4

2 回答 2

0

formatter 方法接受三个参数:formatter(cellvalue, options, rowObject). 您应该能够从rowObject参数中检索状态列的值,例如rowObject[statusColumnIndex]

注意:问题中的网格定义不包含上述文本中提到的“状态”列的定义,因此我不确定“状态”列索引是什么。

于 2013-05-01T13:04:19.477 回答
0

这是我做类似事情的方法,请注意,您必须将 rowObect 列作为索引值引用,您不能使用上面 Rob Willis 提到的名称值,因为 rowObject 将不包含该信息(除非您将其作为额外信息传递JSON 或本地数据中的信息)。

 function FormatCell(cellval, opts, rowObject, action) {
    var displayIcon = rowObject[IndexOfColumToCheck] == "CheckValue" ? true : false;

    if (displayIcon)
        return ....
        //...format html to display the correct icon appended with the cell value
    }//if (priceChange) {
    else { 
        return ... 
           //...format html to display the correct icon appended with the cell value
    }//else

作为 return 中的一个示例,您可以返回如下内容:

return '<img src="/images/icon_clock.jpg" class="CustomSearchResultIcons InGridIcon_Inactive" height="16px" width="16px" alt="Inactive User Icon" title="This user has not logged in recently" />' +
                '<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only InGridIcon InGridIcon_Friend" title="You are freinds with this user">' +
                '<span class="ui-button-icon-primary ui-icon ui-icon-star"></span>' +
                '</button>' + '<span class="InGridUsername">' + cellval + '</span>';
于 2013-05-01T13:26:52.960 回答