【问题标题】:toolbar in jqGrid?jqGrid中的工具栏?
【发布时间】:2010-10-10 23:16:14
【问题描述】:

我想在 jqGrid 中创建一个工具栏,其中只有通常在寻呼机上的按钮。

目前我有以下定义

$("#foroGrid").jqGrid('navGrid', '#pager',
    { add: true, addtitle: 'Add Foro',
        edit: true, edittitle: 'Edit Foro',
        del: true, deltitle: 'Delete Foro',
        refresh: true, refreshtitle: 'Refresh data',
        search: true, searchtitle: 'Show Filters', 
        addfunc: addForo, editfunc: editForo, delfunc: deleteForo },
    {}, // default settings for edit
    {}, // default settings for add
    {}, // default settings for delete
    { closeOnEscape: true, multipleSearch: true, closeAfterSearch: true }, // search options
    {}
);

而且我还需要添加其他功能,例如“CSV 导出”、“PDF 导出”、“打印”等...

事实证明空间将被填满,我想移动顶部工具栏中的按钮,同时在底部保留带有导航器和记录计数信息的寻呼机。

这个配置可以用jqGrid实现吗?

【问题讨论】:

    标签: jquery jqgrid jqgrid-asp.net


    【解决方案1】:

    我扩展了脚本adding-buttons-in-jqgrid-toolbar 并添加了以相同方式添加标签和锚点的功能。 现在你可以使用:

    $("#tableID").toolbarButtonAdd("#t_tableID",{caption:"",position:"first",title:"Refresh", align:"right", buttonicon :'ui-icon-refresh', onClickButton:function(){ $("#tableID").trigger("reloadGrid"); } })
    $("#tableID").toolbarLabelAdd("#t_tableID", { caption: "0 Selected", position: "first", title: "", align: "right", id: 'lblSelectedRows' });
    $("#tableID").toolbarAncherAdd("#t_tableID", { caption: "Select All", title: "Select All", id: 'btnSelectAll', onClickButton: function() { selectAllRecords(true); } });
    

    这是库代码:

    $.fn.extend({
    /*
    *  
    * The toolbar has the following properties
    *   id of top toolbar: t_<tablename>
    *   id of bottom toolbar: tb_<tablename>
    *   class of toolbar: ui-userdata
    * elem is the toolbar name to which button needs to be added. This can be 
    *       #t_tablename - if button needs to be added to the top toolbar
    *       #tb_tablename - if button needs to be added to the bottom toolbar
    */
    toolbarButtonAdd: function(elem, p) {
        p = $.extend({
            caption: "newButton",
            title: '',
            buttonicon: 'ui-icon-newwin',
            onClickButton: null,
            position: "last"
        }, p || {});
        var $elem = $(elem);
        var tableString = "<table style='float:left;table-layout:auto;' cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class='ui-toolbar-table'>";
        tableString += "<tbody> <tr></tr></table>";
        //console.log("In toolbar button add method");
        /* 
        * Step 1: check whether a table is already added. If not add
        * Step 2: If there is no table already added then add a table
        * Step 3: Make the element ready for addition to the table 
        * Step 4: Check the position and corresponding add the element
        * Step 5: Add other properties 
        */
        //step 1 
        return this.each(function() {
            if (!this.grid) { return; }
            if (elem.indexOf("#") != 0) {
                elem = "#" + elem;
            }
            //step 2
            if ($(elem).children('table').length === 0) {
                $(elem).append(tableString);
            }
            //step 3
            var tbd = $("<td style=\"padding-left:1px;padding-right:1px\"></td>");
            $(tbd).addClass('ui-toolbar-button ui-corner-all').append("<div class='ui-toolbar-div'><span class='ui-icon " + p.buttonicon + "'></span>" + "<span>" + p.caption + "</span>" + "</div>").attr("title", p.title || "")
            .click(function(e) {
                if ($.isFunction(p.onClickButton)) { p.onClickButton(); }
                return false;
            })
            .hover(
                function() { $(this).addClass("ui-state-hover"); },
                function() { $(this).removeClass("ui-state-hover"); }
            );
            if (p.id) { $(tbd).attr("id", p.id); }
            if (p.align) { $(elem).attr("align", p.align); }
            var findnav = $(elem).children('table');
            if (p.position === 'first') {
                if ($(findnav).find('td').length === 0) {
                    $("tr", findnav).append(tbd);
                } else {
                    $("tr td:eq(0)", findnav).before(tbd);
                }
            } else {
                //console.log("not first");
                $("tr", findnav).append(tbd);
            }
        });
    },
    toolbarLabelAdd: function(elem, p) {
        p = $.extend({
            caption: "newLabel",
            title: '',
            id: '',
            position: "last"
        }, p || {});
        var $elem = $(elem);
        var tableString = "<table style='float:left;table-layout:auto;' cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class='ui-toolbar-table'>";
        tableString += "<tbody> <tr></tr></table>";
        /* 
        * Step 1: check whether a table is already added. If not add
        * Step 2: If there is no table already added then add a table
        * Step 3: Make the element ready for addition to the table 
        * Step 4: Check the position and corresponding add the element
        * Step 5: Add other properties 
        */
        //step 1 
        return this.each(function() {
            if (!this.grid) { return; }
            if (elem.indexOf("#") != 0) {
                elem = "#" + elem;
            }
            //step 2
            if ($(elem).children('table').length === 0) {
                $(elem).append(tableString);
            }
            //step 3
            var tbd = $("<td style=\"padding-left:1px;padding-right:1px\"></td>");
            $(tbd).addClass('ui-corner-all').append("<div class='ui-toolbar-lbl'><span>" + p.caption + "</span>" + "</div>").attr("title", p.title || "");
            if (p.id) { $(tbd).attr("id", p.id); }
            if (p.align) { $(elem).attr("align", p.align); }
            var findnav = $(elem).children('table');
            if (p.position === 'first') {
                if ($(findnav).find('td').length === 0) {
                    $("tr", findnav).append(tbd);
                } else {
                    $("tr td:eq(0)", findnav).before(tbd);
                }
            } else {
                $("tr", findnav).append(tbd);
            }
        });
    },
    toolbarAncherAdd: function(elem, p) {
        p = $.extend({
            caption: "newButton",
            title: '',
            id: '',
            buttonicon: '',
            buttonclass : '',
            onClickButton: null,
            position: "last"
        }, p || {});
        var $elem = $(elem);
        var tableString = "<table style='float:left;table-layout:auto;' cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class='ui-toolbar-table'>";
        tableString += "<tbody> <tr></tr></table>";
        /* 
        * Step 1: check whether a table is already added. If not add
        * Step 2: If there is no table already added then add a table
        * Step 3: Make the element ready for addition to the table 
        * Step 4: Check the position and corresponding add the element
        * Step 5: Add other properties 
        */
        //step 1 
        return this.each(function() {
            if (!this.grid) { return; }
            if (elem.indexOf("#") != 0) {
                elem = "#" + elem;
            }
            //step 2
            if ($(elem).children('table').length === 0) {
                $(elem).append(tableString);
            }
            //step 3
            var tbd = $("<td style=\"padding-left:1px;padding-right:1px\"></td>"),
                iconClass = p.buttonicon.length === 0 ? "" : "<span class='ui-icon " + p.buttonicon + "'></span>"; 
            $(tbd).addClass('ui-toolbar-button ui-corner-all').append("<a class='ui-toolbar-a " + p.buttonClass + "'>" + iconClass + "<span>" + p.caption + "</span>" + "</a>").attr("title", p.title || "")
            .click(function(e) {
                if ($.isFunction(p.onClickButton)) { p.onClickButton(); }
                return false;
            });
            if (p.id) { $(tbd).attr("id", p.id); }
            if (p.align) { $(elem).attr("align", p.align); }
            var findnav = $(elem).children('table');
            if (p.position === 'first') {
                if ($(findnav).find('td').length === 0) {
                    $("tr", findnav).append(tbd);
                } else {
                    $("tr td:eq(0)", findnav).before(tbd);
                }
            } else {
                //console.log("not first");
                $("tr", findnav).append(tbd);
            }
        });
    },
    });
    

    【讨论】:

    • 我们如何添加分隔符?使用 toolbarButtonAdd 方法?
    【解决方案2】:

    如果您想将按钮添加到顶部工具栏,您必须执行类似this:

    就像我在上一篇文章中所说的那样 向 jQGrid 工具栏添加按钮是 有点棘手。您需要添加 下面的代码在末尾 jQGrid 设置

    完成后,您可以使用:

    $("#tableID").toolbarButtonAdd("#t_tableID",{caption:"",position:"first",title:"Refresh", align:"right", buttonicon :'ui-icon-refresh', onClickButton:function(){ $("#tableID").trigger("reloadGrid"); } });
    

    【讨论】:

      【解决方案3】:

      看看this我的旧答案。在我看来,它正是你想要的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-21
        • 1970-01-01
        相关资源
        最近更新 更多