fastadmin 主键不是ID时,toggle无法执行的解决方法

发布时间 2024-01-04 14:29:58作者: zhang_you_wu

Table.api.formatter.toggle默认使用的是id作为主键,假如这个表格的主键不是id,绑定的数据会变为undefined,从而导致执行失败。

打开assets/js/require-table.js文件
找到下列代码,并修改(注释2行,新增1行):

toggle: function (value, row, index) {
                    //var table = this.table;//注释掉
                    //var options = table ? table.bootstrapTable('getOptions') : {};//注释掉
                    var options = $(table).bootstrapTable("getOptions"); //新增这行
var pk = options.pk || "id"; var color = typeof this.color !== 'undefined' ? this.color : 'success'; var yes = typeof this.yes !== 'undefined' ? this.yes : 1; var no = typeof this.no !== 'undefined' ? this.no : 0; var url = typeof this.url !== 'undefined' ? this.url : ''; var confirm = ''; var disable = false; if (typeof this.confirm !== "undefined") { confirm = typeof this.confirm === "function" ? this.confirm.call(this, value, row, index) : this.confirm; } if (typeof this.disable !== "undefined") { disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable; }

            return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change " + (disable ? 'btn disabled no-padding' : '') + "' data-index='" + index + "' data-id='" + row[pk] + "' " + (url ? "data-url='" + url + "'" : "") + (confirm ? "data-confirm='" + confirm + "'" : "") + " data-params='" + this.field + "=" + (value == yes ? no : yes) + "'><i class='fa fa-toggle-on text-success text-" + color + " " + (value == yes ? '' : 'fa-flip-horizontal text-gray') + " fa-2x'></i></a>"; },