form与table

用户管理界面设计

布局图片




<!DOCTYPE html>

<html ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="jquery.1.12.4.js"></script>
    <script type="text/javascript" src="angular-1.3.0.js"></script>
    <script type="text/javascript" src="data.js"></script>
    <title>用户管理</title>
    <style type="text/css">
        * {
            font-size: 14px;
            margin: 0;
            padding: 0;
        }


        body {
            padding: 16px 32px;
        }


        .search {
            position: relative;
            width: 640px;
            height: 40px;
            line-height: 40px;
            margin: 0 auto;
        }


        .search input {
            width: 152px;
            height: 24px;
            border: 1px solid #999;
            border-radius: 4px;
            padding-left: 8px;
        }


        .search select {
            width: 86px;
            height: 24px;
            border: 1px solid #999;
            border-radius: 4px;
        }


        .search .sort {
            position: absolute;
            right: 0;
            top: 8px;
        }


        .btns {
            width: 640px;
            height: 40px;
            line-height: 40px;
            margin: 0 auto;
        }


        .btns button {
            width: 80px;
            height: 24px;
            background-color: green;
            border: 0;
            border-radius: 4px;
            color: white;
        }


        .btns .remove_btn {
            background-color: red;
        }


        .list {
            width: 640px;
            margin: 0 auto;
        }


        .list thead tr {
            background-color: #777;
        }


        .list tbody tr:nth-child(odd) {
            background-color: #ccc;
        }


        .list tbody tr:nth-child(even) {
            background-color: #fff;
        }


        .form {
            width: 460px;
            border: 1px solid #999;
            margin: 0 auto;
        }


        .formErr {
            border: 1px solid red;
        }


        .form .label {
            display: block;
            float: left;
            width: 80px;
            height: 40px;
            line-height: 40px;
            text-align: end;
        }


        .form .txt {
            display: block;
            float: left;
            width: 340px;
            height: 40px;
            line-height: 40px;
            padding-left: 16px;
        }


        .form input {
            width: 312px;
            height: 24px;
            border: 1px solid #999;
            border-radius: 4px;
            padding-left: 8px;
        }


        .form select {
            width: 64px;
            height: 24px;
            border: 1px solid #999;
            border-radius: 4px;
        }


        .form button {
            width: 56px;
            height: 24px;
            background-color: green;
            border: 0;
            border-radius: 4px;
            color: white;
        }


        .form .errTips {
            width: 226px;
            background-color: lightpink;
            color: darkred;
            border-radius: 4px;
            margin-left: 96px;
            margin-top: 6px;
            margin-bottom: 4px;
            padding: 16px 48px;
        }
    </style>
    <script type="text/javascript">
        var app = angular.module("myApp", []);


        app.controller("myCtrl", function ($scope) {
            $scope.data = data;


            $scope.searchByLevel = function (user) {
                if ($scope.search_level == undefined || $scope.search_level == "") {
                    return true;
                }


                if (user.level <= $scope.search_level) {
                    return true;
                }


                return false;
            };


            $scope.orderByLevel = function (sortType) {
                if (sortType == "") {
                    return "";
                }


                if (sortType != 1 && sortType != 2) {
                    return "";
                }


                if (sortType == 2) {
                    return true;
                }


                return false;
            };
        });
    </script>
</head>
<body ng-controller="myCtrl">
<div class="search">
    <input type="text" placeholder="用户名搜索" ng-model="search_name"/>
    <select class="level" ng-model="search_level">
        <option value="">选择级别</option>
        <option value="1">&le; 1</option>
        <option value="2">&le; 2</option>
        <option value="3">&le; 3</option>
        <option value="4">&le; 4</option>
    </select>
    <select class="sort" ng-model="filter_sort">
        <option value="">排序</option>
        <option value="1">级别正序</option>
        <option value="2">级别倒序</option>
    </select>
</div>


<div class="btns">
    <button class="add_btn">新增用户</button>
    <button class="remove_btn">批量删除</button>
    <span>敏感词:抢劫、小偷</span>
</div>


<div class="list">
    <table width="640px" cellspacing="0" rules="cols" border="1px">
        <thead>
        <tr>
            <th width="6%">
                <input type="checkbox"/>
            </th>
            <th width="6%">ID</th>
            <th width="16%">用户名</th>
            <th width="18%">密码</th>
            <th width="10%">级别</th>
            <th width="32%">创建时间</th>
            <th width="12%">操作</th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="user in data | filter: {name: search_name} | filter: searchByLevel | orderBy: 'level': orderByLevel(filter_sort)">
            <td align="center">
                <input type="checkbox"/>
            </td>
            <td align="center">{{ user.id }}</td>
            <td align="center">{{ user.name }}</td>
            <td align="center">{{ user.password }}</td>
            <td align="center">{{ user.level }}</td>
            <td align="center">{{ user.dt_created }}</td>
            <td align="center">
                <button>修改密码</button>
            </td>
        </tr>
        </tbody>
    </table>
</div>


<div class="form">
    <div>
        <span class="label">新增用户</span>
        <span class="txt"></span>
    </div>
    <div style="clear: both"></div>
    <div>
        <span class="label">用户名</span>
        <span class="txt"><input type="text" placeholder="6-20个字符"/></span>
    </div>
    <div style="clear: both"></div>
    <div>
        <span class="label">密码</span>
        <span class="txt"><input type="password" placeholder="6-20个字符" class="formErr"/></span>
    </div>
    <div style="clear: both"></div>
    <div>
        <span class="label">重复密码</span>
        <span class="txt"><input type="password" placeholder="再次输入密码"/></span>
    </div>
    <div style="clear: both"></div>
    <div>
        <span class="label">级别</span>
        <span class="txt">
            <select>
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
            </select>
        </span>
    </div>
    <div style="clear: both"></div>
    <div class="errTips">
        <ul>
            <li>密码不能为空!</li>
            <li>密码长度不能小于6个字符!</li>
            <li>两次密码输入不一致!</li>
        </ul>
    </div>
    <div style="clear: both"></div>
    <div>
        <span class="label"></span>
        <span class="txt"><button>提交</button></span>
    </div>
    <div style="clear: both"></div>
</div>
<div class="form">
    <div>
        <span class="label">修改密码</span>
        <span class="txt"></span>
    </div>
    <div style="clear: both"></div>
    <div>
        <span class="label">用户名</span>
        <span class="txt"><input type="text" value="曹操" readonly/></span>
    </div>
    <div style="clear: both"></div>
    <div>
        <span class="label">旧密码</span>
        <span class="txt"><input type="password"/></span>
    </div>
    <div style="clear: both"></div>
    <div>
        <span class="label">新密码</span>
        <span class="txt"><input type="password" placeholder="6-20个字符" class="formErr"/></span>
    </div>
    <div style="clear: both"></div>
    <div>
        <span class="label">重复密码</span>
        <span class="txt"><input type="password" placeholder="再次输入密码"/></span>
    </div>
    <div style="clear: both"></div>
    <div class="errTips">
        <ul>
            <li>旧密码不能为空!</li>
            <li>旧密码不正确!</li>
            <li>新密码不能为空!</li>
            <li>新密码长度不能小于6个字符!</li>
            <li>两次密码输入不一致!</li>
        </ul>
    </div>
    <div style="clear: both"></div>
    <div>
        <span class="label"></span>
        <span class="txt"><button>提交</button></span>
    </div>
    <div style="clear: both"></div>
</div>
</body>
</html>
<think>好的,我现在需要回答用户关于如何实现Ant Design Form表单集成Table表格并获取表单中的表格数据的问题。首先,我得回想一下Ant Design(antd)的相关组件用法,特别是FormTable的结合使用。 用户可能希望在一个表单中包含一个表格,允许用户动态添加数据到表格中,并在提交表单时一起提交表格里的数据。这在实际应用中很常见,比如订单中的商品列表,用户可以不断添加商品到表格,最后一起提交。 首先,Form组件通常用于收集用户输入,而Table用于展示结构化数据。要将两者集成,可能需要将Table的数据作为Form的一个字段来管理。记得antd的Form可以通过Form.Item来管理各个字段,但Table的数据结构比较复杂,需要特殊处理。 接下来,我需要考虑如何将Table的数据绑定到Form上。antd的Form提供了form.setFieldsValueform.getFieldsValue来操作表单数据,但Table的数据通常是数组结构,每个元素可能有多个字段。这时候可能需要使用Form.List或者自定义的方式来管理。 不过,Form.List通常用于动态增减的表单项,每个项是简单的结构。对于表格数据,可能更适合在Form中维护一个状态变量来保存表格的数据,然后通过Form的onFinish事件来获取这个状态变量。也就是说,在表单提交时,将表格的数据表单的其他字段一起提交。 具体步骤可能包括: 1. 在Form组件的state中维护一个数组,用来保存表格的数据。 2. 提供一个添加数据的机制,比如通过Modal弹窗输入数据,然后将数据添加到数组中。 3. 在表格中显示这些数据,并且可能需要支持编辑或删除操作。 4. 在表单提交时,将这个数组作为表单的一个字段值提交。 另外,考虑到antd的Table组件本身并不是表单控件,直接将其数据绑定到Form可能需要一些手动处理。比如,可以隐藏一个Form.Item,其值就是表格的数据数组,通过setFieldsValue来更新这个隐藏字段的值。 或者,也可以不使用Form.Item来绑定表格数据,而是在表单提交时手动将表格数据合并到提交的数据中。这取决于具体需求。 用户可能还需要在表格中进行行内编辑,这时可以结合Editable Table的示例,使用Form表单来编辑每一行的数据。但这样复杂度较高,需要处理好表单嵌套的问题。 参考用户提供的引用,引用[1]中提到在state里设置一个空数组保存表单数据,然后通过消息发布订阅机制传递数据。这可能是指在一个组件中管理表单数据,另一个组件显示表格,通过状态提升或上下文来共享数据。但用户现在希望的是在同一个Form中集成Table,所以可能需要将表格数据作为表单的一部分。 引用[2]中提到了使用form.setFieldsValue进行数据回显,这说明可以通过编程方式设置表单的值。因此,可以设计一个Form.Item,其name为tableData,对应的值就是表格的数据数组。当用户添加或修改表格数据时,通过form.setFieldsValue更新这个字段的值。 可能的实现步骤: 1. 使用Form组件包裹整个表单,包括Table添加按钮。 2. 在Form中使用一个隐藏的Form.Item来保存表格数据,例如: <Form.Item name="tableData" hidden> <Input /> </Form.Item> 3. 当用户通过Modal或其他方式添加数据时,将新数据合并到现有的tableData数组中,并通过form.setFieldsValue更新该字段。 4. Table组件根据tableData的值渲染数据,同时提供删除或编辑功能,这些操作同样需要更新tableData的值。 5. 提交表单时,通过onFinish可以直接获取到tableData的值,其他表单字段一起提交。 另外,如果需要在表格中进行行内编辑,可能需要为每一行使用可编辑的单元格,结合Form.List或者单独的表单控件,但这样可能会更复杂。需要根据用户的需求选择合适的方式。 总结一下,关键点在于将表格数据作为Form的一个字段来管理,通过状态更新form.setFieldsValue来同步数据。这样在提交时就能获取到完整的表单数据,包括表格部分。</think>### Antd Form 集成 Table 的实现方式 #### 一、基础数据绑定 通过将 Table 数据作为 Form 的受控字段管理,实现数据联动: ```jsx import { Form, Table, Button } from 'antd'; function IntegratedForm() { const [form] = Form.useForm(); // 初始表格数据 const initialValues = { tableData: [ { key: 1, name: 'Item1', value: 100 } ] }; return ( <Form form={form} initialValues={initialValues}> <Form.Item name="tableData" hidden> <Input /> </Form.Item> <Table dataSource={form.getFieldValue('tableData')} columns={[ { title: 'Name', dataIndex: 'name' }, { title: 'Value', dataIndex: 'value' }, { title: 'Action', render: (_, record) => ( <Button onClick={() => handleDelete(record.key)}>Delete</Button> ) } ]} /> </Form> ); } ``` #### 二、动态数据操作 通过表单方法控制表格数据更新: ```jsx const handleAdd = () => { const newData = { key: Date.now(), name: `Item${form.getFieldValue('tableData').length + 1}`, value: 200 }; form.setFieldsValue({ tableData: [...form.getFieldValue('tableData'), newData] }); }; const handleDelete = (key) => { const newData = form.getFieldValue('tableData') .filter(item => item.key !== key); form.setFieldsValue({ tableData: newData }); }; ``` #### 三、数据提交验证 获取完整表单数据: ```jsx const onFinish = (values) => { console.log('包含表格数据的表单值:', values); // 提交数据到后端 }; // 表单提交按钮 <Form.Item> <Button type="primary" onClick={() => form.submit()}> 提交 </Button> </Form.Item> ``` #### 四、高级功能实现 1. **行内编辑**: ```jsx { title: 'Value', dataIndex: 'value', render: (text, record) => ( <Form.Item name={['tableData', record.key, 'value']} rules={[{ required: true }]} > <InputNumber /> </Form.Item> ) } ``` 2. **数据验证**: ```jsx <Form.Item name="tableData" rules={[ { validator: (_, value) => value?.length > 0 ? Promise.resolve() : Promise.reject('至少需要一条数据') } ]} > ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值