Table的两个属性rowIndex与cellIndex

HTML DOM rowIndex属性

 

功能:返回表格中行的序号。

语法:object.rowIndex

说明:该属性只读。

rowIndex用于判断当前单元格所处行的索引(从0开始)

 

实例1

获取指定行在表格中的序号。

<table width="80%" border="1" cellpadding="0" cellspacing="0" bordercolor="blue">
<thead>
<tr><td>1</td><td>2</td></tr>
</thead>
<tbody>
<tr id="rr"><td>3</td><td>4</td></tr>
<tr><td>5</td><td>6</td></tr>
</tbody>
</table>

<div>
<script type="text/javascript">
document.write( document.getElementById("rr").rowIndex );
</script>
</div>

通常我们可以把他们的事件写在TD里,因为rowIndex属性应该是属于<tr>标记,因此在判断rowIndex需要访问父节点,

<script type="text/javascript">
    function demo(elem){
        var tr = elem.parentNode;
        alert(tr.rowIndex);
    }
</script>
<table>
    <tr>
        <td></td>
        <td οnclick="demo(this);">edit</td>
    </tr>
    <tr>
        <td></td>
        <td οnclick="demo(this);">edit</td>
    </tr>
</table>

 

 

cellIndex属性

功能:返回单元格在行中的位置。

语法:object.cellIndex

说明:该属性只读。

cellIndex用于判断当前单元格所处列的索引(从0开始)
实例2
获取指定单元格在行中的位置。
<table id="mytable" width="80%" border="1" cellpadding="0" cellspacing="0" bordercolor="blue">
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td id="tt">6</td></tr>
<tr><td>7</td><td>8</td><td>9</td></tr>
</table>

<div>
<script type="text/javascript">
document.write( document.getElementById("tt").cellIndex );
</script>
</div>

rowIndex属性的值是行在表格中总的位置,包含了thead、tbody、tfoot各个区域。

好的,下面是一个示例代码,演示如何在表格组件中使用 oncell 属性合并单元格,具体实现方式如下: ```jsx import { Table } from 'antd'; const dataSource = [ { key: '1', name: 'John Brown', age: 32, address: 'New York Park', }, { key: '2', name: 'Jim Green', age: 42, address: 'London Park', }, { key: '3', name: 'Joe Black', age: 32, address: 'Sidney Park', }, { key: '4', name: 'Jim Red', age: 32, address: 'London Park', }, ]; const columns = [ { title: 'Name', dataIndex: 'name', key: 'name', render: (text, record, index) => { const obj = { children: text, props: {}, }; if (index === 1) { obj.props.rowSpan = 2; } // These two lines are used to merge cells vertically if (index === 2 || index === 3) { obj.props.rowSpan = 0; } return obj; }, }, { title: 'Age', dataIndex: 'age', key: 'age', }, { title: 'Address', dataIndex: 'address', key: 'address', }, ]; function App() { return ( <Table dataSource={dataSource} columns={columns} pagination={false} onCell={(record, rowIndex) => { return { onClick: (event) => {}, // 点击行 onDoubleClick: (event) => {}, // 双击行 onContextMenu: (event) => {}, // 右键点击行 onMouseEnter: (event) => {}, // 鼠标移入行 onMouseLeave: (event) => {}, // 鼠标移出行 // 这里可以使用 record 和 rowIndex 来进行单元格合并操作 }; }} /> ); } export default App; ``` 在这个示例中,我们在表格的 `columns` 属性中,使用了 `render` 方法对 `Name` 列进行了单元格合并操作,实现了在第二行合并两个单元格的效果。 同时,我们在 `onCell` 属性中,使用了 `record` 和 `rowIndex` 两个参数,来实现单元格的合并操作,具体实现可以根据实际需求进行调整。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值