Question T12295
Visible to All Users

Getting index of row in dynamic matrix

created 2 years ago

Hello,

In my survey, I have a dynamic matrix named SLOs that contains three columns; one of them, named SLO, I want to be populated with a placeholder containing the text SLO {current index}. So for example if I had three rows in my dynamic matrix all three would have a placeholder corresponding to their position in the matrix:

SLO 1
SLO 2
SLO 3

Is this possible?

Thank you.

Answers approved by surveyjs Support

created 2 years ago

Hello,
Yes, it is possible by using survey.onMatrixCellCreated event.
You can make it work for your survey only:

JavaScript
survey.onMatrixCellCreated.add((sender, options) => {     if(options.column.name === "SLO") {         options.cellQuestion.placeHolder = options.column.title + " " + (options.question.visibleRows.indexOf(options.row) + 1).toString();     } });

You can go further and make it generic by introducing the following property into column:

JavaScript
Survey.Serializer.addProperty("matrixdropdowncolumn", {     name"generatePlaceholder"choices: ["none""increment"],     category"general",     visibleIf(obj) => {          if(!obj || !obj.colOwner) return false;         return obj.cellType === "text" || obj.cellType === "default" && obj.colOwner.cellType === "text";} });

And the use it as the following:

JavaScript
survey.onMatrixCellCreated.add((sender, options) => {     if(options.column.generatePlaceholder === "increment") {         options.cellQuestion.placeHolder = options.column.title + " " + (options.question.visibleRows.indexOf(options.row) + 1).toString();     } });

Here is the working example.

Thank you,
Andrew
SurveyJS Team