12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- // 组件不支持简单的双向绑定,需要使用input和confirm事件来实现。
- Component({
- options: {
- multipleSlots: true,
- styleIsolation: "shared",
- },
- properties: {
- value: {
- type: Array,
- value: [],
- observer: "_value",
- },
- options: {
- required: true,
- type: Array,
- value: [],
- observer: "_options",
- },
- /** 排列方向,可选值为 vertical horizontal */
- direction: {
- type: String,
- value: "horizontal",
- },
- },
- data: {
- innerOptions: [] as any[],
- innerValue: [] as any[],
- },
- methods: {
- onCheckChange(event: any) {
- const value = event.detail;
- this.setData({
- value: value.map((v: any) => String(v)), // 只更新 innerValue
- });
- this.triggerEvent("change", value); // 将值传递给父组件
- },
- onChecked(val: any) {
- return this.data.innerValue.includes(val) ? true : false;
- },
- _value(newVal: any[]) {
- const tempVal: any[] = [];
- this.data.innerOptions.forEach((item: any) => {
- tempVal.push({
- ...item,
- isSelect: newVal.includes(item.value) ? true : false,
- });
- });
- this.setData({
- innerOptions: tempVal,
- innerValue: newVal.map((v: any) => String(v)), // 确保数据类型一致
- });
- },
- _options(newVal: any[]) {
- const value: any = this.data.value;
- const tempVal: any[] = [];
- newVal.forEach((item: any) => {
- tempVal.push({
- ...item,
- isSelect: value.includes(item.value) ? true : false,
- });
- });
- this.setData({
- innerOptions: tempVal,
- });
- },
- },
- });
|