问题
BootstrapTable 在引入了 bootstrap-table-zh-CN.js 并且配置了 locale 参数后,导致 formatNoMatches 不生效。
查看源码发现 BootstrapTable.prototype.init 方法中,会先执行 BootstrapTable.prototype.initLocale 方法。如果配置了 locale 参数,会把对应的语言包中的参数合并到 options 中,会覆盖初始化时配置的 formatNoMatches 。
image.png
解决方案
-
如果是整个项目都统一配置,可以直接修改 bootstrap-table-zh-CN.js 中对应的方法。
image.png 如果要单独配置,可以在调用 $('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 '表单暂无数据';//注意:这里配置的无效
},
});