post by ywillener on May 22, 2024

Hello,

I have added a datagrid button (setDatagridButton) and I need to pass the ID column currently displayed to the linked page. Is there a way to do it with the Grocery CRUD API or it should be implemented in javascript ?

Thanks
Yannick

post by johnny on May 24, 2024

post by ywillener on May 28, 2024

post by johnny on Jun 2, 2024

Hello @ywillener are you referring to the visible columns? If so then this is stored in the localStorage. In order to take the local storage value you will need to add first the setUniqueId for the CRUD. For example:

// You need to have only ONE unique id per CRUD operation or 
// else your different pages will be confused
$crud->setUniqueId("customers");

If your uniqueId is “customers” then you can get your localStorage like this in JavaScript (I’ve created a function to understand it better):

function getVisibleColumns(storageKey) {
    // Retrieve the stored data from local storage
    let persistedData = localStorage.getItem(storageKey);

    // Check if the stored data exists and is not empty
    if (persistedData) {
        // Parse the retrieved JSON string
        let parsedData = JSON.parse(persistedData);
        
        // Check if the datagrid property exists and is not empty
        if (parsedData && parsedData.datagrid) {
            // Parse the datagrid property
            let datagridContent = JSON.parse(parsedData.datagrid);
            
            // Check if the visibleColumns property exists and is not empty
            if (datagridContent && datagridContent.visibleColumns) {
                // Return the visible columns
                return datagridContent.visibleColumns;
            }
        }
    }
    
    // Return null if any of the checks fail
    return null;
}

// Example usage
let visibleColumns = getVisibleColumns("persist:customers");
console.log(visibleColumns);
1 / 4
May 2024