How do you make a cell/dataitem fully dirty?

1 Answer 935 Views
Grid
Richard
Top achievements
Rank 1
Richard asked on 27 Aug 2021, 02:33 PM | edited on 27 Aug 2021, 02:33 PM

Hello everyone

I have the following bound column set up:

  columns.Bound(site => site.SiteHasBin).ClientTemplate("#=dirtyField(data,'SiteHasBin')# #:SiteHasBin#").ClientHeaderTemplate("<a class='k-link'>Bin Present? - Select all</><input style='margin-left:10px;' id='checkAll' type='checkbox' onclick='checkAll(this)' />").Sortable(false);
   


With the associated js:

function dirtyField(data, fieldName) {
    if (data.dirty && data.dirtyFields[fieldName]) {
        return "<span class='k-dirty'></span>"
    }
    else {
        return "";
    }
}


function checkAll(input) {
    var grid = $("#gridBins").data("kendoGrid");
    var items = grid.items();
    items.each(function () {
        var dataItem = grid.dataItem(this);
          if (dataItem.SiteHasBin != input.checked) {
              dataItem.SiteHasBin = input.checked;
        }
        dataItem.dirty = true;
    })
    grid.saveChanges();
    grid.dataSource.sync();
}

However, I cannot for the life of me get the cell into it's dirty state properly which would then allow me to use the Save functionality.

As you can see from the above. It updates the values accordingly but unlike single clicking, doesn't dirty the cell / data item.

Could someone point me in the right direction please?

Rich

 

 

1 Answer, 1 is accepted

Sort by
0
Tsvetomir
Telerik team
answered on 30 Aug 2021, 11:11 AM | edited on 02 Sep 2021, 12:48 PM

Hi, Richard,

I have investigated the provided code snippets and I have noticed that at the end of the checkAll() function, the saveChanges() and the sync() methods are called. What will they do is that they will save the current state of the grid. This can be verified by the loading spinner shown after clicking on the checkbox.

Therefore, the new values of the fields will be true and since they are already saved, they will not be dirty any more. 

If you would like to keep the dirty state of the fields and show the indicator, you should call the refresh method of the grid instead of saveChanges():

grid.refresh();

Also, make sure that you have added the respective field to the dirtyFields collection of the data item:

function checkAll(input) {
    var grid = $("#gridBins").data("kendoGrid");
    var items = grid.items();
    items.each(function () {
        var dataItem = grid.dataItem(this);
          if (dataItem.SiteHasBin != input.checked) {
              dataItem.SiteHasBin = input.checked;
        }
        dataItem.dirty = true;
        dataItem.dirtyFields = {SiteHasBin: true}
    })
    grid.saveChanges();
    grid.dataSource.sync();
}

This would cause the grid to rerender its template and will repaint the cells - hence, the dirty indicator should be shown accordingly.

 

Kind regards,
Tsvetomir
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Richard
Top achievements
Rank 1
commented on 31 Aug 2021, 08:18 AM | edited

Hello Tsvetomir

Whilst I am no longer hitting the async _Update ActionResult  in the controller (yay!), the dirty css class isn't being added to the cell:

 

Here is my code now


function checkAll(input) {
    var grid = $("#gridBins").data("kendoGrid");
        var items = grid.items();
        items.each(function () {
            var dataItem = grid.dataItem(this);
            if (dataItem.SiteHasBin != input.checked) {
                dataItem.SiteHasBin = input.checked;
            }
            dataItem.dirty = true;
        })
        grid.refresh();
}

I'm just wondering if it's because it's an async call it's not repainting the cells ?

Further to this, is there an event I can tap into in order to reset the "Select all" checkbox when Cancel Changes is clicked?

 

Tsvetomir
Telerik team
commented on 02 Sep 2021, 12:50 PM

Hi Richard, I have modified my answer so that it resembles the last part needed to mark a field as dirty. 
Tags
Grid
Asked by
Richard
Top achievements
Rank 1
Answers by
Tsvetomir
Telerik team
Share this question
or