antd table 取消hover、選中高亮、隔行變色

antd table 背景色、hover效果啥的基本上都是作用在td上,直接寫在tr上,會有很多未知效果
思路大概都是這樣,具體的還是要自己修改

取消hover效果

參考博客
直接照搬過來了,修改一下應(yīng)該能用

table:hover,
tr:hover,
thead:hover {
    background: #20293c !important;
}

.ant-table-tbody > tr.ant-table-row:hover > td {
    background: none !important;
}

選中行高亮

樣式(less)

  .selectedRow {
    & > td {
      // background: rgba(47, 122, 213, 0.2);
      background-color: #d7e4f7;
    }
  }
  .ant-table-tbody > .selectedRow > .ant-table-cell-row-hover {
    background-color: #d7e4f7;
  }

js

  const getRowClassName = (record, index) => {
    let className = '';
    let indexNum = index + 1;
// 當選中的id等于該行的id時,className=‘selectedRow’
    className =
      indexNum === selectIndex
        ? 'selectedRow' : '';
    return className;
  };

  <Table
     className="no-hover"
     bordered
     columns={tableColumns}
     dataSource={tableList}
     rowKey={(row) => (row.id)}
     loading={loading}
     rowClassName={getRowClassName}
     pagination={false}
     onRow={(record, index) => {
     return {
          onClick: () => {
              // 設(shè)置選中的index
              setSelectIndex(index + 1)
           },
      };
    }}
/>

隔行變色

樣式(less)

.oddRow {
    & > td {
      background-color: #fff;
    }
  }
  .evenRow {
    & > td {
      background-color: #f8f9fa;
    }
  }

js

  // 區(qū)分表格奇偶行
  const getRowClassName = (record, index) => {
    let className = '';
    className = index % 2 === 0 ? 'oddRow' : 'evenRow';
    return className;
  };

  <Table
     className="no-hover"
     bordered
     columns={tableColumns}
     dataSource={tableList}
     rowKey={(row) => (row.id)}
     loading={loading}
     rowClassName={getRowClassName}
     pagination={false}
/>

隔行變色 + 選中高亮

樣式(less)

  .oddRow {
    & > td {
      background-color: #fff;
    }
  }
  .evenRow {
    & > td {
      background-color: #f8f9fa;
    }
  }
  .selectedRow {
    & > td {
      background-color: #d7e4f7;
    }
  }
  .ant-table-tbody > .selectedRow > .ant-table-cell-row-hover {
    background-color: #d7e4f7;
  }

js

const getRowClassName = (record, index) => {
    let className = '';
// 存在分頁時
    let indexNum = index + (current - 1) * pageSize + 1;
    className =
      indexNum === selectIndex
        ? 'selectRow'
        : index % 2 === 0
        ? 'oddRow'
        : 'evenRow';
    return className;
  };

  <Table
     className="no-hover"
     bordered
     columns={tableColumns}
     dataSource={tableList}
     rowKey={(row) => (row.id)}
     loading={loading}
     rowClassName={getRowClassName}
     pagination={false}
     onRow={(record, index) => {
     return {
          onClick: () => {
              // 設(shè)置選中的index
              setSelectIndex(index + (current - 1) * pageSize + 1);
           },
      };
    }}
/>

2022-03-02

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容