ZBLOG

选择一对多关系的两个表中,实现多的一方表的前后端实现动态条件组合查询(要有一方的下拉框的查询)、分页、下拉框一页记录个数实现代码

以下是一个简单的示例,展示了如何实现多对一关系中,一方表的前后端动态条件组合查询、分页和下拉框查询:

  1. 后端代码(使用Java和Spring Boot):
// 一方表的实体类
@Entity
public class OneEntity {
    @Id
    private Long id;
    // 其他属性...
}

// 多方表的实体类
@Entity
public class ManyEntity {
    @Id
    private Long id;
    // 其他属性...

    @ManyToOne(fetch = FetchType.EAGER)
    private OneEntity oneEntity;
}

// 多方表的查询参数DTO类,用于接收前端传递的查询条件
public class ManyQueryDto {
    private Long oneEntityId; // 下拉框选项值

    // 其他查询条件...
    
    // Getters and Setters...
}

// 多方表的Repository接口
@Repository
public interface ManyRepository extends JpaRepository<ManyEntity, Long> {

    Page<ManyEntity> findByOneEntityIdAndOtherConditions(Long oneEntityId, OtherConditions... , Pageable pageable);
}

// 控制器类,处理前端请求并返回结果
@RestController
@RequestMapping("/api/many")
public class ManyController {
    
    @Autowired
    private ManyRepository manyRepository;

    @GetMapping("/search")
    public Page<ManyEntity> search(ManyQueryDto queryDto,
                                   @RequestParam(defaultValue = "0") int page,
                                   @RequestParam(defaultValue = "10") int size) {

        Long oneEntityId = queryDto.getOneEntityId();
        OtherConditions otherConditions = ...; // 从queryDto获取其他查询条件
        
        Pageable pageable = PageRequest.of(page, size);
        return manyRepository.findByOneEntityIdAndOtherConditions(oneEntityId, otherConditions..., pageable);
    }

    // 下拉框选项值的查询接口
    @GetMapping("/oneOptions")
    public List<OneEntity> getOneOptions() {
        return oneRepository.findAll();
    }
}
  1. 前端代码(使用HTML、JavaScript和Vue.js):
<template>
  <div>
    <!-- 下拉框 -->
    <select v-model="selectedOption">
      <option value="">全部</option>
      <option v-for="option in oneOptions" :key="option.id" :value="option.id">{{ option.name }}</option>
    </select>

    <!-- 其他查询条件 -->

    <!-- 搜索按钮 -->
    <button @click="search">搜索</button>

    <!-- 查询结果列表 -->
    <ul>
      <li v-for="item in resultData" :key="item.id">{{ item.name }}</li>
    </ul>

     <!-- 分页组件 -->
     <pagination :total-items="totalItems" :per-page="perPage" @paginate="paginate"></pagination>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      selectedOption: '',
      oneOptions: [], // 存放下拉框选项数据
      resultData: [], // 存放查询结果数据
      totalItems: 0,
      perPage: 10,
      currentPage: 1
   };
  },
  mounted() {
     this.fetchOneOptions(); // 初始化加载下拉框选项数据
     this.search(); // 执行默认搜索
  },
  methods: {
     fetchOneOptions() {
         axios.get('/api/many/oneOptions')
              .then(response => {
                  this.oneOptions = response.data;
              })
              .catch(error => {
                  console.error(error);
              });
     },
     search() {
         const params = {
             oneEntityId: this.selectedOption,
             // 其他查询条件...
             page: this.currentPage - 1,
             size: this.perPage
         };

         axios.get('/api/many/search', { params })
              .then(response => {
                   this.resultData = response.data.content;
                   this.totalItems = response.data.totalElements;
              })
              .catch(error => {
                   console.error(error);
               });
       },
       paginate(page) {
           this.currentPage = page;
           this.search();
       }
   }
};
</script>

请注意,这只是一个简单的示例,实际情况可能会根据具体的框架和需求进行调整。希望能对你有所帮助!


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=9105

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?