Z02的下拉选择器

本文介绍了一个使用Java连接数据库并获取选项的Selector类,详细解释了如何通过字符串参数和表名来选择数据库中的特定字段,并返回一个包含这些选项的列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.pz.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import org.apache.struts.util.LabelValueBean;

import com.sun.java_cup.internal.production;

public class Selector {

 private Connection con = null;
 
 public Selector(){
  try {
   con = Database.getConnection();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 public ArrayList getOptions(String str,String table){
  ArrayList list=new ArrayList();
  list.add(new LabelValueBean("----请选择----", ""));
  
  PreparedStatement ps=null;
  ResultSet rs=null;
  
  if (!str.equals("")&&!table.equals("")){
   String sqlstr=null;
   sqlstr="select distinct "+str+" from "+table;
   try {
    ps = con.prepareStatement(sqlstr);
    rs=ps.executeQuery();
    while (rs.next()){
     String s=rs.getString(1).trim();
     list.add(new LabelValueBean(s,s));
    }
   } catch (Exception e) {
    e.printStackTrace();
   } finally{
    try {
     rs.close();
     rs = null;
     ps.close();
     ps = null;
     con.close();
     con = null;
    } catch (Exception e) {
     e.printStackTrace();
    }    
   }
  }
  return list;
 }
 
}

### 实现 UniApp 中 Select 下拉单选选择器 在 UniApp 中实现一个功能完善的 `Select` 下拉单选选择器可以通过自定义组件来完成。这不仅能满足特定的业务需求,还能提供更好的用户体验。 #### 组件结构设计 为了构建一个高效的下拉单选选择器,可以采用 Vue 的组合式 API 和 TypeScript 来增强代码可读性和维护性[^2]: ```html <template> <view class="select-container"> <!-- 显示当前选项 --> <view @click="toggleDropdown()" :class="{ 'is-open': isOpen }">{{ selectedOption || placeholder }}</view> <!-- 下拉列表容器 --> <view v-if="isOpen" class="dropdown-list"> <view v-for="(option, index) in options" :key="index" @click="onSelect(option)" :class="{ 'selected-item': option === selectedOption }"> {{ option }} </view> </view> </view> </template> ``` #### 脚本逻辑编写 通过脚本来控制显示状态以及处理用户交互行为: ```javascript <script setup lang="ts"> import { ref, watch } from "vue"; // 定义 props 接收外部传入的数据 const props = defineProps({ modelValue: String, options: Array<string>, placeholder: { type: String, default: "请选择..." } }); let isOpen = ref(false); let selectedOption = ref(props.modelValue); watch(() => props.modelValue, (newValue) => { selectedOption.value = newValue; }, { immediate: true }); function toggleDropdown() { isOpen.value = !isOpen.value; } function onSelect(option: string) { selectedOption.value = option; emit('update:modelValue', option); // 更新父级绑定值 isOpen.value = false; // 关闭菜单 } </script> ``` #### 样式美化 为了让组件看起来更美观,还可以为其添加一些基础样式: ```css <style scoped> .select-container { position: relative; width: 100%; } .dropdown-list { border: 1px solid #ccc; max-height: 200px; overflow-y: auto; background-color: white; z-index: 999; } .selected-item { color: blue; } .is-open::after { content: "\25BC"; /* 向下的箭头 */ margin-left: 8px; } </style> ``` #### 使用说明 当集成此组件到页面时,只需按照如下方式进行调用即可: ```html <SelectComponent v-model="selectedItem" :options="['苹果','香蕉','橙子']"/> ``` 在此基础上可以根据实际项目调整和完善更多细节特性,比如增加搜索过滤等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值