How do i get the value for a grid view in javascript?

I can get values using data.field_XX for a detail view. and can get form values as well.

but i have a scene with a grid view on it… and i want to disable the submit button if the grid is empty.
if i use data.field_XX i keep getting ‘undefined’ and i cant figure out how to get the value of a grid item.

trying field_XX_raw didnt help, nor did data.field_XX[0]

any tips?

Thank you

Hi Irfan

Can you share a screenshot of the page and indicate the submit you want to disable and the grid and field you want the value from?

From your description I can’t quite visualise what you are trying to do.

Craig

This may answer your question:

for (const record of records){
     const value = record.field_123;
     Your code......
}

If this doesn’t help if you give me more info I can help.

Craig

Hi here is a screen shot.

View_100 is a grid and thats the challenge im having… trying to figure out how to get the data from that grid.
View_101 is a form and I have it worked out to not submit if 101 field58 or field54 are empty.

running into trouble with view 100 though. cant figure out the syntax to check if view_100 field_58 record[0] exist or empty.

thank you so much for the help as always!

Hi @Irfan

Is there ever more than one line item in grid 100? This will affect how we retrieve the data. If more than one are we looking for them all to empty?

Thanks
Amanda
(I’m working with @CSWinnall)

Yes, there can be several line items in view_100. I only care that its not completely empty.

view100 starts empty for every new case. the workflow is that the team should hit the “add line item” button after entering data, which then saves the information from view101 into view100. once view100 has SOMETHING in it, then its safe to submit the form.

thanks again!

Ok, I think we have enough info to find a solution. Could you please post any existing code you have running on those views?

Thanks
Amanda

I have this to block the submit action and alert the user that they need to click the add button. this solves the view101 portion. when they click addlineitem it saves it to the view100 grid.

$(document).on(‘knack-view-render.view_101’, function(event, view, data) {

$(“#view_160 .kn-button”).on(“click”, function() {

if ($('#view_101-field_58').val() != "") { 
		      console.log($('#view_101-field_58').val())
        console.log($('#view_100-field_58').val());
        alert ("2 Please Click \"Add Line Item\" to save the last entry.  Or reset the dropdowns to \"Select...\" before saving.")
        return false
	}
		
if ($('#view_101-field_54').val() != "") {
        alert ("3 Please Click \"Add Line Item\" to save the last entry.  Or reset the dropdowns to \"Select...\" before saving.");
        console.log($('#view_101-field_54').val())
        console.log($('#view_100-field_58').val())
		return false;
	}		

// }

})
})

// Check if field_58 in view_101 is not empty
if ($(‘#view_101-field_58’).val() !== “”) {
console.log("Field_58 View_101: " + $(‘#view_101-field_58’).val());
console.log("Field_58 View_100: " + $(‘#view_100-field_58’).val());
alert(“Please click "Add Line Item" to save the last entry, or reset the dropdowns to "Select…" before saving.”);
return false;
}

// Check if field_54 in view_101 is not empty
if ($(‘#view_101-field_54’).val() !== “”) {
alert(“Please click "Add Line Item" to save the last entry, or reset the dropdowns to "Select…" before saving.”);
console.log("Field_54 View_101: " + $(‘#view_101-field_54’).val());
console.log("Field_58 View_100: " + $(‘#view_100-field_58’).val());
return false;
}

here is the improved version
regards : wordcombine

Hi Irfan

A table that has no data has this class on the tr: kn-tr-nodata so the below code should work for what you need.

$(document).on('knack-view-render.view_101', function(event, view, data) {

        $('#view_160 .kn-button').on('click', function() {
        
            if ($('#view_101-field_58').val() != "") { 
                console.log($('#view_101-field_58').val());
                console.log($('#view_100-field_58').val());
                alert ("2 Please Click \"Add Line Item\" to save the last entry.  Or reset the dropdowns to \"Select...\" before saving.");
                return false;
            }
                    
            if ($('#view_101-field_54').val() != "") {
                alert ("3 Please Click \"Add Line Item\" to save the last entry.  Or reset the dropdowns to \"Select...\" before saving.");
                console.log($('#view_101-field_54').val());
                console.log($('#view_100-field_58').val());
                return false;
            }

            if ($('#view_100 tbody tr.kn-tr-nodata').length) {
                alert('No data in table');
                return false;
            }
        });
    });

If this isn’t quite right please let us know and we will try again.

Craig & Amanda

It worked! thanks! sorry for the delay I didnt have a chance to check it out until now. thank you so much for the help that will save a lot of aggravation

1 Like