Kendo Grid点击第六列复选框设dirty为true时遇未定义错误求助
Hey there! Let's get that Kendo Grid issue sorted out for you. The error you're seeing happens because your code is trying to use a grid variable that hasn't been properly initialized to reference your actual Kendo Grid instance. Here's how to fix it:
Step 1: Get the correct Grid instance
First, you need to grab the active Kendo Grid object. If you initialized your Grid with an ID (like $("#grid").kendoGrid(...)), you can fetch the instance using data("kendoGrid"):
let grid = $("#grid").data("kendoGrid");
Step 2: Reliably get the row containing the checkbox
Instead of relying on grid.select() (which only works if the row is explicitly selected), you can traverse up from the clicked checkbox to find its parent row. This is far more reliable for checkbox interactions:
let row = $(this).closest("tr");
Step 3: Updated working code
Putting it all together, here's your revised code. Also, note that using a class instead of an ID for your checkbox is better (since IDs must be unique in the DOM, and your Grid will render one checkbox per row):
First, update your column template to use a class instead of an ID:
<input type="checkbox" class="sixthColumnCheck" />
Then update your event handler:
$(document).on('change', '.sixthColumnCheck', function() { // Fetch the Grid instance let grid = $("#grid").data("kendoGrid"); // Get the row associated with the clicked checkbox let row = $(this).closest("tr"); // Get the data item for that row let gridrow = grid.dataItem(row); // Mark the row as dirty gridrow.dirty = true; })
Why this works:
- We're properly retrieving the Kendo Grid instance, so the
gridvariable is no longer undefined. - Using
closest("tr")ensures we always get the right row, even if the user didn't select the row before clicking the checkbox. - Switching to a class for the checkbox avoids duplicate ID issues in the DOM, which can cause unexpected behavior down the line.
内容的提问来源于stack exchange,提问作者user9638369

