This commit is contained in:
2024-07-10 12:19:52 +08:00
parent 5baf00de64
commit adbf4750cc
16 changed files with 1176 additions and 722 deletions

@@ -87,6 +87,17 @@ function filterSource(searchkeys, textkey, key, source) {
return source;
}
function getValue(it, valuekey, textkey) {
if (it == null) {
return null;
}
const value = it[valuekey];
if (value == null || value === '') {
return it[textkey];
}
return value;
}
/**
* 下拉列表参数对象
* @typedef DropdownOptions
@@ -171,6 +182,7 @@ export class Dropdown {
const source = this.source;
const count = source.length;
const valuekey = this._var.options.valueKey;
const textkey = this._var.options.textKey;
let index = source?.indexOf(this._var.selected);
if (isNaN(index) || index < -1) {
index = -1;
@@ -192,7 +204,7 @@ export class Dropdown {
index = count - 1;
}
}
const target = source[index]?.[valuekey];
const target = getValue(source[index], valuekey, textkey);
if (target != null) {
this.select(target);
}
@@ -315,7 +327,7 @@ export class Dropdown {
const textkey = this._var.options.textKey;
const template = this._var.options.htmlTemplate;
const htmlkey = this._var.options.htmlKey;
let item = this.source.find(it => (ignoreCase ? String(it[valuekey]).toLowerCase() : String(it[valuekey])) === selected);
let item = this.source.find(it => (ignoreCase ? String(getValue(it, valuekey, textkey)).toLowerCase() : String(getValue(it, valuekey, textkey))) === selected);
if (this._var.options.input) {
if (item == null) {
item = { [valuekey]: selected };
@@ -377,7 +389,7 @@ export class Dropdown {
const htmlkey = this._var.options.htmlKey;
const itemlist = selectedlist.map(a => {
const v = typeof a === 'string' ? a : String(a);
let item = source.find(it => String(it[valuekey]) === v);
let item = source.find(it => String(getValue(it, valuekey, textkey)) === v);
if (item == null) {
item = {
[valuekey]: v,
@@ -557,15 +569,16 @@ export class Dropdown {
}
const multiselect = this.multiSelect;
const valuekey = this._var.options.valueKey;
const textkey = this._var.options.textKey;
const allchecked = this._var.allChecked;
const selectedlist = this.selectedList;
source.forEach((item, i) => {
let val = item[valuekey];
let val = getValue(item, valuekey, textkey);
if (typeof val !== 'string') {
val = String(val);
}
if (multiselect) {
const selected = selectedlist.some(s => String(s[valuekey]) === val);
const selected = selectedlist.some(s => String(getValue(s, valuekey, textkey)) === val);
item.__checked = allchecked || selected;
}
});
@@ -588,7 +601,7 @@ export class Dropdown {
const selected = this.selected;
let scrolled;
array.forEach((item, i) => {
let val = item[valuekey];
let val = getValue(item, valuekey, textkey);
if (typeof val !== 'string') {
val = String(val);
}
@@ -671,9 +684,9 @@ export class Dropdown {
if (all != null) {
all.checked = false;
}
list = this.source.filter(it => String(it[valuekey]) !== val);
list = this.source.filter(it => String(getValue(it, valuekey, textkey)) !== val);
} else {
list = this.selectedList.filter(it => String(it[valuekey]) !== val);
list = this.selectedList.filter(it => String(getValue(it, valuekey, textkey)) !== val);
}
}
}