把1,4,5-8,9,89,11-14 => 1,4-9,11-14,89

本文介绍了一种将带有范围的数字字符串进行解析、合并及排序的方法,并最终转换为规范格式的字符串。通过使用C++标准库函数,文章实现了字符串的分割、范围的解析与数字数组的生成,并对数组进行了排序,最后将排序后的数字重新组合成符合要求的格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//把1,4,5-8,9,89,11-14 => 1,4-9,11-14,89
//思路:先把这个字符串使用strtok按照","记性分解;
//把每个字符分解好的串中的"-"进行解析;
//把解析之后的转成数字数组并排序
//把排好序的数字数字连接成目标字符串
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdlib.h>
using namespace std;
typedef vector<int> vInt;
typedef vector<int>::iterator itInt;
typedef vector<char *> vStr;
typedef vector<char *>::iterator itStr;

//将字符串以","为界限进行分割
vStr parseStr(char *pStr){
	char *p;
	vStr vtmp;
	p=strtok(pStr,",");
	while(p!=NULL){
		vtmp.push_back(p);
		p=strtok(NULL,",");
	}
	return vtmp;
}
//对"-"进行解析
char* find(char *str1,char ch){
	char *tmp=str1;
	bool flag=false;
	while(*tmp!='\0'){
		if(*tmp == ch)
			return tmp;
		tmp++;
	}
	return NULL;
}
//将字符串转为int数组
vInt parase(vStr vstr){
	vInt vtmp;
	for(itStr it=vstr.begin();it!=vstr.end();it++){
		if(find(*it,'-')==NULL)
			vtmp.push_back(atoi(*it));
		else{
			char *p=find(*it,'-');
			int m=atoi(*it);
			int n=atoi((++p));
			for(int i=m;i<=n;i++)
				vtmp.push_back(i);
		}
	}
	return vtmp;
}
//把排序好的数字数组,按照要求划分字符字串
vStr concat(vInt vint){
	vStr vtmp;
	for(itInt it=vint.begin();it!=vint.end();it++){
		static char ch[]="-";
		char *p=new char[10];
		//这里注意判断最后一个元素,否则会越界
		if((it+1)==vint.end())
			vtmp.push_back(itoa(*it,p,10));
		else if(*it+1 != *(it+1)){
			vtmp.push_back(itoa(*it,p,10));
		}
		else{
			vtmp.push_back(itoa(*it,p,10));
			vtmp.push_back(ch);
			while(it!=vint.end() && *it+1 == *(it+1)){
				it++;
			}
			char *q=new char[10];
			vtmp.push_back(itoa(*it,q,10));
		}
	}
	return vtmp;
}

//把分解好的字串拼接成最终要求的字符串
string func(vStr v02){
	string str="";
	for(itStr it=v02.begin();it!=v02.end();it++){
		str+=*it;
		//这里注意不能直接使用*it,因为这样取出的是一个字符
		string str01=*it;
		string str02="";
		if(it+1 != v02.end())
			str02=*(it+1);
		if(str01!="-" && str02!="-")
			str+=",";
	}
	return str;
}

int main()
{
	vInt vint;
	vStr vstr;
	vStr v02;
	string str;
	//char pStr[]="1,4,5-8,9,89,11-14";
	char pStr[]="1,4,5-8,3,9,89,15,11-14";

	vstr=parseStr(pStr);
	vint=parase(vstr);
	sort(vint.begin(),vint.end()); //排序
	v02=concat(vint);
	str=func(v02);

	cout << str << endl;
	return 0;
}

<tr data-index="0" class="ant-table-row ng-star-inserted"><!----><td data-col-index="0" class="text-truncate text-center left-sticky-checkbox ant-table-cell ant-table-cell-fix-left ng-star-inserted" style="position: sticky; left: 0px;"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted"></span><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><label nz-checkbox="" class="ant-checkbox-wrapper ng-valid ng-star-inserted ng-dirty ng-touched ant-checkbox-wrapper-checked"><span class="ant-checkbox ant-checkbox-checked"><input type="checkbox" class="ant-checkbox-input ng-valid ng-dirty ng-touched"><span class="ant-checkbox-inner"></span></span><span></span></label><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="1" class="text-truncate text-col ant-table-cell ant-table-cell-fix-left ant-table-cell-fix-left-last ng-star-inserted" style="position: sticky; left: 45px;"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">项目编号</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="ZB2025-16" class="ng-star-inserted">ZB2025-16</span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="2" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">项目经办人</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="天众软件运维账号" class="ng-star-inserted">天众软件运维账号</span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="3" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">项目名称</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="测试" class="ng-star-inserted">测试</span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="4" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">当前进度</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="生成项目" class="ng-star-inserted">生成项目</span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="5" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">需求部门</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="教学与学生管理处" class="ng-star-inserted">教学与学生管理处</span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="6" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">需求部门对接人</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="7" class="text-truncate text-right ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">预算金额(万元)</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="90" class="ng-star-inserted">90</span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="8" class="text-truncate text-center ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">采购意向</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><a title="" class="ng-star-inserted"></a><!----><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="9" class="text-truncate date-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">意向公告时间</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="10" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">确认书编号</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="11" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">采购类别</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="12" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">采购类型</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="13" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">采购方式</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="14" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">代理公司</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="15" class="text-truncate date-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">招标公告时间</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="16" class="text-truncate text-center ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">招标公告链接</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><a title="" class="ng-star-inserted"></a><!----><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="17" class="text-truncate date-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">开标时间</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="18" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">开标地点</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="19" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">采购人代表</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="20" class="text-truncate text-right ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">中标金额(万元)</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="21" class="text-truncate text-right ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">招标结余(万元)</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="22" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">中标金额备注</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="23" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">中标单位</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="24" class="text-truncate text-center ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">中标结果链接</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><a title="" class="ng-star-inserted"></a><!----><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="25" class="text-truncate text-right ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">履约保证金(万元)</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="26" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">履约保证金缴纳状态</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="27" class="text-truncate date-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">项目生成时间</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="2025-07-16" class="ng-star-inserted">2025-07-16</span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="28" class="text-truncate date-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">进度记录时间</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="2025-07-16" class="ng-star-inserted">2025-07-16</span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="29" class="text-truncate date-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">项目完成时间</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="30" class="text-truncate text-col ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">备注</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span title="" class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="31" class="text-truncate text-center ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">采购请示及相关文件</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><a title="" class="ng-star-inserted"></a><!----><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="32" class="text-truncate text-center ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">预算执行确认书</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><a title="" class="ng-star-inserted"></a><!----><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="33" class="text-truncate text-center ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">招标文件</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><a title="" class="ng-star-inserted"></a><!----><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="34" class="text-truncate text-center ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">合同文件</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><a title="" class="ng-star-inserted"></a><!----><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="35" class="text-truncate text-center ant-table-cell ng-star-inserted"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">其他相关附件</span><small class="st__head-optional ng-star-inserted"> </small><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><a title="" class="ng-star-inserted"></a><!----><!----><!----><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><td data-col-index="36" class="text-truncate text-center ant-table-cell ant-table-cell-fix-right ant-table-cell-fix-right-first ng-star-inserted" style="position: sticky; right: 0px;"><span class="ant-table-rep__title ng-star-inserted"><span class="ng-star-inserted">操作</span><!----><!----><!----></span><!----><st-td><!----><!----><!----><!----><!----><!----><!----><!----><!----><!----><span class="ng-star-inserted"></span><!----><!----><!----><!----><!----><!----><!----><span class="ng-star-inserted"><!----><a class="st__btn-text ng-star-inserted"><i nz-icon="" class="anticon anticon-eye ng-star-inserted"><svg viewBox="64 64 896 896" focusable="false" fill="currentColor" width="1em" height="1em" data-icon="eye" aria-hidden="true"><path d="M942.2 486.2C847.4 286.5 704.1 186 512 186c-192.2 0-335.4 100.5-430.2 300.3a60.3 60.3 0 000 51.5C176.6 737.5 319.9 838 512 838c192.2 0 335.4-100.5 430.2-300.3 7.7-16.2 7.7-35 0-51.5zM512 766c-161.3 0-279.4-81.8-362.7-254C232.6 339.8 350.7 258 512 258c161.3 0 279.4 81.8 362.7 254C791.5 684.2 673.4 766 512 766zm-4-430c-97.2 0-176 78.8-176 176s78.8 176 176 176 176-78.8 176-176-78.8-176-176-176zm0 288c-61.9 0-112-50.1-112-112s50.1-112 112-112 112 50.1 112 112-50.1 112-112 112z"></path></svg></i><!----><!----><!----><!----><span class="pl-xs ng-star-inserted"></span><!----></a><!----><!----><!----><!----><!----><!----></span><!----><nz-divider nztype="vertical" class="ant-divider ant-divider-vertical ng-star-inserted"><!----></nz-divider><!----><!----><!----><!----><span class="ng-star-inserted"><!----><a class="st__btn-text ng-star-inserted"><i nz-icon="" class="anticon anticon-edit ng-star-inserted"><svg viewBox="64 64 896 896" focusable="false" fill="currentColor" width="1em" height="1em" data-icon="edit" aria-hidden="true"><path d="M257.7 752c2 0 4-.2 6-.5L431.9 722c2-.4 3.9-1.3 5.3-2.8l423.9-423.9a9.96 9.96 0 000-14.1L694.9 114.9c-1.9-1.9-4.4-2.9-7.1-2.9s-5.2 1-7.1 2.9L256.8 538.8c-1.5 1.5-2.4 3.3-2.8 5.3l-29.5 168.2a33.5 33.5 0 009.4 29.8c6.6 6.4 14.9 9.9 23.8 9.9zm67.4-174.4L687.8 215l73.3 73.3-362.7 362.6-88.9 15.7 15.6-89zM880 836H144c-17.7 0-32 14.3-32 32v36c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-36c0-17.7-14.3-32-32-32z"></path></svg></i><!----><!----><!----><!----><span class="pl-xs ng-star-inserted"></span><!----></a><!----><!----><!----><!----><!----><!----></span><!----><nz-divider nztype="vertical" class="ant-divider ant-divider-vertical ng-star-inserted"><!----></nz-divider><!----><!----><!----><!----><span class="ng-star-inserted"><a nz-popconfirm="" class="st__btn-text ng-star-inserted"><i nz-icon="" class="anticon anticon-delete ng-star-inserted"><svg viewBox="64 64 896 896" focusable="false" fill="currentColor" width="1em" height="1em" data-icon="delete" aria-hidden="true"><path d="M292.7 840h438.6l24.2-512h-487z" fill="#ffeae6"></path><path d="M864 256H736v-80c0-35.3-28.7-64-64-64H352c-35.3 0-64 28.7-64 64v80H160c-17.7 0-32 14.3-32 32v32c0 4.4 3.6 8 8 8h60.4l24.7 523c1.6 34.1 29.8 61 63.9 61h454c34.2 0 62.3-26.8 63.9-61l24.7-523H888c4.4 0 8-3.6 8-8v-32c0-17.7-14.3-32-32-32zm-504-72h304v72H360v-72zm371.3 656H292.7l-24.2-512h487l-24.2 512z" fill="#f00"></path></svg></i><!----><!----><!----><!----><span class="pl-xs ng-star-inserted"></span><!----></a><!----><!----><!----><!----><!----><!----><!----><!----></span><!----><!----><!----><!----><!----><!----></st-td></td><!----><!----><!----></tr> 选中行有ant-checkbox-wrapper-checked属性 如何将整行高亮
最新发布
07-17
<think>我们面对的问题:在Ant Design的Angular组件库(NG-ZORRO)中,当选中表格行的复选框时,如何实现该行高亮显示。 根据引用[1]中提到的CSS方法,我们可以通过CSS选择器来实现:当一行被选中时(通常会有特定的类名,如.ant-table-row-selected),我们改变该行的背景色。 但是,在NG-ZORRO中,当使用内置的rowSelection时,选中行会自动添加一个类名:.ant-table-row-selected。因此,我们可以通过CSS来定义这个类名的样式。 然而,用户提到的是“当选中具有ant-checkbox-wrapper-checked属性的复选框时”,注意:这个类名实际上是复选框被选中时包裹元素的类名,而不是行的类名。在NG-ZORRO中,我们更推荐使用行选择内置的选中状态,而不是通过复选框的类名来操作。 但是,如果我们希望根据复选框的选中状态来设置行的样式,我们可以这样考虑: 1. 在表格中,每一行的复选框被选中时,NG-ZORRO会为这一行添加一个类名(如.ant-table-row-selected),我们可以直接使用这个类名来设置样式。 2. 如果用户希望自定义高亮样式,可以通过覆盖这个类名的样式来实现。 步骤: 1. 在组件的CSS文件中添加以下样式: .ant-table-row.ant-table-row-selected > td { background-color: #e6f7ff; /* 高亮的背景色 */ } 2. 如果用户希望同时改变该行所有单元格的样式,也可以使用上述选择器。 但是,如果用户希望使用复选框的类名(ant-checkbox-wrapper-checked)来设置行样式,我们可以通过以下方式: 由于复选框和行元素是兄弟元素或嵌套关系,我们可以通过CSS选择器实现:当同一行内的复选框具有.ant-checkbox-wrapper-checked类时,设置该行的背景色。 但是,在NG-ZORRO的表格中,复选框和行之间的关系是:每一行的第一个单元格(td)内包含复选框。因此,我们可以这样写: tr:hover { ... /* 原有悬停样式 */ } /* 当该行的复选框被选中时,即复选框的包裹元素有.ant-checkbox-wrapper-checked类 */ tr td .ant-checkbox-wrapper.ant-checkbox-wrapper-checked { /* 这个样式是针对复选框包裹元素的,不是行 */ } /* 但是,我们无法通过复选框的状态直接选择到行(tr),因为CSS没有父选择器。 */ 因此,我们只能通过NG-ZORRO内置的行选中状态类(.ant-table-row-selected)来实现。 另一种方案:使用JavaScript(Angular)动态添加类。 我们可以通过监听选中事件,然后动态给行添加类名。但是,NG-ZORRO的表格组件已经为我们处理了选中状态,所以我们只需要利用它提供的类即可。 所以,推荐使用第一种方法:使用.ant-table-row-selected类。 如果用户没有使用内置的rowSelection,而是自己实现的复选框列,那么就需要自己管理选中状态,并通过[ngClass]来动态添加类。 假设用户使用的是内置的rowSelection: 在表格组件中,设置rowSelection属性: <nz-table [nzData]="data" [nzPageSize]="10" [nzRowSelection]="rowSelection" > ...列定义 </nz-table> 在组件类中定义rowSelection: rowSelection = { onChange: (selectedRowKeys: string[], selectedRows: any[]) => { // 可以在这里处理选中变化事件 } }; 然后通过CSS设置选中行的样式即可。 如果用户希望自定义高亮颜色,可以覆盖.ant-table-row-selected类的背景色。 但是,如果用户希望使用不同的高亮颜色,或者需要根据行的其他状态来改变高亮样式,那么可能需要通过[ngClass]来动态设置行的类名。 总结: 1. 使用内置rowSelection,然后通过CSS设置.ant-table-row-selected的样式。 2. 如果自定义复选框列,则需要在每个行上使用[ngClass]绑定,根据该行是否被选中来添加一个类,然后定义这个类的样式。 下面给出自定义复选框列的方案(因为用户可能没有使用rowSelection,而是自己实现的复选框): 在列定义中,我们可以这样写: <nz-table #basicTable [nzData]="listOfData"> <thead> <tr> <th> <label nz-checkbox [(ngModel)]="allChecked" (ngModelChange)="checkAll($event)"></label> </th> <th>Name</th> <th>Age</th> <th>Address</th> </tr> </thead> <tbody> <tr *ngFor="let data of basicTable.data" [class.highlight-row]="data.checked"> <td> <label nz-checkbox [(ngModel)]="data.checked" (ngModelChange)="refreshStatus()"></label> </td> <td>{{data.name}}</td> <td>{{data.age}}</td> <td>{{data.address}}</td> </tr> </tbody> </nz-table> 然后在组件的CSS中: .highlight-row { background-color: #e6f7ff; } 这样,当data.checked为true时,该行就会添加highlight-row类。 但是,注意:这种方式需要自己实现全选逻辑(即checkAll和refreshStatus方法)。 因此,根据用户的具体实现方式,选择上述两种方案之一。 由于用户问题中提到了“ant-checkbox-wrapper-checked”,这个类名是Ant Design的复选框被选中时添加到包裹元素的类,我们可以利用这个类名来设置行的样式吗? 由于CSS没有父选择器,我们无法直接通过这个类名选择到行(tr),所以不能直接设置。但是,我们可以通过JavaScript(Angular)来监听复选框的状态变化,然后动态设置行的类名,也就是上面自定义列的方法。 因此,我们推荐: 1. 如果使用内置rowSelection,使用内置的.ant-table-row-selected类。 2. 如果自定义复选框列,使用[ngClass]绑定,根据数据中的checked属性(或类似属性)来添加高亮类。 下面给出一个使用内置rowSelection的完整示例: 组件模板: <nz-table #basicTable [nzData]="listOfData" [nzPageSize]="10"> <thead> <tr> <th></th> <!-- 选择列 --> <th>Name</th> <th>Age</th> <th>Address</th> </tr> </thead> <tbody> <tr *ngFor="let data of basicTable.data"> <td> <label nz-checkbox [nzChecked]="data.checked" (ngModelChange)="onCheckedChange(data, $event)"></label> </td> <td>{{data.name}}</td> <td>{{data.age}}</td> <td>{{data.address}}</td> </tr> </tbody> </nz-table> 但是,注意:这种方式还是自定义列,因为内置的rowSelection是通过nz-table的[nzRowSelection]属性来设置的,它会自动在每一行前面加上选择框,并且管理选中状态。 所以,我们更推荐使用内置的rowSelection: <nz-table #basicTable [nzData]="listOfData" [nzPageSize]="10" [nzRowSelection]="rowSelection"> <thead> <tr> <th>Name</th> <th>Age</th> <th>Address</th> </tr> </thead> <tbody> <tr *ngFor="let data of basicTable.data"> <td>{{data.name}}</td> <td>{{data.age}}</td> <td>{{data.address}}</td> </tr> </tbody> </nz-table> 在组件类中: import { NzTableComponent } from 'ng-zorro-antd/table'; @ViewChild('basicTable') basicTable: NzTableComponent; rowSelection = { selectedRowKeys: [], onChange: (selectedRowKeys: any[], selectedRows: any[]) => { console.log(selectedRowKeys, selectedRows); } }; 然后,在组件的样式文件中: .ant-table-tbody > tr.ant-table-row-selected > td { background: #e6f7ff; } 这样,当行被选中时,就会高亮。 注意:NG-ZORRO的版本可能会影响类名,但通常都是使用.ant-table-row-selected。 最后,如果用户希望高亮样式在复选框被选中时立即生效,那么使用内置的rowSelection是自动处理的,而自定义列需要自己管理选中状态。 因此,根据用户的具体情况,选择合适的方法。 如果用户使用的是最新版本的NG-ZORRO,并且希望使用内置的rowSelection,那么通过CSS覆盖是最简单的方法。 如果用户需要更复杂的行高亮逻辑(比如根据多个条件),那么可能需要使用自定义列,并在行上使用[ngClass]绑定。 根据用户的问题描述,他们希望选中复选框时行高亮,这正好是内置rowSelection的功能之一,所以推荐使用内置功能。 但是,用户提到了“ant-checkbox-wrapper-checked”属性,这可能是他们自己已经实现了复选框列,所以下面也给出自定义列的方法。 总结回答: 有两种方法可以实现: 方法一(推荐):使用NG-ZORRO内置的行选择(rowSelection),然后通过CSS设置选中行的背景色。 方法二:自定义复选框列,然后通过[ngClass]绑定根据数据中的选中状态属性来动态添加高亮类。 下面分别给出实现步骤: 方法一(内置rowSelection): 1. 在表格上设置[nzRowSelection]属性,并定义rowSelection对象(至少包含onChange事件处理)。 2. 在组件的样式文件中添加: .ant-table-tbody > tr.ant-table-row-selected > td { background: #e6f7ff; /* 或者你喜欢的任何颜色 */ } 方法二(自定义复选框列): 1. 在表格数据中为每个数据项添加一个布尔属性(如checked)来表示是否选中。 2. 在表格行(tr)上使用[ngClass]绑定,当数据项的checked为true时,添加一个高亮类(如'highlight-row')。 3. 在组件的样式文件中定义.highlight-row的样式。 4. 在复选框的ngModelChange事件中,更新数据项的checked属性,并手动触发变更检测(如果必要)。 示例代码(方法二): 模板: <nz-table #basicTable [nzData]="listOfData"> <thead> <tr> <th>选择</th> <th>Name</th> <th>Age</th> <th>Address</th> </tr> </thead> <tbody> <tr *ngFor="let data of basicTable.data" [class.highlight-row]="data.checked"> <td> <label nz-checkbox [(ngModel)]="data.checked"></label> </td> <td>{{data.name}}</td> <td>{{data.age}}</td> <td>{{data.address}}</td> </tr> </tbody> </nz-table> 组件样式: .highlight-row { background-color: #e6f7ff; } 注意:方法二需要自己处理全选/全不选等逻辑,而内置的rowSelection则自动支持。 因此,如果只需要简单的行选择高亮,方法一更简洁;如果需要更复杂的控制,可以选择方法二。 根据用户的具体场景选择合适的方法。</think>在 Ant Design 的 Angular 组件库(NG-ZORRO)中,实现选中复选框时表格行高亮显示,可通过以下两种方法实现: --- ### 方法一:使用 CSS 样式绑定(推荐) 通过 NG-ZORRO 内置的选中状态类名 `.ant-table-row-selected` 实现高亮效果: 1. **添加全局样式** 在组件的 CSS 文件中添加: ```css /* 选中行的背景色 */ .ant-table-tbody > tr.ant-table-row-selected > td { background-color: #e6f7ff !important; /* 高亮颜色 */ } ``` 2. **启用表格的行选择功能** 在模板中使用 `nzRowSelection` 属性: ```html <nz-table [nzData]="data" [nzPageSize]="10" [nzRowSelection]="{ type: 'checkbox', selectedRowKeys: selectedRows, onChange: onSelectChange }" > <thead> <tr> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr *ngFor="let item of data"> <td>{{ item.name }}</td> <td>{{ item.age }}</td> </tr> </tbody> </nz-table> ``` 3. **组件逻辑** ```typescript export class YourComponent { data = [{ id: 1, name: 'John', age: 30 }, ...]; selectedRows: number[] = []; // 存储选中行的 key // 选中变化时更新 onSelectChange(selectedKeys: number[]) { this.selectedRows = selectedKeys; } } ``` **原理**: 当复选框被选中时,NG-ZORRO 会自动为对应行添加 `.ant-table-row-selected` 类名,通过 CSS 覆盖其背景色即可实现高亮[^1]。 --- ### 方法二:动态绑定行类名 通过 `[nzClassName]` 动态绑定行类名: 1. **模板设置** ```html <nz-table [nzData]="data"> <tbody> <tr *ngFor="let item of data" [nzClassName]="isSelected(item.id) ? 'highlight-row' : ''" > <td> <label nz-checkbox [(ngModel)]="item.checked" (ngModelChange)="toggleHighlight(item.id)" ></label> </td> <td>{{ item.name }}</td> </tr> </tbody> </nz-table> ``` 2. **组件逻辑** ```typescript export class YourComponent { data = [ { id: 1, name: 'John', checked: false }, { id: 2, name: 'Alice', checked: false } ]; selectedIds: number[] = []; // 切换选中状态 toggleHighlight(id: number) { if (this.selectedIds.includes(id)) { this.selectedIds = this.selectedIds.filter(i => i !== id); } else { this.selectedIds.push(id); } } // 判断是否选中 isSelected(id: number): boolean { return this.selectedIds.includes(id); } } ``` 3. **CSS 样式** ```css .highlight-row { background-color: #e6f7ff; transition: background-color 0.3s; } ``` **原理**: 通过复选框的 `ngModel` 绑定数据项的选中状态,动态为行添加高亮类名[^2][^4]。 --- ### 关键注意事项 1. **禁用复选框事件穿透** 若遇到点击行时触发复选框的问题,添加以下 CSS: ```css .ant-table-row { pointer-events: none; /* 禁用行事件 */ } .ant-table-row td .ant-checkbox-wrapper { pointer-events: auto; /* 启用复选框事件 */ } ``` 2. **性能优化** 大数据量时建议使用方法一(内置行选择),避免为每行创建动态绑定[^5]。 3. **自定义颜色** 替换 `#e6f7ff` 为任何颜色值(如 `#fff2e8` 橙色高亮)。 --- ### 最终效果 - 选中复选框 → 整行背景色高亮 - 取消选中 → 高亮消失 - 支持多行同时高亮
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值