Ver código fonte

删除功能

duy 1 dia atrás
pai
commit
82aaa576ba

+ 7 - 3
src/views/formula/component/formulaItem.vue

@@ -20,8 +20,8 @@
       {{ item.name }}
     </span>
     
-    <!-- 光标指示器 - 只在选中时显示 -->
-    <span v-if="item.selected" class="cursor-blink"></span>
+    <!-- 光标指示器 - 选中时或强制显示时显示 -->
+    <span v-if="item.selected || showCursor" class="cursor-blink"></span>
   </div>
 </template>
 
@@ -33,6 +33,11 @@ export default {
       type: Object,
       default: () => ({})
     },
+    // 新增属性:用于强制显示光标
+    showCursor: {
+      type: Boolean,
+      default: false
+    }
   },
   methods: {
     itemClick(item) {
@@ -120,4 +125,3 @@ export default {
 }
 
 </style>
-    

+ 76 - 18
src/views/formula/edit.vue

@@ -120,7 +120,7 @@
                   @keydown.left="handleLeftArrow"
                   @keydown.right="handleRightArrow"
                   @keydown.delete="handleDelete"
-                  @keydown.backspace="handleDelete"
+                 
                   @focus="containerFocused = true"
                   @blur="containerFocused = false"
                 >
@@ -652,6 +652,14 @@ export default {
       }
     }
   },
+  watch: {
+  selectEleFormula: {
+    handler() {
+      this.checkDefaultSelection();
+    },
+    deep: true
+  }
+},
   created() {
     // this.wbsid = this.$route.query.wbsid;
     // this.eleid = this.$route.query.eleid;
@@ -775,6 +783,11 @@ export default {
     operationEdit(){
       this.selectEleFormula= JSON.parse(JSON.stringify(this.processFormula));
       this.operationVisible = true;
+       this.checkDefaultSelection();
+        // 确保容器获得焦点
+        this.$nextTick(() => {
+          this.$el.querySelector('.sele-ele-box1').focus();
+        });
     },
 
     eleAddFormula(){
@@ -1121,7 +1134,23 @@ export default {
     //   }else{
     //     this.curSeleEleIndex = -1;
     //   }
-    // },  // 点击元素时的处理
+    // },  
+    // 检查是否有选中的元素,如果没有则选中最后一个
+    checkDefaultSelection() {
+      const hasSelected = this.selectEleFormula.some(item => item.selected);
+      if (!hasSelected && this.selectEleFormula.length > 0) {
+        // 取消所有选中
+        this.selectEleFormula.forEach(item => {
+          item.selected = false;
+        });
+        
+        // 选中最后一个元素
+        const lastIndex = this.selectEleFormula.length - 1;
+        this.selectEleFormula[lastIndex].selected = true;
+        this.curSeleEleIndex = lastIndex;
+      }
+    },
+    // 点击元素时的处理
     eleFormulaClick(obj, index) {
       // 先取消所有选中状态
       this.selectEleFormula.forEach(item => {
@@ -1169,22 +1198,51 @@ export default {
     },
     
     // 处理删除操作
-    handleDelete(e) {
-      e.preventDefault(); // 阻止默认行为
-      this.removeSelect();
-      
-      // 删除后自动选中下一项或上一项
-      if (this.selectEleFormula.length > 0) {
-        // 如果删除的是最后一项,选中前一项
-        if (this.curSeleEleIndex >= this.selectEleFormula.length) {
-          this.curSeleEleIndex = this.selectEleFormula.length - 1;
-        }
-        // 如果还有元素,选中当前索引位置的元素
-        if (this.curSeleEleIndex > -1) {
-          this.selectEleFormula[this.curSeleEleIndex].selected = true;
-        }
-      }
-    },
+handleDelete(e) {
+  e.preventDefault();
+  
+  // 增加更严格的索引检查
+  if (
+    this.curSeleEleIndex === null || 
+    this.curSeleEleIndex === undefined ||
+    this.curSeleEleIndex < 0 || 
+    this.curSeleEleIndex >= this.selectEleFormula.length
+  ) {
+    
+    return;
+  }
+  
+  // 保存当前要删除的元素索引(避免后续操作影响)
+  const indexToDelete = this.curSeleEleIndex;
+  const itemToDelete = this.selectEleFormula[indexToDelete];
+  
+ 
+  
+  // 处理元素取消勾选
+  if (itemToDelete && itemToDelete.type === 'Element') {
+    this.deleEleIndex = indexToDelete;
+    this.unCheckEleFormulac(itemToDelete.id);
+  }
+  
+  // 执行删除(只删除指定索引的1个元素)
+  const deletedItems = this.selectEleFormula.splice(indexToDelete, 1);
+  
+  
+  // 正确更新当前选中索引
+  if (this.selectEleFormula.length > 0) {
+    // 如果删除的是最后一个元素,选中前一个
+    this.curSeleEleIndex = Math.min(
+      indexToDelete, 
+      this.selectEleFormula.length - 1
+    );
+    // 确保选中状态正确
+    this.selectEleFormula.forEach((item, idx) => {
+      item.selected = idx === this.curSeleEleIndex;
+    });
+  } else {
+    this.curSeleEleIndex = -1;
+  }
+},
     //取消勾选
     unCheckEleFormulac(eleId){
       for (let i = 0; i < this.eleList.length; i++) {