【问题标题】:Set the status text on a Dojo EnhancedGrid FilterBar在 Dojo EnhancedGrid FilterBar 上设置状态文本
【发布时间】:2013-11-26 23:39:19
【问题描述】:

EnhancedGrid 上的 FilterBar 有一个标签,显示“未应用过滤器”、“显示 100 个项目中的 0 个”等。

有没有一种方法可以以编程方式将此文本更改为我想要的任何内容?

谢谢,
特里斯坦

编辑: Ken 的方法对我很有效,但对于那些需要它的人来说,这里可能还有另一种方法:Replacing Dojo/Dijit NLS strings

【问题讨论】:

    标签: javascript dojo dojox.grid


    【解决方案1】:

    您要自定义的字符串是dojox/grid/enhanced/nls/Filter 本地化包的一部分。对此捆绑包的引用存储在 Filter 插件本身的构造函数中(例如 1.9.1 中的 here)。

    自定义这些消息的最可重用和最不稳定的方法可能是创建过滤器插件的扩展并相应地更改nls 对象。例如:(以下假设您需要 dojo/_base/declaredojo/_base/langdojox/grid/EnhancedGriddojox/grid/enhanced/plugins/Filter 到适当的变量)

    var CustomFilter = declare(Filter, {
        name: 'customFilter',
        constructor: function () {
            // Avoid modifying the original nls bundle
            this.nls = lang.clone(this.nls);
            // Alter the "no filter applied" message
            this.nls.filterBarMsgNoFilterTemplate = 'Custom message here';
            // Alter other messages here
        }
    });
    
    EnhancedGrid.registerPlugin(CustomFilter);
    

    然后,当您创建 EnhancedGrid 实例时,您将指定 customFilter,而不是在您的 plugins 哈希中指定 filter 键。它的属性将保持不变,因为您仍在扩展原始插件。

    如果您需要找出nls 对象中您希望自定义的消息的键,请查看the source for the bundle that defines them

    当然,请注意,通常会根据浏览器的区域设置加载不同的本地化包。上面的示例假设您只关心通过自定义支持一种语言;如果您想支持多个,您可能希望设置自己的一组 nls 资源以混合在第一个资源之上。如果您需要帮助,我可以扩展此答案,但这至少应该可以帮助您入门。

    【讨论】:

    • 您好,谢谢,这是一个非常好的解决方案。然而有一个问题。此方法不会翻译所有过滤器文本。我使用了 this.nls = dojox.grid.enhanced.nls.ja.Filter 可能应该替换所有字符串,但它只翻译了很少的文本和文本,如各种条件和匹配条件保持为英文,但是各种标题和标签被翻译了,你能想出为什么以及如何做到这一点
    【解决方案2】:

    要以编程方式更改文本“未应用过滤器”,您可以这样做。 我已经在我实际工作的网格中对其进行了测试。

    首先获取定义Text的dom节点:

    var node = JSON.stringify(dom.byId("dojox_grid_enhanced_plugins_filter_FilterBar_0"));
    

    然后搜索定义文本的span-tag的类并更改它:

    dojo.query( '.dojoxGridFBarStatus' ).forEach(function(node){ 
      node.innerHTML = "Set in the Text you would prefer";
     }
    

    您可以对“显示的 100 个项目中的 0 个”标签的文本执行相同的操作。 除了必须在定义过滤器之后进行更改。

    这是网格中过滤栏后面的代码。(由FF Web-DeveloperTool捕获)

    <tbody>
    <tr>
        <td class=\"dojoxGridFBarBtnTD\">
        <span aria-label=\"Tabelle filtern\" 
                title=\"Filter definieren\" 
                widgetid=\"dijit_form_Button_12\" 
                class=\"dijit dijitReset dijitInline dijitButton dojoxGridFBarBtn\" 
                role=\"presentation\">
                    <span class=\"dijitReset dijitInline dijitButtonNode\" 
                    data-dojo-attach-event=\"ondijitclick:_onClick\" 
                    role=\"presentation\">
                        <span style=\"-moz-user-select: none;\" 
                        id=\"dijit_form_Button_12\" 
                        tabindex=\"0\" 
                        class=\"dijitReset dijitStretch dijitButtonContents\" 
                        data-dojo-attach-point=\"titleNode,focusNode\" 
                        role=\"button\" 
                        aria-labelledby=\"dijit_form_Button_12_label\">
                            <span class=\"dijitReset dijitInline dijitIcon dojoxGridFBarDefFilterBtnIcon\" 
                            data-dojo-attach-point=\"iconNode\">
                            </span>
                                <span class=\"dijitReset dijitToggleButtonIconChar\">?</span>
                                <span class=\"dijitReset dijitInline dijitButtonText\" 
                                id=\"dijit_form_Button_12_label\" 
                                data-dojo-attach-point=\"containerNode\">...</span>
                        </span>
                    </span>
                <input value=\"\" 
                class=\"dijitOffScreen\" 
                tabindex=\"-1\"
                role=\"presentation\" 
                data-dojo-attach-point=\"valueNode\" 
                type=\"button\">
        </span>
        </td>
        <td class=\"dojoxGridFBarInfoTD\">
            <span class=\"dojoxGridFBarInner\">
                <span class=\"dojoxGridFBarStatus\" 
                dojoattachpoint=\"statusBarNode\">Kein Filter angewendet</span>
                    <span aria-label=\"Filter abwählen\" 
                    style=\"display: none;\" 
                    widgetid=\"dijit_form_Button_13\" 
                    class=\"dijit dijitReset dijitInline dijitButton dojoxGridFBarClearFilterBtn\" 
                    role=\"presentation\">
                        <span class=\"dijitReset dijitInline dijitButtonNode\" 
                        data-dojo-attach-event=\"ondijitclick:_onClick\" 
                        role=\"presentation\">
                            <span style=\"-moz-user-select: none;\" 
                                id=\"dijit_form_Button_13\" 
                                tabindex=\"0\" 
                                class=\"dijitReset dijitStretch dijitButtonContents\" 
                                data-dojo-attach-point=\"titleNode,focusNode\" 
                                role=\"button\" 
                                aria-labelledby=\"dijit_form_Button_13_label\">
                                    <span class=\"dijitReset dijitInline dijitIcon dojoxGridFBarClearFilterBtnIcon\" 
                                        data-dojo-attach-point=\"iconNode\">
                                    </span>
                                    <span class=\"dijitReset dijitToggleButtonIconChar\">?</span>
                                        <span class=\"dijitReset dijitInline dijitButtonText\" 
                                            id=\"dijit_form_Button_13_label\" 
                                            data-dojo-attach-point=\"containerNode\">Filter löschen</span>
                                        </span>
                            </span>
                            <input value=\"\" 
                            class=\"dijitOffScreen\" 
                            tabindex=\"-1\" 
                            role=\"presentation\" 
                            data-dojo-attach-point=\"valueNode\" 
                            type=\"button\">
                        </span>
                        <span widgetid=\"dijit_form_Button_14\" 
                        class=\"dijit dijitReset dijitInline dijitButton dojoxGridFBarCloseBtn\" 
                        role=\"presentation\">
                            <span class=\"dijitReset dijitInline dijitButtonNode\" 
                            data-dojo-attach-event=\"ondijitclick:_onClick\" 
                            role=\"presentation\">
                                <span style=\"-moz-user-select: none;\" 
                                title=\"Filterleiste schließen\" 
                                id=\"dijit_form_Button_14\" 
                                tabindex=\"0\" 
                                class=\"dijitReset dijitStretch dijitButtonContents\" 
                                data-dojo-attach-point=\"titleNode,focusNode\" 
                                role=\"button\" 
                                aria-labelledby=\"dijit_form_Button_14_label\">
                                    <span class=\"dijitReset dijitInline dijitIcon dojoxGridFBarCloseBtnIcon\" 
                                    data-dojo-attach-point=\"iconNode\">
                                    </span>
                                        <span class=\"dijitReset dijitToggleButtonIconChar\">?
                                        </span>
                                            <span class=\"dijitReset dijitInline dijitButtonText dijitDisplayNone\" 
                                            id=\"dijit_form_Button_14_label\" 
                                            data-dojo-attach-point=\"containerNode\">Filterleiste schließen
                                            </span>
                                </span>
                            </span>
                            <input value=\"\" 
                            class=\"dijitOffScreen\" 
                            tabindex=\"-1\" 
                            role=\"presentation\" 
                            data-dojo-attach-point=\"valueNode\" 
                            type=\"button\">
                        </span>
                    </span>
        </td>
    </tr>
    </tbody>
    

    希望对你有所帮助。

    问候,米里亚姆

    【讨论】:

    • 您好,Downvoter。你能解释一下你为什么投反对票吗?问候
    • 几个原因:(1)在DOM节点上调用JSON.stringify没有多大意义(容易导致循环结构错误),(2)按类查询节点并重置它的innerHTML 是一个相当难以维护的解决方案,并且一旦对网格采取进一步的操作(例如,再次填充和清除结果),就不能保证保持其效果。与小部件一起使用的可维护解决方案要多得多,而不是直接绕过其 DOM 元素。
    • 好的,谢谢肯!知道哪里出了问题或为什么不好对我来说很重要,因为我仍在学习道场并想知道如何使它变得更好。再次感谢您的回答。问候
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多