Jump to content

Inline edit only in certain circumstances


Recommended Posts

Hi there,

I have a tabular report with inline edit and delete enabled.  This report shows a list of comments made by different users.  I wish to only show the edit / delete options for the user who has made the comment.   I have tried this:

<script>
var user = "[@authfield:User_info_User_ID]";
var author = "[@field:Card_comments_and_updates_Author_ID]";

if (user === author) {
document.getElementByClassName("cbResultSetActionCell").style.display = "block";
}
if (user !== author) {
document.getElementByClassName("cbResultSetActionCell").style.display = "none";
}

</script>

but it's not working.  I think it's something to do with the fact I've used "getElementByClassName", but there is no ID assigned to this field, so not sure how to reference it.  I've added an html field to write YEP if the user=author and NOPE if not, just to check this is working OK, which it is in the attached screenshot, so I just want to apply this same logic to show or hide the edit / delete (which is inside this row:

<td class="cbResultSetActionCell cbResultSetData">

The only other way I can think of to do this is to completely hide the edit / delete section, and then add a new one in, in the html field where I've currently got the YEP/NOPE. But I don't know what custom link needs to go in there for edit and delete, or if this can even be done in the same way you can add a custom save / delete button in a datapage.

Any help would be much appreciated!

Many thanks

Nikki 

Caspio-forum.png

Link to comment
Share on other sites

  • 2 months later...

Hi @NikkiC,

Put the following code below in the Footer element:

<script>
document.addEventListener('DataPageReady', function(event) {

    function hideControlsForPerson(nameColumnIndex, controlsColumnIndex) {
        var table = typeof document.getElementsByClassName("cbResultSetTable" + event.detail.uniqueSuffix)[0] == "undefined" ?
            document.getElementsByClassName("cbResultSetTable")[0] : document.getElementsByClassName("cbResultSetTable" + event.detail.uniqueSuffix)[0];

        for (var i = 1, row; row = table.rows[i]; i++) {
            if (row.cells[nameColumnIndex].textContent !== '[@authfield:Name]') {
                row.cells[controlsColumnIndex].style = "display:none;"
            }
        }
    }

    var nameColumnIndex = 1; //position of the column with names
    var controlsColumnIndex = 6; //position of the column with inline edite/delete 

    hideControlsForPerson(nameColumnIndex, controlsColumnIndex);

});
</script>

This code will hide cells for all the users except the logged in one.

Hope it helps.
 

Link to comment
Share on other sites

  • 1 year later...

Hi There,

Would it be possible to update this to automatically find the Index for "nameColumnIndex"  and "controlsColumnIndex" based on the name of those fields by counting until some condition is met? 

This would help when we add change the column positions later on so as not to break the code.

Thanks in advance 

Link to comment
Share on other sites

Just leaving this here ...  

 

<script>
document.addEventListener('DataPageReady', function(event) {

    function hideControlsForPerson() {
        var table = typeof document.getElementsByClassName("cbResultSetTable" + event.detail.uniqueSuffix)[0] == "undefined" ?
            document.getElementsByClassName("cbResultSetTable")[0] : document.getElementsByClassName("cbResultSetTable" + event.detail.uniqueSuffix)[0];

				// Find the index of the last column, which happens to be the inline edit/delete 
				var total_num_columns = table.rows[0].cells.length;
			    var controlsColumnIndex = total_num_columns - 1; //position of the column with inline edit/delete 


				// Find the index position of the column by searching the column's label
				row = table.rows[0];
				var i;
				var KeyColumnIndex;
				for (i = 0; i < total_num_columns; i++) {
					if (row.cells[i].textContent == 'Label_of_Target_Field') {
						KeyColumnIndex = i;
						i = total_num_columns;  // this is essentially to exit the loop
					}
				}


				// Iterating through rows and hiding Content 
				for (var i = 1, row; row = table.rows[i]; i++) {
					if (row.cells.length > KeyColumnIndex && row.cells[KeyColumnIndex].textContent !== '[@authfield:Name]') {
						row.cells[controlsColumnIndex].style = "display:none;"
					}
				}

    }


    hideControlsForPerson( );

});
</script>

 

Note:  " row.cells.length > KeyColumnIndex "  was added to the if statement to protect against an uncaught TypeError that happens if using a date rollup or other field grouping. 

 

 

Link to comment
Share on other sites

  • 3 years later...

Hi! If you would like to apply this article: https://howto.caspio.com/tech-tips-and-articles/disabling-inline-edit-option-in-reports-based-on-a-condition/ when Data Grouping is enabled.

You can change the code from 

const calculatedFieldPosition = 1

to

const calculatedFieldPosition = 2

 

1700735

 

Make sure that the Calculated Field (formula/condition) is in 2nd place of the DataPage Elements and the first is the field for Grouping.

Sample Result:

image.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...