BootstrapTable formatNoMatches 不生效

问题

BootstrapTable 在引入了 bootstrap-table-zh-CN.js 并且配置了 locale 参数后,导致 formatNoMatches 不生效。
查看源码发现 BootstrapTable.prototype.init 方法中,会先执行 BootstrapTable.prototype.initLocale 方法。如果配置了 locale 参数,会把对应的语言包中的参数合并到 options 中,会覆盖初始化时配置的 formatNoMatches 。

image.png

解决方案

  1. 如果是整个项目都统一配置,可以直接修改 bootstrap-table-zh-CN.js 中对应的方法。

    image.png

  2. 如果要单独配置,可以在调用 $('table').bootstrapTable({}) 之前覆盖对应的语言包配置。相当于自己写一个 bootstrap-table-zh-CN.js

var myLocales = {
    
    formatLoadingMessage: function () {
        return '自定义加载中。。。';
    },
    formatNoMatches: function () {
        return '自定义没有数据';
    },
    
};

var locale = 'zh-CN';//假如使用中文包

$.extend($.fn.bootstrapTable.locales[locale], myLocales);//合并到语言包
// $.extend($.fn.bootstrapTable.defaults, myLocales);

$('table').bootstrapTable({
    locale: lang,
    /* 
     * 其他配置
     */
    formatNoMatches: function () {
        return '表单暂无数据';//注意:这里配置的无效
    },
    
});
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。