表单布局控制

本指南介绍如何使用 Kinlink Layout API 控制表单布局,实现响应式设计和自定义布局。

基础布局控制

了解如何控制基本的布局元素,如页眉、页脚和操作栏。

JavaScript
// 基础布局控制示例
(function() {
  kinlink.events.on(kinlink.FormEvents.FORM_LOADED, () => {
    const layout = kinlink.layoutApi;
    
    // 隐藏页眉
    layout.hideHeader();
    
    // 获取内容区域高度
    const contentHeight = layout.getContentAreaHeight();
    console.log('可用内容区域高度:', contentHeight);
  });
})();

响应式布局

学习如何根据设备类型调整表单布局,优化移动端体验。

JavaScript
// 响应式布局示例
(function() {
  kinlink.events.on(kinlink.FormEvents.FORM_LOADED, () => {
    const layout = kinlink.layoutApi;
    const form = kinlink.formApi;
    
    if (layout.checkIsMobileDevice()) {
      // 移动端布局优化
      layout.mobileHideFormActionBar();
      
      // 简化表单
      ['详细描述', '附加信息'].forEach(field => {
        form.hideField(field);
      });
      
      // 调整输入框大小
      ['姓名', '电话'].forEach(field => {
        form.setFieldComponentStyle(field, {
          fontSize: '16px',
          padding: '12px 8px'
        });
      });
    }
  });
  
  // 监听布局变化
  const cleanup = kinlink.layoutApi.onLayoutChange((detail) => {
    const { element, visible, height } = detail;
    console.log(`布局元素 ${element} 变化: 可见性=${visible}, 高度=${height}px`);
  });
})();

自定义布局

了解如何创建完全自定义的布局元素,满足特定的业务需求。

JavaScript
// 自定义布局示例
(function() {
  kinlink.events.on(kinlink.FormEvents.FORM_LOADED, () => {
    const layout = kinlink.layoutApi;
    const form = kinlink.formApi;
    
    if (layout.checkIsMobileDevice()) {
      // 创建自定义移动端操作栏
      layout.mobileHideFormActionBar();
      
      const customBar = document.createElement('div');
      customBar.style.position = 'fixed';
      customBar.style.bottom = '0';
      customBar.style.left = '0';
      customBar.style.right = '0';
      customBar.style.padding = '12px';
      customBar.style.backgroundColor = '#fff';
      customBar.style.boxShadow = '0 -2px 8px rgba(0,0,0,0.1)';
      customBar.style.zIndex = '100';
      
      const submitBtn = document.createElement('button');
      submitBtn.textContent = '提交';
      submitBtn.onclick = () => form.submit();
      submitBtn.style.width = '100%';
      submitBtn.style.padding = '12px';
      submitBtn.style.backgroundColor = '#1890ff';
      submitBtn.style.color = '#fff';
      submitBtn.style.border = 'none';
      submitBtn.style.borderRadius = '4px';
      
      customBar.appendChild(submitBtn);
      document.body.appendChild(customBar);
    }
  });
})();

布局控制要点

  • 设备类型检测
  • 元素显示控制
  • 高度计算
  • 自定义元素创建

最佳实践

  • 优先使用 API 方法
  • 注意清理自定义元素
  • 保持布局一致性
  • 考虑不同设备尺寸

注意事项

  • 避免频繁的布局变化
  • 注意自定义元素的层级
  • 处理布局变化的副作用
  • 考虑性能影响

相关资源