DictList.vue 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
<template>
  <a-card :bordered="false">

    <!-- 右侧面板 -->
    <a-col :span="5">
      <div class="table-operator" style="margin-bottom: 18px;">
        <a-row :gutter="24">
          <a-col :span="9">
            <a-button @click="addDict" type="primary" icon="plus">添加字典</a-button>
          </a-col>

          <a-col :span="9">
            <a-dropdown>
              <a-menu slot="overlay">
                <a-menu-item key="1" @click="editDict">编辑字典</a-menu-item>
                <a-menu-item key="2" @click="delDict">删除字典</a-menu-item>
                <a-menu-item key="3" @click="refreshDict">刷新</a-menu-item>
              </a-menu>
              <a-button style="margin-left: 8px">更多操作 <a-icon type="down" /></a-button>
            </a-dropdown>
          </a-col>
        </a-row>
      </div>

      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;padding: 5px">
        <a-icon type="select"/>&nbsp;
        当前选中:
        <span v-if="visible">
          <span style="color: #40a9ff;font-size: 12px;font-weight: 600;">{{ dict.dictName }}{{ dict.dictCode }}</span>
          <a><span @click="cancelDict" style="color:#2d8cf0"> 取消选择</span></a>
        </span>

      </div>

      <div>
        <a-input-search
          placeholder="输入搜索字典"
          @search="onSearch"
          style="margin-bottom: 16px;"
        />
        <a-tree
          :treeData="treeData"
          @select="this.onSelect">
        </a-tree>
      </div>
    </a-col>

    <!-- 中间面板 -->
    <a-col :span="1"/>

    <!-- 右侧面板 -->
    <a-col :span="18">
      <div class="table-operator">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :span="8">
              <a-form-item label="名称">
                <a-input placeholder="请输入名称" v-model="queryParam.itemText"></a-input>
              </a-form-item>
            </a-col>

            <a-col :span="8">
              <a-form-item label="状态">
                <a-select
                  style="width: 200px"
                  v-model="queryParam.status"
                  placeholder="请选择状态"
                >
                  <a-select-option value="1">正常</a-select-option>
                  <a-select-option value="0">禁用</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

            <a-col :span="8">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" icon="search" @click="searchByquery">查询</a-button>
                <a-button type="primary" icon="reload" @click="searchReset" style="margin-left: 8px">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div style="margin-bottom: 16px;">
        <a-button type="primary" icon="plus" @click="addDictItem">新增</a-button>
        <a-button type="primary" :loading="loadrefresh" icon="reload" @click="refreshDictItem" style="margin-left: 8px">刷新</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay" @click="handleMenuClick">
            <a-menu-item key="1"><a-icon type="delete"/>删除</a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px">批量操作 <a-icon type="down" /></a-button>
        </a-dropdown>
      </div>

      <!-- table区域-begin -->
      <div>
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
          <i class="anticon anticon-info-circle ant-alert-icon"></i>
          已选择&nbsp;<a style="font-weight: 600">{{ selectedRowKeys.length }}</a>&nbsp;&nbsp;
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        </div>

        <a-table
          ref="table"
          bordered
          rowKey="id"
          size="middle"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
          @change="handleTableChange"
        >

          <span slot="action" slot-scope="text, record">
            <a @click="editDictItem(record)">编辑</a>
            <a-divider type="vertical" />
            <a-popconfirm title="确定删除吗?" @confirm="() => delDictItem(record.id)">
              <a>删除</a>
            </a-popconfirm>
          </span>

        </a-table>
      </div>
      <!-- table区域-end -->
    </a-col>

    <dict-modal ref="modal" @ok="modalFormOk"></dict-modal>  <!-- 字典类型 -->
    <dict-item-modal ref="itemModal" @ok="modalFormOk"></dict-item-modal> <!-- 字典数据 -->
  </a-card>
</template>

<script>
  import { filterObj } from '@/utils/util';
  import DictModal from './modules/DictModal'
  import DictItemModal from './modules/DictItemModal'
  import {delDict,getDictItemList,delDictItem,delDictItemList,treeList} from '@/api/api'

  export default {
    name: "DictList",
    components: {DictModal,DictItemModal},
    data () {
      return {
        description: '这是数据字典页面',
        visible:false,
        // 查询条件
        queryParam: {
          dictId:"",
          dictName:null,
          delFlag:"1",
          status:[],
        },
        // 表头
        columns: [
          {
            title: '排序',
            align: "center",
            dataIndex: 'sortOrder',
            width: "100px",
            sorter: true
          },
          {
            title: '名称',
            align: "center",
            dataIndex: 'itemText',
          },
          {
            title: '数据值',
            align: "center",
            dataIndex: 'itemValue',
          },
          {
            title: '描述',
            align: "center",
            dataIndex: 'description',
          },
          {
            title: '状态',
            align: "center",
            dataIndex: 'status',
            customRender:function (text) {
              if(text==1){
                return "正常";
              }else if(text==0){
                return "禁用";
              }else{
                return text;
              }
            },
          },
          {
            title: '创建时间',
            dataIndex: 'createTime',
            align: "center",
            sorter: true
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: "center",
            width: "120px",
            scopedSlots: {customRender: 'action'},
          }
        ],
        //字典数据集
        dataSource: [],
        //字典集
        treeData:[],
        dict:"",
        // 分页参数
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + "-" + range[1] + "" + total + ""
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        isorter: {
          column: 'createTime',
          order: 'asc',
        },
        loading: false,
        loadrefresh: false,
        selectedRowKeys: [],
        selectedRows: [],
      }
    },
    created() {
      this.loadData();
    },
    methods: {
      loadData (arg){
        if(arg===1){
          this.ipagination.current = 1;
        }
        this.loading=true;
        // 查询字典
        treeList({dictName:this.queryParam.dictName,delFlag:this.queryParam.delFlag}).then((res)=>{
          if(res.success){
            this.treeData=[];
            for(let a=0;a<res.result.length;a++){
              let temp = res.result[a];
              this.treeData.push(temp);
            }
          }
        })
        // 查询字典数据
        var params = this.getQueryParams();//查询条件
        getDictItemList(params).then((res)=>{
          if(res.success){
            this.dataSource = res.result.records;
            this.ipagination.total = res.result.total;
            this.loadrefresh=false;
            this.loading=false;
          }
        })
      },
      getQueryParams(){
        var param = Object.assign({}, this.queryParam,this.isorter);
        param.dictId = this.dictId;
        param.field = this.getQueryField();
        param.pageNo = this.ipagination.current;
        param.pageSize = this.ipagination.pageSize;
        return filterObj(param);
      },
      getQueryField(){
        var str = "id,";
        for(var a = 0;a<this.columns.length;a++){
          str+=","+this.columns[a].dataIndex;
        }
        return str;
      },

      //添加字典
      addDict(){
        this.$refs.modal.add();
        this.$refs.modal.title="新增";
      },
      //编辑字典
      editDict(){
        if(!this.dict.id){
          this.$message.warning("您还未选择要编辑的字典");
        }else{
          this.$refs.modal.edit(this.dict);
          this.$refs.modal.title="编辑";
        }
      },
      //删除字典
      delDict(){
        let that=this;
        if(!this.dict.id){
          this.$message.warning("您还未选择要删除的字典");
        }else{
          that.$confirm({
            title:"确认删除",
            content:"您确定要删除 "+this.dict.dictName+" 字典类型?",
            onOk: function(){
              delDict({id:that.dict.id}).then((res)=>{
                if(res.success){
                  that.$message.success(res.message);
                  that.loadData();
                  that.cancelDict();
                }else{
                  that.$message.warning(res.message);
                }
              });
            },
            onCancel() {},
          });
        }
      },
      //刷新字典
      refreshDict(){
        this.loadData();
      },
      //选择字典
      onSelect (selectedKeys, info) {
        this.dict = info.selectedNodes[0].data.props;
        this.dict.dictName = info.selectedNodes[0].data.props.title;
        this.dictId = this.dict.id;
        this.visible = true;
        this.loadData();
      },
      //取消选择
      cancelDict(){
        this.dict="";
        this.dictId = "";
        this.visible = false;
        this.loadData();
      },
      //查询字典
      onSearch (value) {
        if(value){
          this.queryParam.dictName = value;
        }else{
          this.queryParam.dictId = null;
          this.queryParam.dictName = null;
        }
        this.loadData();
      },


      // 添加字典数据
      addDictItem(){
        if(!this.dict.id){
          this.$message.warning("请选择一个字典类别");
        }else{
          this.$refs.itemModal.add(this.dict.id);
          this.$refs.itemModal.title="新增 "+this.dict.dictName+"("+this.dict.dictCode+") 的数据";
        }
      },
      //编辑字典数据
      editDictItem(record){
        if(!this.dict.id){
          this.$refs.itemModal.title="编辑字典的数据";
        }else{
          this.$refs.itemModal.title="编辑 "+this.dict.dictName+"("+this.dict.dictCode+") 的数据";
        }
        this.$refs.itemModal.edit(record);
      },
      handleMenuClick(e){
        if(e.key==1){
          this.batchdel();
        }
      },
      // 批量删除字典数据
      batchdel: function(){
        if(this.selectedRowKeys.length<=0){
          this.$message.warning("您还未选择要删除的数据");
        }else{
          let ids="";
          let that = this;
          that.selectedRowKeys.forEach(function(val){
            ids+=val+",";
          });
          that.$confirm({
            title:"确认删除",
            content:"您确定要删除所选的"+that.selectedRowKeys.length+"条数据?",
            onOk: function(){
              delDictItemList({ids:ids}).then((res)=>{
                if(res.success){
                  that.$message.success(res.message);
                  that.loadData();
                  that.onClearSelected();
                }else{
                  that.$message.warning(res.message);
                }
              });
            },
            onCancel() {},
          });
        }
      },
      //删除字典
      delDictItem(id){
        delDictItem({id:id}).then((res)=>{
          if(res.success){
            this.$message.success(res.message);
            this.loadData();
          }else{
            this.$message.warning(res.message);
          }
        });
      },
      // 查询字典数据
      searchByquery(){
        this.loadData();
      },
      // 重置字典数据
      searchReset(){
        var that = this;
        that.queryParam.status=[];
        that.queryParam.itemText="";
        that.loadData(this.ipagination.current);
      },
      // 刷新
      refreshDictItem(){
        this.loadrefresh=true;
        this.loadData();
      },

      handleTableChange(pagination, filters, sorter){
        //TODO 筛选
        if (Object.keys(sorter).length>0){
          this.isorter.column = sorter.field;
          this.isorter.order = "ascend"==sorter.order?"asc":"desc"
        }
        this.ipagination = pagination;
        this.loadData();
      },
      onSelectChange (selectedRowKeys,selectionRows) {
        this.selectedRowKeys = selectedRowKeys;
        this.selectionRows = selectionRows;
      },
      onClearSelected(){
        this.selectedRowKeys = [];
        this.selectionRows = [];
      },
      modalFormOk () {
        this.loadData();
      },
    },
    watch: {
      openKeys (val) {
        console.log('openKeys', val)
      },
    },
  }
</script>

<style scoped>
  .table-operator{margin-bottom: 10px}
  .ant-tree li span.ant-tree-switcher, .ant-tree li span.ant-tree-iconEle{width:0px}
</style>