antd多選下拉框一行展示的實現(xiàn)方式
我們都知道antd的select多選時,如果下拉框?qū)挾炔蛔悖瑒t自動浮動到下一行將下拉框撐大,但是這回影響到頁面的整體布局。
我們期望的效果是,下拉框只顯示一行的值,超出一行的部分自動隱藏。
下面有2種方案來實現(xiàn)這個效果。
1.利用浮動原理
設(shè)置下拉框的最大高度為一行的高度,然后超出的部分隱藏。
.ant-select-selection--multiple {
max-height: 32px;
overflow: hidden;
}
這種方式存在的弊端是如果有2個選項,一個很短一個很長,那么只能看到很短的值,長值被隱藏,會剩余很大的空白。

2.flex布局
將下拉框選項放到一行顯示,如果超出了下拉框長度則隱藏。默認的選項是采用float浮動顯示的,所以我們要屏蔽掉浮動效果。
.ant-select-selection--multiple .ant-select-selection__rendered {
overflow: hidden;
}
.ant-select-selection--multiple .ant-select-selection__rendered ul {
display: flex;
flex-wrap: nowrap;
overflow: hidden;
float: left;
}
.ant-select-selection--multiple .ant-select-selection__choice {
float: none;
overflow: visible;
}
.ant-select-selection--multiple .ant-select-search--inline {
float: none;
position: absolute;
}
.ant-select-selection--multiple {
max-height: 32px;
overflow: hidden;
}
這里重寫了下拉選項的樣式,達到了目的,但是會存在另一個問題,因為下拉選項排成了不換行的一列,那么必須指定下拉框的長度為固定值,不能使用百分比,因為一旦選中的下拉值超出了屏幕寬度,那么他會自動撐大整個屏幕的寬度。

補充知識:antd design Menu菜單下拉回調(diào)以及下拉列表時只能顯示一個列表,其余關(guān)閉
我做的是一個顯示全國省市區(qū)的下拉列表:如下圖

這個下拉列表是三層嵌套的下拉列表,統(tǒng)計列表不能同時打開,一次只能點開一個。點擊下拉時觸發(fā)函數(shù)獲得下一層級的下拉數(shù)據(jù)。
代碼如下:
render(){
let city=this.state.cityList.map(itemss=>(
<SubMenu
key={itemss.id}
title={<span ><Icon type="team" /><span className="nav-text">{itemss.name}</span></span>}
onTitleClick={this.getCountryList.bind(this,itemss.id,itemss.name)}
>
{
this.state.countryList.map(citem=>(
<Menu.Item key={citem.id}> <span onClick={this.checkedItem.bind(this,citem.id,citem.name)} >{citem.name}</span></Menu.Item>
))
}
</SubMenu>
));
const { startValue, endValue, endOpen } = this.state;
return(
<div className="div-body">
<div className="div-page">
<div className="div_query ">
<Layout>
<div className="" />
<Sider
collapsed={this.state.collapsed}
style={{backgroundColor:'#FFFFFF'}}
className=""
onCollapse={this.onCollapse}
openKeys={this.state.openKeys}--->根據(jù)this.state.openKeys的值去對應SubMenu的key值 從而展開此列表。
>
<Menu theme="" mode={this.state.mode}
defaultSelectedKeys={['6']}
openKeys={this.state.openKeys}
>
<SubMenu
key="全國"
title={<span><Icon type="user" /><span className="nav-text">全國</span></span>}
onTitleClick={this.getProvinceList}
>
{
this.state.provinceList.map((items,i)=>
<SubMenu
key={items.id}
title={<span ><Icon type="team" /><span className="nav-text">{items.name}</span></span>}
onTitleClick={this.getCity.bind(this,items.id,items.name,0)}--->onTitleClick---》點擊觸發(fā)回調(diào)函數(shù)
>
{city}
</SubMenu>
)
}
</SubMenu>
</Menu>
</Sider>
)
getProvinceList=()=>{
const result=fetch('/web/chargeTrend/getChargePrinceList.htm'
,{method:'GET',
credentials:'include',
}).then((res)=>{
return res.json();
}).then((data)=>{
//var ds=eval('('+data+')');
console.log('ds',data);
if(data.length>0)
{
if(this.state.openKeys[0]==="全國")
{
this.setState({
provinceList: data,
openKeys:[],
},()=>{
console.log('privince',this.state.provinceList);
})
}else{
var arrs=["全國"];
this.setState({
provinceList: data,
openKeys:arrs,
},()=>{
console.log('privince',this.state.provinceList);
})
}
}
});
}
getCity=(parentid,name)=>{
var arr=this.state.openKeys;
const result=fetch('/web/chargeTrend/getChargeCityList.htm?parentid='+parentid,
{method:'GET',
credentials:'include',
}).then((res)=>{
return res.json();
}).then((data)=>{
console.log('city',data);
if(data.length>0)
{
if(parentid===this.state.openKeys[1])
{
var arrs=["全國"];
this.setState({
cityList:data,
adCode:parentid,
sRange:name,
openKeys:arrs,
},()=>{
console.log('cityList',this.state.cityList);
console.log('city1',this.state.openKeys);
});
}else{
var arrs=["全國"];
arrs.push(parentid);
this.setState({
cityList:data,
adCode:parentid,
sRange:name,
openKeys:arrs,
},()=>{
console.log('cityList',this.state.cityList);
console.log('city1',this.state.openKeys);
});
}
}
});
}
getCountryList=(parentid,name)=>{
var arr=this.state.openKeys;
const result=fetch('/web/chargeTrend/getCountyList.htm?parentid='+parentid,
{method:'GET',
credentials:'include',
}).then((res)=>{
return res.json();
}).then((data)=>{
console.log('country',data);
if(data.length>0)
{
if(this.state.openKeys.length>=3)
{
if(parentid===this.state.openKeys[2])
{
var arrs=["全國"];
arrs.push(arr[1]);
this.setState({
countryList:data,
adCode:parentid,
sRange:name,
openKeys:arrs,
},()=>{
console.log('Country1',this.state.openKeys)
});
}else{
var arrs=["全國"];
arrs.push(arr[1]);
arrs.push(parentid);
this.setState({
countryList:data,
adCode:parentid,
sRange:name,
openKeys:arrs,
},()=>{
console.log('Country2',this.state.openKeys)
});
}
}else{
arr.push(parentid);
this.setState({
countryList:data,
adCode:parentid,
sRange:name,
openKeys:arr,
},()=>{
console.log('Country3',this.state.openKeys)
});
}
}
});
}
}
以上這篇antd多選下拉框一行展示的實現(xiàn)方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue.js組件使用props傳遞數(shù)據(jù)的方法
這篇文章主要為大家詳細介紹了Vue.js組件使用props傳遞數(shù)據(jù)的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10
利用vite創(chuàng)建vue3項目的全過程及一個小BUG詳解
Vite作為一個構(gòu)建工具,提供了一種快速的方法來構(gòu)建Vue應用,而Vue3?則是一個前端框架,提供了強大的功能來構(gòu)建和管理前端項目,下面這篇文章主要給大家介紹了關(guān)于利用vite創(chuàng)建vue3項目的全過程及一個小BUG的相關(guān)資料,需要的朋友可以參考下2023-04-04
Vue Echarts渲染數(shù)據(jù)導致殘留臟數(shù)據(jù)的問題處理
這篇文章主要介紹了Vue Echarts渲染數(shù)據(jù)導致殘留臟數(shù)據(jù)的問題處理,文中通過代碼示例給大家講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-08-08
解決vue中無法動態(tài)修改jqgrid組件 url地址的問題
下面小編就為大家分享一篇解決vue中無法動態(tài)修改jqgrid組件 url地址的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03

