Hi Team, Is there any way to get the count of colu...
# gooddata-ui
j
Hi Team, Is there any way to get the count of columns for insight using insightId ?
i
Hi, I believe that you can achieve this via custom execution. It will give you information about the result: https://sdk.gooddata.com/gooddata-ui/docs/custom_execution.html
Firstly, you’d need to get the Insight execution - some starting code:
Copy code
const insight = await backend.workspace(WORKSPACE).insights().getInsight(idRef("insight-id"))
    const execution = backend.workspace(WORKSPACE).execution().forInsight(insight);
    const result = await execution.execute();
    const colsNum = result.dimensions[1].headers.length; // not sure about the [1], maybe it is [0] would need to test it
👌 1
j
Hi @Ivana Gasparekova thanks for the response, I will try and let you know
i
Great, fingers crossed for you!
j
Hi @Ivana Gasparekova I tried a shared code by you but it is not giving correct number of columns in my case, I have insight which has 3 columns 1. Region 2. Property type summary 3. Net amount As shown is attached screenshots but
headers[0]
has length 1 and
headers[1]
has length 2 and both has region column
@Matyáš Kandl @Peter Plochan @Martin Burian @Jan Rehanek can anyone help me on my query
d
Hi @Jitender Singh I am not sure a general solution is feasible. Especially insights that have some attribute in the Columns bucket derive their columns number from the actual attribute elements being used (see the screenshot for an example), so in order to get the number of columns you would actually need to know the shape of the data, then actually execute the execution and then somehow resolve how the result headers would look like in the table. In that case it is almost easier to actually display the table and check its resultign HTML 😕 Or maybe it would help if you could share your particular usecase for this, please?
j
Hi @Dan Homola, my actual use-case is that I have insightID and I have to design the layout of insight according to # of columns. 1. # column count >= 6 then insight will take 100% width 2. # column count < 6 then insight will take 50% width. So as per my use-case I have to identify # columns even before rendering using insightId it since rendering will be based on logic of # of columns
d
Hm, in that case maybe just counting the numer of items in the buckets might be good enough. Measures and Rows map 1:1 to number of columns, Colums are a bit more complicated, but it could be good enough (you could also have some logic that would say if there is anything in the Columns, you use the bigger layout). This is how you can count the total count of items:
Copy code
import { bucketsItems, idRef, insightBuckets } from "@gooddata/sdk-model";

const insight = await backend.workspace(WORKSPACE).insights().getInsight(idRef("insight-id"))
const approxCols = bucketsItems(insightBuckets(insight)).length
j
Thanks @Dan Homola, @Ivana Gasparekova for the solution 🙂
🙌 1