ionic4-標題之顯示開頭和結尾,中間省略

前情

        因爲要將後端查詢回來的標題名稱顯示到頁面上,但是有得標題名稱十分的長並且是系列課程的話,顯示在頁面中就會無法區分,所以特地將名稱的開頭與結尾顯示出來,中間內容省略。

代碼

const dataUrl = 'brainstorming-web/brainStorming/queryListByStatesClassId/'
            + this.states + '/' + this.classId;
        this.http.get(dataUrl).subscribe(
            res => {
                if (res.json().code === '0000') {
                    if (res.json().data.length > 0) {
                        // 查詢回來的內容
                        this.test = res.json().data;
                        for (let i = 0; i < this.test.length; i++) {
                            // 將每條數據中的名稱進行截取
                            let name = this.test[i].theme;
                            let title = '';
                            const nameLength = name.length;
                            console.log('看一下name長度', nameLength);
                            if (nameLength > 4) {
                                let titleLength = nameLength - 4;
                                while (titleLength > 0) {
                                    title += '.';
                                    titleLength--;
                                }

                                name = name.substr(0, 4) + title + name.substr(name.length - 2, name.length);
                            }
                            console.log('看一下效果-----', name);
                        }
                    }
                }
            }
        );

顯示結果

學習古詩……列一

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章