【问题标题】:Show icons on jqgrid row mouse hover在 jqgrid 行鼠标悬停时显示图标
【发布时间】:2017-11-21 09:58:59
【问题描述】:

我如何在 jqGrid 的悬停行上显示图标(添加、编辑图标等),并通过单击我想调用 JS 函数等的图标。我想在每一行上显示图标。

下面是我的 jqGrid

$grid.jqGrid({
                url: '@Url.Action("GetOrganizationList", "Organization")',
                datatype: "json",
                height: 'auto',
                colNames: ['@objLocalizer["organization_organizationID"]','@objLocalizer["OrganizationsName"]', '@objLocalizer["ParentOrganization"]', '@objLocalizer["Email"]', '@objLocalizer["ContactPhone"]', '@objLocalizer["CurrencyID"]','@objLocalizer["CreatedByorg"]','@objLocalizer["CreatedOnorg"]']
                colModel: [{
                    name: 'OrganizationID',
                    index: 'OrganizationID',
                    sortable: true,
                    sorttype: 'number',
                    formatter: addLink,
                    align: 'center'
                },{
                    name: 'OrganizationName',
                    index: 'OrganizationName',
                    sortable: true,
                    sorttype: 'text',

                }, {
                    name: 'PratentOrganization',
                    index: 'PratentOrganization',
                    sortable: true,
                    sorttype: 'text',
                    align: 'left'
                },
                {
                    name: 'ContactEmail',
                    index: 'ContactEmail',
                    sortable: true,
                    sorttype: 'text',
                    hidden: true,
                },
                {
                    name: 'ContactPhone',
                    index: 'ContactPhone',
                    sortable: true,
                    sorttype: 'text',
                    hidden: true,

                },
                  {
                      name: 'CurrencyID',
                      index: 'CurrencyID',
                      sortable: true,
                      sorttype: 'text',
                      hidden: true,
                  },
                    {
                        name: 'UserName',
                        index: 'UserName',
                        sortable: true,
                        sorttype: 'text',
                        align: 'left'

                    },
                {
                    name: 'WhenCreated',
                    index: 'WhenCreated',
                    sortable: true,
                    sorttype: 'text',
                    formatter: 'date',
                    align: 'center'
                }
                ],

                cmTemplate: { autoResizable: true, editable: true },
                autoResizing: { compact: true, resetWidthOrg: true },
                iconSet: "fontAwesome",
                rowNum: 10,
                rowList: [5, 10, 20, "10000:All"],
                viewrecords: true,
                sortable: true,
                toppager: false,
                pager: true,
                rownumbers: false,
                sortname: "invdate",
                sortorder: "desc",
                pager: '#pjqgridOrg',
                pagerRightWidth: 150,
                loadonce: true,
                multiselect: false,
                hidegrid: false,
                gridComplete: function () {

                },

            }).jqGrid("navGrid")
            .jqGrid("inlineNav")
            .jqGrid("filterToolbar");

第一列“OrganizationID”的格式化程序

 function addLink(cellvalue, options, rowObject) {

        var validrow = rowObject.Organization;

        if (validrow != undefined) {
            return "<a href='#' style='height:25px;width:120px;' type='button'  onclick=CallActionMethod(" + "'" + OrganizationID + "'" + ")>" + OrganizationID + "</a>";
        } else {
            return "<a href='#' style='height:25px;width:120px;' type='button'  onclick=CallActionMethod(" + "'" + rowObject['OrganizationID'] + "'" + ")>" + rowObject['OrganizationID'] + "</a>";
        }
    }

下面是截图,它的样子

我想在第一列即“OrganizationID”列中显示图标。

【问题讨论】:

    标签: jquery jqgrid free-jqgrid


    【解决方案1】:

    尝试在.hover() 事件行上添加您的自定义按钮,例如,

    $("tr").hover(function() {
        $(this).find('td:first').append($("<span class='custom-btns'><button>Add</button><button>Delete</button></span>"));
      }, function() {
        $(this).find(".custom-btns").remove();
    });
    

    片段

    jQuery("#list").jqGrid({
      datatype: "local",
      height: 250,
      colNames: ['Inv No', 'Date', 'Client', 'Amount', 'Tax', 'Total', 'Notes'],
      colModel: [{
          name: 'id',
          index: 'id',
          width: 200,
          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
        }
      ],
    });
    var mydata = [{
        id: "1",
        invdate: "2007-10-01",
        name: "test",
        note: "note",
        amount: "200.00",
        tax: "10.00",
        total: "210.00"
      },
      {
        id: "2",
        invdate: "2007-10-02",
        name: "test2",
        note: "note2",
        amount: "300.00",
        tax: "20.00",
        total: "320.00"
      },
      {
        id: "3",
        invdate: "2007-09-01",
        name: "test3",
        note: "note3",
        amount: "400.00",
        tax: "30.00",
        total: "430.00"
      },
      {
        id: "4",
        invdate: "2007-10-04",
        name: "test",
        note: "note",
        amount: "200.00",
        tax: "10.00",
        total: "210.00"
      },
      {
        id: "5",
        invdate: "2007-10-05",
        name: "test2",
        note: "note2",
        amount: "300.00",
        tax: "20.00",
        total: "320.00"
      },
      {
        id: "6",
        invdate: "2007-09-06",
        name: "test3",
        note: "note3",
        amount: "400.00",
        tax: "30.00",
        total: "430.00"
      },
      {
        id: "7",
        invdate: "2007-10-04",
        name: "test",
        note: "note",
        amount: "200.00",
        tax: "10.00",
        total: "210.00"
      },
      {
        id: "8",
        invdate: "2007-10-03",
        name: "test2",
        note: "note2",
        amount: "300.00",
        tax: "20.00",
        total: "320.00"
      },
      {
        id: "9",
        invdate: "2007-09-01",
        name: "test3",
        note: "note3",
        amount: "400.00",
        tax: "30.00",
        total: "430.00"
      }
    ];
    for (var i = 0; i <= mydata.length; i++)
      jQuery("#list").jqGrid('addRowData', i + 1, mydata[i]);
    
    $("#list tr").hover(function() {
      $(this).find('td:first').append($("<span class='custom-btns'><button>Add</button><button>Delete</button></span>"));
    }, function() {
      $(this).find(".custom-btns").remove();
    }).on('click', '.custom-btns button', function() {
      alert($(this).text());
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqgrid/4.6.0/js/jquery.jqGrid.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/jqgrid/4.6.0/css/ui.jqgrid.css" rel="stylesheet" />
    <table id="list"></table>

    如果您的数据是动态加载的,则使用gridComplete event

    【讨论】:

    • 不工作..你能粘贴完整的代码/示例
    • 我已经添加了这样的内容,它只出现在第一行并且在鼠标移出时被删除。 $grid.hover(function () { $(this).find('td:first').append($("&lt;span class='custom-btns'&gt;&lt;button&gt;Add&lt;/button&gt;&lt;button&gt;Delete&lt;/button&gt;&lt;/span&gt;")); }, function () { $(this).find(".custom-btns").remove(); });
    • 这个答案没有解决你的问题吗?还是您还有其他问题?
    • 添加setTimeout(function(){/*hover code*/},500) 以防止出现此问题。
    猜你喜欢
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    相关资源
    最近更新 更多