0

I have a tabset that is rendering article records. Does someone have a lightning example how I would create the index to set the tab title and the order?

HTML
<template>
<lightning-card title="ApexWireMethodToProperty">
     <lightning-tabset variant="scoped">
         <template for:each={articles.data} for:item="article" for:index="index">
         <lightning-tab label={article.Title} key={article.Id}>
        <lightning-record-form key={article.Id} record-id={article.Id} object-api-name="Knowledge__kav"
             layout-type="Full" mode="view">
        </lightning-record-form>
    </lightning-tab></template></lightning-tabset>
</lightning-card>

JS

[import { LightningElement, api, track, wire } from 'lwc';
import fetchArticles from '@salesforce/apex/SampleAuraController.fetchArticles';
//import {getRecord} from 'lightning/uiRecordApi';
export default class ArticleList extends LightningElement {
     @track articleList;      
    @api articleList = \[ { title: "Group 1 article" , index : 1} ,
    { title: "How to Clean and Maintain Your Solar Panel" , index : 2} , 
    { title: "Menu 2 articles" , index :3} ,
    {title: "Group 1 article 2", index :4}\];   
    @api recordId;
    @api objectApiName;     
    @wire(fetchArticles) wiredArticles({data,error}){
        if(data){
            this.articleList=data;
            this.articleList = \["Group 1 article", "Group 1 article 2", "Menu 2 articles","How to Clean and Maintain Your Solar Panel"\];//controls order or articles
            this.articleList = this.articleList.map((title, index) => {
        return { title, sno: index + 1 };
    });
            console.log(data);
             } else if (error) {
                 console.log(error);
             }   }}

Apex class

public class SampleAuraController {
     @AuraEnabled(cacheable=true)
    public static List < Knowledge__kav > fetchArticles() {
       return
     //  [SELECT Id, Title,ArticleNumber,Article_body__c,PublishStatus  FROM Knowledge__kav where Title=:Title and PublishStatus='Online'];
   [SELECT Id, Title,ArticleNumber,Article_body__c,PublishStatus  FROM Knowledge__kav where PublishStatus='Online'];
    }
}
6
  • You are changing the value of articleList after you set it from data. What does it look like after your .map()? It is probably not showing anything because after map your parameter is "title" but in your template for:each your key is "Title"
    – CyberJus
    Sep 28, 2021 at 23:36
  • Thank you, changing to title now brings back the article title but still not the record view form format. In the console I'm just seeing: record id is required but is currently undefined or null. The tabset isn't as important as just populating the record view form because I can manually make tabs so if it helps to remove that from the mix that's ok. I'll be trying that today. Sep 29, 2021 at 14:20
  • You are referencing an Id property for your record-id, but you have overwritten your articleList variable without an Id property.
    – CyberJus
    Sep 29, 2021 at 17:17
  • Overwriting isn't my intention, I'm new to this so if there's a better way...the goal here is to grab all the articles, group them into defined order of tabs where each tab shows the article record. There will be 5 groups each having 2-10 articles each. Are you saying that my @api articleList is not actually using what is fetched by the @wire? Sep 29, 2021 at 18:34
  • The wire method is probably returning correctly. You then have 3 lines that start with "this.articleList =" Each one of those is setting the articleList to something different, and you are left with the last one.
    – CyberJus
    Sep 30, 2021 at 2:05

0

You must log in to answer this question.