日报2025319

今日继续学习el组件

<template>
  <div class="policy-container">
    <!-- 左侧分类列表 -->
    <div class="category-panel">
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <span>政策分类</span>
          </div>
        </template>
        <el-checkbox-group v-model="selectedTypes" @change="handleTypeChange">
          <el-checkbox
            v-for="category in categories"
            :key="category.typeId"
            :label="category.type">
            {{ category.type }}
          </el-checkbox>
        </el-checkbox-group>
      </el-card>
    </div>

    <!-- 右侧政策列表 -->
    <div class="policy-panel">
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <span>政策列表</span>
          </div>
        </template>

        <!-- 添加搜索条件区域 -->
        <div class="search-area">
          <div v-for="(condition, index) in searchConditions" :key="index" class="search-item">
            <el-select v-model="condition.field" placeholder="选择搜索字段" class="search-field">
              <el-option label="政策标题" value="name" />
              <el-option label="政策内容" value="text" />
              <el-option label="发文机构" value="organ" />
              <el-option label="政策分类" value="type" />
              <el-option label="政策文号" value="number" />
            </el-select>
            <el-input
              v-model="condition.value"
              placeholder="请输入搜索内容"
              class="search-input"
              @keyup.enter="handleSearch"
            />
          </div>
          <div class="button-group">
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </div>
        </div>

        <!-- 原有的表格和分页部分保持不变 -->
        <el-table :data="policies" style="width: 100%">
          <el-table-column prop="name" label="政策名称" />
          <el-table-column prop="type" label="政策类型" width="120" />
          <el-table-column prop="organ" label="制定机关" width="180" />
          <el-table-column prop="pubdata" label="发布日期" width="120">
            <template #default="scope">
              {{ formatDate(scope.row.pubdata) }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="120">
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="handleView(scope.row)">
                查看详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="pagination">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          />
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import request from '@/utils/request'
import { useRouter } from 'vue-router'

// 数据定义
const categories = ref([])
const policies = ref([])
const selectedTypes = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 获取政策分类
const loadCategories = async () => {
  try {
    const res = await request.get('/policy/categories')
    categories.value = res.data
  } catch (error) {
    console.error('加载分类失败:', error)
  }
}

// 获取政策列表
// 添加搜索条件相关的数据
const searchConditions = ref([
  { field: 'name', value: '' },
  { field: 'name', value: '' },
  { field: 'name', value: '' }
])

// 修改加载政策列表方法
const loadPolicies = async () => {
  try {
    // 过滤出有值的搜索条件
    const validConditions = searchConditions.value
      .filter(condition => condition.value.trim() !== '')
      .map(condition => ({
        field: condition.field,
        value: condition.value.trim()
      }));

    // 构建基础请求参数
    const params = new URLSearchParams();
    params.append('pageNum', currentPage.value);
    params.append('pageSize', pageSize.value);

    // 添加搜索条件,确保正确的 JSON 序列化
    if (validConditions.length > 0) {
      validConditions.forEach(condition => {
        const jsonStr = JSON.stringify(condition);
        params.append('conditions', jsonStr);
      });
    }

    // 添加类型条件
    if (selectedTypes.value && selectedTypes.value.length > 0) {
      selectedTypes.value.forEach(type => {
        params.append('types', type);
      });
    }

    const res = await request.get(`/policy/search/conditions?${params.toString()}`);
    policies.value = res.data.list;
    total.value = res.data.total;
  } catch (error) {
    console.error('加载政策失败:', error);
  }
}

// 添加搜索相关方法
const handleSearch = () => {
  currentPage.value = 1
  loadPolicies()
}

const resetSearch = () => {
  searchConditions.value.forEach(condition => {
    condition.field = 'name'
    condition.value = ''
  })
  handleSearch()
}

// 分类选择变化处理
const handleTypeChange = () => {
  currentPage.value = 1
  loadPolicies()
}

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  loadPolicies()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  loadPolicies()
}

// 日期格式化
const formatDate = (date) => {
  if (!date) return ''
  return new Date(date).toLocaleDateString()
}

// 页面加载时初始化数据
onMounted(() => {
  loadCategories()
  loadPolicies()
})

const router = useRouter()

const handleView = (row) => {
  window.open(`/policy/${row.id}`, '_blank')
}
</script>

<style scoped>
.policy-container {
  display: flex;
  gap: 20px;
  padding: 20px;
  height: calc(100vh - 100px);
}

.category-panel {
  width: 250px;
  flex-shrink: 0;
}

.policy-panel {
  flex-grow: 1;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-checkbox-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

.search-area {
  margin-bottom: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: flex-start;
}

.search-item {
  display: flex;
  gap: 10px;
  width: 100%;
}

.search-field {
  width: 150px;
}

.search-input {
  flex: 1;
  max-width: 400px;
}

.button-group {
  display: flex;
  gap: 10px;
  margin-top: 10px;
  align-self: flex-end;  /* 将按钮组靠右对齐 */
}

/* 删除原来的按钮样式 */
.search-area > div:last-child {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}
</style>
posted @ 2025-03-19 21:47  花落水无痕  阅读(16)  评论(0)    收藏  举报