Skip to content

table 表格

表格组件

基本用法

    1. 只需对表格列进行配置即可实现简单的CRUD
    1. 双击行弹框编辑
    1. 鼠标移到列头,可进行列过滤,列配置
    1. 点击列头文字进行排序
    1. 拖动调整列宽度
    1. 拖动调整行高度功能
    1. 高度为固定高度,固定表头,滚动内容
    1. 支持单元格多选,多列选择,多行选择
    1. 支持 excel 复制、粘贴功能
<template>
    <dn-page>     
      <dn-form ref="formRef" v-bind="formOptions"></dn-form>
    </dn-page>   
  </template>
  
<script lang="jsx">
import {ref,defineComponent, onMounted} from "vue";
import { useForm,dn } from "@sdevnet/devnet-form";

const formRef = ref();
const formOptions = ref();

let data=[];
for(var i=0;i<35;i++){
  let row ={
    fid:(i+1),
    name:'姓名_'+i,   
    regdate:'2024-08-24 13:20:30',
    isadmin: i % 2==0 ?1:0,
    state: i % 2==0 ?'0':"1",
    remark:'remark'   
  };  
  data.push(row);
}
let selectData=[];
for(var i=0;i<10;i++){
  selectData.push({
    value:(i+1),
    text:'Name'+i
  })
}

let dictData=[
  {id:"1",text:'打开',type:'primary'},
  {id:"0",text:'关闭',type:'danger'}
];

function createFormOptions() {  
  const { buildFormOptions } = useForm();
  return buildFormOptions({
    columns: {
      childTable: {
        title: "",
        type: "table",
        form: {
          col:{span:24}, 
          props:{          
            table:{            
              component:{
                rowKey:'fid',
                height:'350px',
                onRowContextmenu:(row,column,event)=>{
                  console.log(row,column,event);
                  event?.preventDefault();
                  event?.stopPropagation();
                },
                onHeaderContextmenu:(e)=>{
                  console.log(e);
                  e.event?.preventDefault();
                  e.event?.stopPropagation();
                }
              },
              props:{
                headerAlign:'center',
                showNumber:true,
                showCheckbox:false,
                enabledEdit:true,
                editMode:'childForm',
                enabledFilter:true,
                enabledSort:true,
                //enabledRowDrag:true
              }, 
              columns:{                             
                name: {
                  title: "姓名",
                  type: "text",                   
                  props:{
                    filterData:selectData,
                    filterType:'multiple',
                  },                                            
                  form:{             
                    rules:[{required:true,message:'姓名必填'}],  
                    col:{span:24},
                    props:{                     
                      search:{
                        isSearch:true,
                        col:{span:12}
                      }
                    }
                  }
                },                
                state:{
                  title:'状态',                 
                  type:'checkboxlist',                   
                  props:{
                    valueMember:'id',
                    textMember:'text',
                    filterData:dictData,
                    align:'center',
                    request:{
                      getDataRequest:(query)=>{
                        return {data:dictData}
                      }
                    },
                  },
                  form:{
                    col:{span:24}
                  }
                },
                regdate:{
                    title:'日期',
                    type:'datetime', 
                    form:{
                      col:{span:24}
                    }                     

                },
                isadmin:{
                  title:'复选框',
                  type:'switch',
                  props:{
                    align:'center'
                  },
                  form:{
                    col:{span:24}
                  }                 
                }               
              },
              data:[],
              tableToolbar:{
                show:true,
                title:'基础表格',
                position:'top',
                buttons:{
                    add:{
                        show:true 
                    },
                    edit: {
                        show:true
                    },
                    delete:{
                        show:true
                    }                      
                }                                            
              }, 
            },   
            childForm:{
              props:{
                title:'弹框编辑'
              }
            },
            request:{
              getDataRequest:(query)=>{  
                  let sort= query.sort;             
                  let sortName='fid';
                  let order='asc';
                  if(sort && sort.length>0){                    
                     sortName=sort[0]['sortname'] ?? 'fid';
                     order = sort[0]['sortorder'] ?? 'asc';
                  }                  
                  data.sort((a,b)=>{
                    if(a[sortName]<b[sortName]){
                      return order==='asc'? -1 : 1;
                    }
                    if(a[sortName]>b[sortName]){
                      return order==='asc'? 1 : -1;
                    }
                    return 0;
                  });
                  
                  var ret={data:[]};
                  for(var i=((query.pageIndex-1) * query.pageSize);
                        i<((query.pageIndex) * query.pageSize);
                        i++){
                      if(i<data.length){
                        ret.data.push(data[i]);
                      }
                  }
                  var pcount= parseInt(data.length / query.pageSize);
                  if((data.length % query.pageSize) !== 0){
                    pcount++;
                  }                  
                  ret.pageindex=query.pageIndex;
                  ret.pagesize=query.pageSize;
                  ret.rowcount=data.length;
                  ret.pagecount=pcount;     
                  dn.message.success('模拟数据哦');
                  return ret;
                },
                saveRequest:({row,rows,key})=>{                
                  console.log(rows);                  
                  return {fid:999};
                },
                deleteRequest:({rows,rowIds})=>{                    
                },
                infoRequest:({mode,row})=>{                    
                    return row;
                },              
            },
            search:{
                show:true, 
                container:{
                    col:{span:12},  
                }
            },
            form:{                           
              beforeSubmit(scope){
                console.log('beforesubmit',scope);
              },
              async submit(context){
                //这里覆盖了框架保存,需要自己保存                 
                let mode =formRef.value.children.childTable.formBinding.mode?.name;                               
                dn.notification.info("submit表单提交:" + JSON.stringify(context.form));
                if (context.mode === "edit") {                
                  if (mode === "local") {              
                    formRef.value.children.childTable.updateRow(context.index, context.form);
                  } else {      
                    //这里回发后台请求,返回保存后的数据或包含主键ID的json               
                    return {fid:context.form.fid};
                  }
                } else if (context.mode === "add") {                 
                  if (mode === "local") {                   
                    formRef.value.children.childTable.insertRow(0, context.form);
                  } else {
                    //这里回发后台请求,返回保存后的数据或包含主键ID的json               
                    return {fid:999};
                  }
                }
              },
              afterSubmit(scope){
                console.log('aftersubmit:',scope);
              }
            } 
          }       
        }
      }
    }
  });
}

function initFormOptions() { 
  formOptions.value = createFormOptions();
  formOptions.value.initData = { fid:1,childTable:[]};
  
  return {
    formOptions,   
    formRef
  };
}

export default defineComponent({
  setup(props,ctx) {  
    onMounted(()=>{
      formRef.value.children.childTable.refresh();       
    });
   
     return {
      ...initFormOptions()
     }
  }
});
</script>

table 容器 API

PropsNameDescriptionTypeDefault
key字段名称子组件column配置json
└ title标题string
└ typetablestring
└ form表单栏位配置json
└└ colel-col配置,参考原生组件json
└└ props组件属性json
└└└ table表格配置json
└└└└ ......表格配置参考下面tableapi描述json
└└└ childForm弹窗子窗体配置json
└└└└ formComponent子窗体自定义表单组件string
自动根据栏位生成
└└└└ formProps传递给子窗的props参数
└└└└ closeRefresh子窗体关闭后刷新表格booleanfalse
└└└└ props属性配置
└└└└└ title弹窗标题string
└└└└└ router子窗体路由
└└└└└ onBeforeOpen子窗体打开前
└└└└└ onOpened子窗体打开后
└└└└└ onBeforeClose子窗体关闭前
└└└└└ onClosed子窗体关闭后
└└└└└ width子窗体宽度string | number
└└└└└ height子窗体高度string | number
└└└└└ dialogMode弹框模式

drawer
string
└└└└└ toolbarPosition子窗体工具栏位置'top'|'bottom'|'form'|formBottom'
└└└└ buttons子窗体按钮json
└└└└└ confirm确认按钮json默认实现
└└└└└ cancel关闭按钮json默认实现
└└└└└└ key按钮配置
└└└└└ key其它按钮json
└└└ slots组件槽位,json
└└└└ key组件槽位
└└└ requestapi请求配置json
└└└└ getDataRequest数据分页请求
└└└└ saveRequest保存数据请求
└└└└ deleteRequest删除数据请求
└└└└ infoRequest获取单条数据
└└└ search查询配置json
└└└└ show是否显示查询栏booleanfalse
└└└└ container查询栏容器json
└└└└└ colel-col配置,参考原生组件json
└└└└└ buttonColSpan查询按钮栏占位spannumber4
└└└└ buttons查询按钮配置json
└└└└└ search查询按钮json
└└└└└└ key按钮配置,参考按钮配置json
└└└ form默认弹窗的表单配置,参考Form
└└└└ key原生el-form组件属性,参考原生form组件
└└└└ rowrow 配置json
└└└└ col列配置object
└└└└ beforeSubmit表单校验完完成后,提交前处理
└└└└ submit点击保存按钮时执行操作,将覆盖reques.saveRequest
└└└└ afterSubmit表单提交后处理

table 容器 Slots

NameDescription
key_toolbar工具栏Title槽位
key_table-header表格头部
key_table-bottom表格底部

table 容器 Exposes

NameDescription
formBinding表格绑定属性
formExpose表格操作对象
tableRef原生table组件

table API

PropsNameDescriptionTypeDefault
...key上级key,见table容器apijson
└ table表格配置string
└└ component原生组件属性json
└└└ rowKey数据行键key,一般为主键字段string
└└└ height表格高度
└└└ border是否显示表格边框boolean
└└└ stripe是否奇偶行背景boolean
└└└ onHeaderContextmenu列头右击事件
└└└ onRowContextmenu行右击事件
└└└ default-expand-all树时是否展开所有boolean
└└└ tree-props树配置json
└└└└ checkStrictly复选框时父子节点是否不关联booleanfalse
└└└└ key参考原生el树组件
└└ props表格属性
└└└ highlightCurrentRow高亮当前行boolean
└└└ headerAlign列头对齐方式
└└└ align单元格对齐方式
└└└ showNumber是否显示序号列boolean
└└└ numberFixed序号列是否固定boolean
└└└ showCheckbox是否显示复选框列boolean
└└└ checkBoxFixed复选框列是否固定boolean
└└└ enabledFilter是否启用列头过滤boolean
└└└ enabledSort是否启用列头排序boolean
└└└ enabledEdit是否启用编辑boolean
└└└ editMode编辑模式childForm
└└└ rowHeight表格行高,px单位
└└└ rowMaxHeight表格最大行高,px单位string
└└└ linkSaveRefreshCtl保存表格后刷新的关联组件string
└└└ linkChildCtl选择行关联刷新的子组件string
└└└ linkChildCtlWhere选择行关联刷新的子组件的Where条件string
└└└ contextMenu右键菜单
└└└ mergeColumns合并列
└└└ mergeRows合并行
└└└ mergeCell自定义合并单元格函数
└└└ enabledRowDrag启用行拖动排序功能boolean
└└└ cellFocus单元格高亮booleantrue
└└└ cellMultiple单元格多选(开启后自动开启复制粘贴功能)booleanfalse
└└└ rowResizable拖动调整行高booleanfalse
└└└ headerCellMultiple列头,行序号拖动多选booleanfalse
└└└ headerCellFocus列头,行序号高亮booleanfalse
└└└ rememberChecked分页记住勾选行booleanfalse
└└└ readonly只读boolean | false
└└└ classFn样式boolean |
└└└ dataBind数据绑定属性json
└└└└ sortStr排序字符串string
└└└└ where绑定数据条件WhereCompare[]
└└└ events事件json
└└└└ onCurrentChange点击行数据时
└└└└ onSelectionChange点击复选框勾选时触发
└└└└ onRowDblclick双击行事件
└└└ slots组件槽位,json
└└└└ cell单元格槽位,json
└└└└└ cell_columnKey单元格槽位
└└ columns列配置json
└└└ key字段keyjson
└└└└ title标题string
└└└└ type类型,参考组件类型,linkcol为表格列专用string
└└└└ props属性配置json
└└└└└ enabledSort启用排序boolean
└└└└└ enabledFilter启用过滤boolean
└└└└└ enabledEdit启用编辑boolean
└└└└└ headerAlign列头对齐方式
└└└└└ align单元格对齐方式
└└└└└ columnShow表格列显示boolean
└└└└└ width列宽度number
└└└└└ filterType过滤类型
└└└└└ filterDatamultiple或下拉类型时的过滤下拉项array
└└└└└ valueMember下拉项的valuestring
└└└└└ textMember下拉项的textstring
└└└└└ request请求配置json
└└└└└└ getFilterDatafilterData的请求配置
└└└└└└ getDataRequest下拉选项的请求配置
└└└└ form编辑表单配置json
└└└└└ key编辑时需要配置,参考表单配置-form项json
└└└└└ props表单配置json
└└└└└└ defaultCopy新增时是否复制最后一行数据booleanfalse
└└└└└└ search查询配置json
└└└└└└└ isSearch是否查询booleanfalse
└└└└└└└ defaultValue查询默认值
└└ tableConfig列配置
└└└ storage存储配置的key,默认表单的[key]key
└└ tableToolbar工具栏位置
└└└ show是否显示boolean
└└└ position工具栏位置
└└└ buttons按钮,默认:新增,修改,删除json
└└└└ add新增按钮json默认实现
└└└└└ show显示新增按钮booleanfalse
└└└└ edit编辑按钮json默认实现
└└└└└ show显示编辑按钮booleanfalse
└└└└ delete删除按钮json默认实现
└└└└└ show显示删除按钮booleanfalse
└└└└ buttonkey其他自定义按钮json
└└└└ close删除按钮json默认实现
└└└└└ show是否显示booleanfalse
└└└└└ key参考buttonjson
└└└└└ debounce按钮防抖属性booleanfalse
└└└└└ delay按钮防抖延迟number2000

table 事件

NameDescription
原生事件component下写事件,参考原生组件事件

table Slots

NameDescription
cell_columnKey单元格槽位