编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

aggrid识别cell中url并实现跳转

wxchong 2024-06-19 22:22:59 开源技术 14 ℃ 0 评论

comlum配置

    {
      headerName: I18N.value('pages_comment'),
      field: 'comments',
      sortable: true,
      cellRenderer: 'UrlDirectCellRender',
      cellRendererParams: {
        highlightText: searchText,
      },
    },

自定义cell

import React, { Component } from 'react';
import './urlDirectCellRender.scss';
import { replaceKeywords } from '../../common/utils';

export default class UrlDirectCellRender extends Component {
  getCellContent() {
    const {
      agGridReact, highlightText, value,
    } = this.props;
    let displayValue = value;
    const reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
    if (displayValue !== null && displayValue !== undefined) {
      displayValue = displayValue.replace(reg, "<a href=$1$2 target='_blank' style='text-decoration:underline;'>$1$2</a >");
    }
    const cellHeight = agGridReact.gridOptions.rowHeight;
    const contentStyle = this.getCellStyle();
    return (
      <div className="urlCellRender-container" style={{ height: `${cellHeight}px`, lineHeight: `${cellHeight}px` }}>
        <div
          style={contentStyle}
          title={value}
          className="commentUrl"
          // dangerouslySetInnerHTML={{ __html: displayValue.replace(reg, "<a href='$1$2' target='_blank' style='text-decoration:underline;'>$1$2</a >").replace(/\n/g, '<br />') }}
          dangerouslySetInnerHTML={{ __html: displayValue }}
        />
      </div>
    );
  }

  getCellStyle = () => {
    const contentStyle = { display: 'inine-block', fontSize: '16px', color: '#333' };
    contentStyle.userSelect = 'text';
    return contentStyle;
  }

  render() {
    const cellContent = this.getCellContent();
    return cellContent;
  }
}

核心代码

   
  // 获取url方法
   let testString = value;
    const reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
    if (testString !== null && testString !== undefined) {
      testString = displayValue.replace(reg, "<a href=$1$2 target='_blank' style='text-decoration:underline;'>$1$2</a >");
    }

  // cellcontent使用url
        <div
          style={contentStyle}
          title={value}
          className="commentUrl"
          dangerouslySetInnerHTML={{ __html: testString }}
        />

效果如图

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表