Hide or show blocks based on a record's attributes

Hi @swshrey, happy new year and sorry for the time it took me to answer.

I think what you’re trying to do is definitely possible, you could use custom code in the header of your page to hide the blocks you want with a querySelector, and then use another snippet in the footer to toggle the visibility of the specific one you want to display back on.

For that last part, I would advise to have a look at Artur’s solution here, since mine above has been impacted by some changes in the meantime and might not be fully functional anymore (especially the “data-mappedto” attribute used in the selector).

Let me know if you manage to work your way around that, if not happy to help !

@Julien_Berawen no probs at all and thanks v much for the reference. I think Artur’s solution MIGHT resolve a lot of user management issues that we currently have - will msg him and cc you - and if I need any help will get back to you, with huge thanks in advance in any event! I very much doubt if I can ever return the favour, unless you ever have to deal with data models relating to artworks, in which case I’m your man :slight_smile: :slight_smile: - if my tech colleagues ever need a softr coding resource, are you available on an hourly basis? If so, do let me know (didn’t see your name in the ‘Softr expert’ list…

1 Like

hey folks,

as there was a question on hiding and un-hiding it goes like this typically

Sure you can use

myElement.style.display = 'none'; // to hide

myElement.style.display = 'block'; // to unhide

Please keep in mind this is not a security measure if the data needs to be not shown at all user visibility rules or conditional filters.

1 Like

Mny tks @artur ! V useful - and noted re visibility/security.

Hello, all, I’m not a coder but this seems not to be working for me when I’m using a checkbox field and seeking a value of “true” so I’m assuming I’m missing something easy. If I just comment out the ‘if’ statement, it works.

The “markfinalbutton” is an action button in a form with hidden fields. The “marked_final” checkbox is displayed in a list details element.

document.addEventListener("DOMContentLoaded", function() {    
    var waitForData = setInterval(function () {
        if (typeof $ != 'undefined') {
            if (document.querySelectorAll('div[data-mappedto="marked_final"][data-value="true" i]').length > 0) {
                
                var myForm = document.getElementById('markfinalbutton');

                myForm.style.display = 'none';
                
                clearInterval(waitForData);
            }
        }
    }, 100);
});

@mb12142329 I think the data_mappedto attribute cannot be used anymore after some changes in the code structure. You’ll probably be better off using that solution.

Thank you but it’s still not working unfortunately. Here’s my version. Does it matter that it’s a linked field in Airtable? Should I be using a value other than “true”?

document.addEventListener(“DOMContentLoaded”, function() {
var waitForData = setInterval(function () {
if (typeof $ != ‘undefined’) {
const recordId = getUrlParam(‘recordId’);
if (window.records && window.records[recordId] && >
window.records[recordId].record.fields[‘markedfinal’] === ‘true’) {
var form1 = document.getElementById(‘markfinalbutton’);
form1.style.display = ‘none’;

                clearInterval(waitForData);
            }
        }
    }, 100);

    function getUrlParam(name) {
        const url = new URL(window.location.href);
        let param;
        for(var key of url.searchParams.keys()) {
            if(key.toLowerCase() === name.toLowerCase()) {
            param = url.searchParams.get(name);
            break;
            }
        }
    
        if(!param && name.toLowerCase() === 'recordid') {
            param = getRecordIdFromPath();
        }
        return param;
    }
    
    function getRecordIdFromPath() {
        let pathName = window.location.pathname;
        if (pathName.indexOf('/r/rec') !== -1) {
            pathName = pathName.substr(pathName.indexOf('/r/rec') + 3);
            if (pathName.indexOf("/") !== -1) {
            pathName = pathName(0, pathName.indexOf('/'))
            }
            return pathName;
        }
        return undefined;
    }
});

@mb12142329 Is there any error or warning in the browser console ? If so can you paste them here ?

  • policy-edits:9 The key “target-densitydpi” is not supported.
  • micromodal.min.js:1 MicroModal: :exclamation:Please specify at least one ‘micromodal-trigger’ data attribute.
  • (anonymous) @ micromodal.min.js:1
  • micromodal.min.js:1 Example:
  • (anonymous) @ micromodal.min.js:1
  • policy-edits:1 Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received
  • DevTools failed to load source map: Could not load content for https://assets.softr-files.com/libs/iframe-resizer/4.2.11/iframeResizer.contentWindow.map: HTTP error: status code 403, net::ERR_HTTP_RESPONSE_CODE_FAILURE

Can you try with this slightly different version ?

document.addEventListener('DOMContentLoaded', function() {
  var waitForData = setInterval(function () {
    if (typeof $ != 'undefined') {
      const recordId = getUrlParam('recordId');
      if (window.records && window.records[recordId] && window.records[recordId].record && window.records[recordId].record.fields && window.records[recordId].record.fields['markedfinal'] === 'true') {
        var form1 = document.getElementById('markfinalbutton');
        form1.style.display = 'none';
        clearInterval(waitForData);
        }
      }
    }, 100);

    function getUrlParam(name) {
        const url = new URL(window.location.href);
        let param;
        for(var key of url.searchParams.keys()) {
            if(key.toLowerCase() === name.toLowerCase()) {
            param = url.searchParams.get(name);
            break;
            }
        }
    
        if(!param && name.toLowerCase() === 'recordid') {
            param = getRecordIdFromPath();
        }
        return param;
    }
    
    function getRecordIdFromPath() {
        let pathName = window.location.pathname;
        if (pathName.indexOf('/r/rec') !== -1) {
            pathName = pathName.substr(pathName.indexOf('/r/rec') + 3);
            if (pathName.indexOf("/") !== -1) {
            pathName = pathName(0, pathName.indexOf('/'))
            }
            return pathName;
        }
        return undefined;
    }
});

Still the same unfortunately. To be clear, I’m putting this code in the footer. I’m not sure what else to try unless I’m just messing something up that’s very basic.

Yes, the footer should be the right place. Last thing I can think of : can you try individually testing each of the conditions using the browser console and let us know what comes out of each test ?
That would be successively :

  • window.records
  • window.records[recordId]
  • window.records[recordId].record
  • window.records[recordId].record.fields
  • window.records[recordId].record.fields['markedfinal']

If that doesn’t help, I’ll let someone more talented or more awake than I am try and figure it out. :slight_smile:

1 Like

Thanks, going to the console proved helpful because I realized (to my question earlier about checkboxes) that the value should be “checked” not “true”. It looks like there were two issues though because changing the value only worked when I simplified the IF statement and hardcoded the recordId. The other two functions seem unable to get the recordID for me. If I then go with the commented line below, it does not work. If I then put “window.records[recordId]” into the console, I get “Uncaught ReferenceError: recordId is not defined at :1:16”

document.addEventListener(‘DOMContentLoaded’, function() {
var waitForData = setInterval(function () {
if (typeof $ != ‘undefined’) {
const recordId = window.records[‘reclEC7KDP8Jq67PM’].record.fields.MarkedFinal;
// const myrecordId = getUrlParam(‘recordId’);
if (recordId === ‘checked’) {
var form1 = document.getElementById(‘markfinalbutton’);
form1.style.display = ‘none’;
clearInterval(waitForData);
}
}
}, 100);

function getUrlParam(name) {
const url = new URL(window.location.href);
let param;
for(var key of url.searchParams.keys()) {
if(key.toLowerCase() === name.toLowerCase()) {
param = url.searchParams.get(name);
break;
}
}

        if(!param && name.toLowerCase() === 'recordid') {
            param = getRecordIdFromPath();
        }
        return param;
    }
    
    function getRecordIdFromPath() {
        let pathName = window.location.pathname;
        if (pathName.indexOf('/r/rec') !== -1) {
            pathName = pathName.substr(pathName.indexOf('/r/rec') + 3);
            if (pathName.indexOf("/") !== -1) {
            pathName = pathName(0, pathName.indexOf('/'))
            }
            return pathName;
        }
        return undefined;
    }
});

This should work : window.records[Object.keys(records)[0]].recordId
(instead of window.records[recordId])

2 Likes

That’s it! Thank you. Here’s what’s working if others want to replicate.

document.addEventListener(‘DOMContentLoaded’, function() {
var waitForData = setInterval(function () {
if (typeof $ != ‘undefined’) {
const recordId = window.records[Object.keys(records)[0]].record.fields.MarkedFinal;
if (recordId === ‘checked’) {
var form1 = document.getElementById(‘markfinalbutton’);
form1.style.display = ‘none’;
clearInterval(waitForData);
}
}
}, 100);
function getUrlParam(name) {
const url = new URL(window.location.href);
let param;
for(var key of url.searchParams.keys()) {
if(key.toLowerCase() === name.toLowerCase()) {
param = url.searchParams.get(name);
break;
}
}
if(!param && name.toLowerCase() === ‘recordid’) {
param = getRecordIdFromPath();
}
return param;
}
function getRecordIdFromPath() {
let pathName = window.location.pathname;
if (pathName.indexOf(‘/r/rec’) !== -1) {
pathName = pathName.substr(pathName.indexOf(‘/r/rec’) + 3);
if (pathName.indexOf(“/”) !== -1) {
pathName = pathName(0, pathName.indexOf(‘/’))
}
return pathName;
}
return undefined;
}
});

2 Likes

You can even get rid of the getting the recordID from the URL part, since you’re getting it directly from the JS object.

document.addEventListener("DOMContentLoaded", function() {
  var waitForData = setInterval(function () {
    if (typeof $ != 'undefined') {
      const isFinal = window.records[Object.keys(records)[0]].record.fields.MarkedFinal;
      if (isFinal === 'checked') {
        var buttonToHide = document.getElementById('markfinalbutton');
        buttonToHide.style.display = 'none';
        clearInterval(waitForData);
      }
    }
  }, 100);
});
1 Like

This error message indicates that there was a problem with the communication between the listener and the message channel. The listener sent a response indicating that it was ready to receive a message, but the message channel closed before the listener was able to receive the message.

In most cases the reason is that you call chrome.runtime.sendMessage when the popup is not shown. The popup cannot receive messages if it’s not shown. You can suppress the error by using ‘()=>chrome.runtime.lastError’ instead of ‘function(response){}’ .

Another possible solution would be show the popup in a new window using chrome.windows.create or as a DOM element inside the web page.

if (document.querySelectorAll(‘div[data-mappedto=“event_status”][data-value=“full” i]’).length > 0) {

Am I able to make data-value= a variable such as Logged-in User’s email?

I want it to basically check a field in the record and if the logged-in user’s email in that field, then don’t show it

Hi there!

I’d love to use this but I’m not sure I’m configuring correctly. First, I notice there is “bookingForm” – do I need to edit that parameter? I’m not sure what that is referring to in my set-up.

Also the data field I’m looking at is a roll-up for balance owing across invoices. I’ve tried “$0.00” and just “0” but the block still shows.

Any help would be appreciated!

Cheers,

Nicole

Hi! so I tried to use the code and according to support it doesn’t work anymore. So can you plase share the updated code for it. Thanks.

1 Like