H5+JS表格全选和删除

全选

let input=document.getElementsByTagName("input");
        $("#Complete").click(function () {//单击全选
            if (this.checked===true){
                for (let i=1;i<input.length;i++){
                    input[i].checked=true;
                }
            }else {
                for (let f=1;f<input.length;f++){
                    input[f].checked=false;
                }
            }
        });


单选全部选中

$(".second").click(function () {//单选全部选中
            let num=0;
            if (this.checked===false){
                document.getElementById("Complete").checked=false;
            }else {
                for (let i=1;i<input.length;i++){
                    if (input[i].checked){
                        num++;
                    }
                    if (num===input.length-1){
                        document.getElementById("Complete").checked=true;
                    }
                }
            }
        });


删除

$(".delete").click(function () {//删除
            for (let i=input.length-1;i>=1;i--){
                if (input[i].checked===true){
                    input[i].parentNode.parentNode.parentNode.removeChild(input[i].parentNode.parentNode)
                }
                if (input.length===1){
                    document.getElementById("Complete").checked=false;
                    $(".without").show();
                }
            }
        })


H5

<table border="1px" cellspacing="0" cellpadding="0" >
        <tr>
            <td><input type="checkbox" id="Complete">全选</td>
            <td>商品信息</td>
            <td>商品价格</td>
            <td>商品数量</td>
        </tr>
        <tr>
            <td><input type="checkbox" class="second"></td>
            <td>1.1</td>
            <td>1.2</td>
            <td>1.3</td>
        </tr>
        <tr>
            <td><input type="checkbox" class="second"></td>
            <td>2.1</td>
            <td>2.2</td>
            <td>2.3</td>
        </tr>
        <tr>
            <td><input type="checkbox" class="second"></td>
            <td>3.1</td>
            <td>3.2</td>
            <td>3.3</td>
        </tr>
        <tr>
            <td colspan="4" class="without">暂无数据...</td>
        </tr>
        <tr>
            <td style="cursor: pointer" colspan="4" class="delete">删除选中</td>
        </tr>
    </table>

<think>我们正在讨论的是在uni-app的小程序环境中使用uni-table组件实现多选功能并添加按钮。 根据引用[1],我们知道有一个自定义增强版的table表格组件,它兼容H5、小程序App端。因此,我们可以基于这个自定义组件来实现多选功能。 引用[2]提供了一个删除按钮的示例,它使用了u-button,并且通过@click事件绑定了一个删除方法,这个方法可能接收选中的项。 由于uni-app官方的表格组件可能不满足需求,我们可以参考自定义组件的实现思路。 实现多选功能通常需要以下步骤: 1.表格的每一行前面添加多选框(checkbox)或选择列。 2. 维护一个选中的数据列表(例如selectedList),用于记录当前选中的行。 3. 提供全选/反选功能(可选)。 4. 添加操作按钮(如删除按钮),并绑定事件,事件中可以对选中的数据进行操作。 由于引用中提到了自定义表格组件,我们可以假设这个自定义组件已经支持多选,或者我们可以按照上述思路扩展。 具体实现: 1.表格组件中,为每一行添加一个多选框,绑定选择事件。 2. 当用户选择某一行时,将这一行的数据添加到selectedList中,或者从selectedList中移除(如果取消选择)。 3.表格外部(或内部)添加按钮,比如删除按钮,当点击按钮时,执行删除操作,删除selectedList中选中的行。 参考引用[2]中的删除按钮,我们可以这样写: <u-button type="primary" size="mini" shape="circle" style='margin-left:20rpx;' @click="deleteBut(selectedList)">删除</u-button> 其中,selectedList是当前选中的行数据数组。 但是,我们需要注意的是,自定义表格组件可能已经封装了多选功能。因此,我们需要查看该组件的文档或代码,看是否提供了多选功能以及如何获取选中的数据。 如果自定义表格组件没有提供多选功能,我们可以自己实现: 假设我们使用自定义组件uni-table,我们可以这样设计: 在表格的列定义中,增加一列作为选择列: ```html <uni-table> <uni-tr> <uni-th width="50">选择</uni-th> <uni-th>姓名</uni-th> <!-- 其他列 --> </uni-tr> <uni-tr v-for="(item, index) in tableData" :key="index"> <uni-td> <checkbox :value="item.id" :checked="selectedIds.indexOf(item.id) >=0" @click="selectItem(item)"/> </uni-td> <uni-td>{{item.name}}</uni-td> <!-- 其他列 --> </uni-tr> </uni-table> ``` 在script中: ```javascript data() { return { tableData: [], // 表格数据 selectedIds: [], // 选中行的id数组,用于绑定checkbox的checked状态 selectedList: [] // 选中的行数据 } }, methods: { selectItem(item) { const index = this.selectedIds.indexOf(item.id); if (index >= 0) { this.selectedIds.splice(index, 1); this.selectedList.splice(index, 1); } else { this.selectedIds.push(item.id); this.selectedList.push(item); } }, deleteBut(selectedList) { // 删除选中的行 // 1. 从tableData中删除选中的数据 // 2. 清空selectedIdsselectedList // 注意:删除操作可能需要请求后端接口,这里仅做前端删除 if (selectedList.length === 0) { uni.showToast({ title: '请选择要删除的数据', icon: 'none' }); return; } const idsToDelete = selectedList.map(item => item.id); this.tableData = this.tableData.filter(item => !idsToDelete.includes(item.id)); this.selectedIds = []; this.selectedList = []; } } ``` 另外,如果需要全选功能,我们可以在表头也添加一个全选checkbox,并绑定全选事件。 注意:由于是在小程序环境中,我们使用的是uni-app的checkbox组件,而不是HTML的checkbox。 但是,上述实现是假设自定义表格组件没有提供多选功能的情况。如果引用[1]中的自定义表格组件已经实现了多选,那么我们应该使用它提供的方法。 根据引用[1]的描述,该自定义表格组件功能强大,很可能已经支持多选。因此,我们应该查看该组件的文档或者示例代码,看是否有以下特性: - 是否支持多选模式(通过属性开启) - 是否提供选中事件(@selection-change)来获取选中的数据 - 是否提供全选/反选功能 如果该组件支持,那么我们可以这样使用: ```html <custom-uni-table :data="tableData" selection @selection-change="handleSelectionChange"> <!-- 定义列 --> <custom-uni-column prop="name" label="姓名"></custom-uni-column> <!-- 其他列 --> </custom-uni-table> <u-button @click="deleteBut(selectedList)">删除</u-button> ``` 在script中: ```javascript data() { return { tableData: [], selectedList: [] // 选中的行数据 } }, methods: { handleSelectionChange(selection) { this.selectedList = selection; }, deleteBut(selectedList) { // 同上 } } ``` 因此,我们首先需要确定自定义表格组件是否已经支持多选。如果支持,则使用组件自带的多选功能;如果不支持,则自己实现。 总结: 1. 使用自定义表格组件(如引用[1]中的组件)来实现表格展示。 2. 根据组件文档,开启多选模式并监听选中事件,获取选中的数据。 3.表格下方添加按钮,如删除按钮,并绑定事件,对选中的数据进行操作。 由于引用[1]没有给出具体组件的文档,我们只能给出通用思路。实际开发中,需要参考具体组件的API。 最后,根据用户的问题,我们建议用户查阅所使用的自定义表格组件的文档,或者如果组件是自己开发的,则按照我们提供的思路添加多选功能。 相关问题: 1. 在uni-app中,如何实现表格全选功能? 2. 如何优化uni-app中表格多选操作的性能? 3. 在自定义uni-table组件中,如何兼容不同平台(H5、小程序、App)的多选样式交互? </think>在小程序中使用 `uni-table` 表格组件实现可多选功能并添加按钮的操作方法如下: ### 一、实现多选功能的步骤 1. **开启表格多选模式** 在 `uni-table` 组件中添加 `selection` 属性启用多选列: ```vue <uni-table selection ref="selection" @selection-change="handleSelectionChange"> <!-- 表格列配置 --> </uni-table> ``` - `ref="selection"`:用于获取表格实例 - `@selection-change`:监听选中行变化事件 2. **维护选中数据** 通过事件回调获取已选中的数据列表: ```javascript data() { return { selectedRows: [] // 存储选中行数据 } }, methods: { handleSelectionChange(e) { this.selectedRows = e.detail.data // 更新选中数据 } } ``` 3. **添加操作按钮** 在表格外部添加自定义按钮(如删除按钮): ```vue <u-button type="error" size="mini" @click="handleDelete" >删除选中项</u-button> ``` 4. **实现删除功能** 根据选中数据执行删除操作: ```javascript methods: { handleDelete() { if (this.selectedRows.length === 0) { return uni.showToast({ title: '请先选择数据', icon: 'none' }) } // 过滤删除后的数据(示例) this.tableData = this.tableData.filter( item => !this.selectedRows.includes(item.id) ) // 清空选中状态 this.$refs.selection.clearSelection() } } ``` ### 二、完整示例代码 ```vue <template> <view> <!-- 删除按钮 --> <u-button type="error" size="mini" style="margin-bottom: 20rpx;" @click="handleDelete" >删除选中项</u-button> <!-- 带多选的表格 --> <uni-table :data="tableData" selection ref="selection" @selection-change="handleSelectionChange" > <uni-tr> <uni-th width="50">ID</uni-th> <uni-th>姓名</uni-th> <uni-th>操作</uni-th> </uni-tr> <uni-tr v-for="item in tableData" :key="item.id"> <uni-td>{{ item.id }}</uni-td> <uni-td>{{ item.name }}</uni-td> <uni-td> <u-button size="mini" @click="editItem(item)">编辑</u-button> </uni-td> </uni-tr> </uni-table> </view> </template> <script> export default { data() { return { tableData: [ { id: 1, name: '张三' }, { id: 2, name: '李四' }, // ... ], selectedRows: [] } }, methods: { handleSelectionChange(e) { this.selectedRows = e.detail.data.map(item => item.id) }, handleDelete() { if (this.selectedRows.length === 0) { return uni.showToast({ title: '请选择数据', icon: 'none' }) } // 执行删除操作 this.tableData = this.tableData.filter( item => !this.selectedRows.includes(item.id) ) // 清空选择 this.$refs.selection.clearSelection() this.selectedRows = [] }, editItem(item) { console.log('编辑操作', item) } } } </script> ``` ### 三、关键注意事项 1. **多选联动** 通过 `this.$refs.selection.toggleSelection(row)` 可手动切换行选中状态 2. **跨平台兼容** 需在 `manifest.json` 中配置小程序组件库: ```json "mp-weixin": { "usingComponents": { "uni-table": "@/components/uni-table/uni-table" } } ``` 3. **性能优化** 当数据量较大时(>1000条),建议: - 使用分页加载 - 避免全量数据绑定 - 使用虚拟滚动插件(如 `z-paging`) 4. **样式自定义** 可通过 CSS 覆盖默认样式: ```css /* 自定义选中行背景色 */ .uni-table-tr--selected { background-color: #f0f9ff !important; } ``` > **组件参考**:实现基于 uni-app 官方表格组件增强的自定义实现,支持更灵活的多选操作按钮集成[^1]。删除操作实现参考了 uView UI 的按钮交互模式[^2]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值