28 lines
522 B
JavaScript
28 lines
522 B
JavaScript
import { Ut } from "./Ut";
|
|
|
|
export class AppendOptions {
|
|
|
|
constructor(target, data, opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
if (opts.clear) {
|
|
this.replaceChildren();
|
|
}
|
|
|
|
if (Ut.isStr(data)) {
|
|
|
|
let options = '';
|
|
|
|
data.replace(/"((?:\\.|[^"\\])*)"\s*:\s*"((?:\\.|[^"\\])*)"/g,
|
|
(_, k, v) => (options += `<option value="${JSON.parse(`"${k}"`)}">${JSON.parse(`"${v}"`)}</option>`, '')
|
|
);
|
|
|
|
target.innerHTML = options;
|
|
|
|
return;
|
|
}
|
|
|
|
Ut.each(data, (val, id) => target.append(new Option(val, id)) );
|
|
}
|
|
} |