Calculate row values on batch update

7 posts, 0 answers
  1. Pinaki
    Pinaki avatar
    6 posts
    Member since:
    May 2012

    Posted 08 Feb 2013 Link to this post

    Hello all Kendo experts. I'm facing an issue here with MVC Grid batch update. Your help will be appreciated here. So here is my scenario.

    1. I'm using ASP.NET MVC 4 with Kendo UI. I'm using a grid  with batch update.
    2. There are some rows, that the user types in value (numeric), and there are some rows, that are calculated fields, and are not editable.

    Please bear in mind, it's the same column, but rows are editable or not (given the condition). I've alredy acheived at this point.

    (See screenshot attached).

    What I'm looking for is.

    On Save Changes event, calculate the values of the other rows.
    The problem I'm facing is, since on the natch update, these rows were not "Edited" by the user, these values are not refreshes at the controller.
    So, basically what I'm looking for, is for the same type of column, make some rows non-editable, but refresh with calculated values on the Save changes.

    In the screeshot supplied, the rows in blue are my calculated rows.

    Any help please?

  2. Daniel
    Admin
    Daniel avatar
    1455 posts
    Member since:
    Sep 2012

    Posted 12 Feb 2013 Link to this post

    Hi,

    In order for the items to be send to the server, you should set the dirty flag of the modified items to true e.g.

    function saveChanges(e) {
        var item = this.dataSource.view()[0];
        item.NumericValue = 1000;
        item.dirty = true;
    }
    If the items are editable in the model then you could also use the model set method:
    function saveChanges(e) {
        var item = this.dataSource.view()[0];
        item.set("NumericValue", 1000);     
    }
    Kind regards,
    Daniel
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!

  3. Pinaki
    Pinaki avatar
    6 posts
    Member since:
    May 2012

    Posted 16 Feb 2013 Link to this post

    Hello Daniel.

    Thank you for the response and help. It's apprecited. However this does not satisfy what I really need, and there seems to be a flaw.

    Your code selects the first row item.

    1. Here are my scenarios.
      Yes, the fields I want to show calculated are editable in the model. I've only disabled editing in front-end ui by using closeCell() metthod.
    2. In my model, there is a column DATAFIELDCODE for each row. I'm not displaying it in the grid, but is in the model.
      So, instead of selecting a hard-coded row index, I want to find the row with a DATAFIELDCODE = 'XXX".

    Can you help with the jQuery selector, or some other method, where I can get a specific row, given the DATAFIELDCODE value, and then change the column NUMERICVALUE, of that specific row?

    Thank you.

  4. Daniel
    Admin
    Daniel avatar
    1455 posts
    Member since:
    Sep 2012

    Posted 19 Feb 2013 Link to this post

    Hi again,

    In order to find all rows that have a specific value you can iterate over the view array and check the values for each item :

    function saveChanges(e) {
        var view = this.dataSource.view();
        for (var i = 0; i < view.length; i++) {
            if (view[i].DATAFIELDCODE == "XXX") {
                var item = view[i];
                item.MyField = calculatedValue;
                item.dirty = true;
            }
        }              
    }
    Regards,
    Daniel
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!

  5. Pinaki
    Pinaki avatar
    6 posts
    Member since:
    May 2012

    Posted 22 Feb 2013 Link to this post

    Hello Daniel.
    Thank you again. But your code example has some problems.
    Firstly, I hav a lot of  records to check and there might be over 100 records. And since I want to update values of only seelcted rows, whose DataFieldCode, I already know, this seems very inefficient, to itereate through all the rows.

    Is there anyway, I can do a find () or seek() or something of that nature?

    Another problem is that since I'm only using a View, the underlying data is not saved, when  you go to a different page on the gird.

    Please see me code snippet.

    Any help please?

    // ------------------------------------------------------------------------------
    // This method is called on on savechanges for PlayVolume grid to calculate values for calculated rows
    // -------------------------------------------------------------------------------
    function playVolumeSaveChanges(e) {
        var view = this.dataSource.view();
        //variables needed for calculation.
        var playTechPosVal = 0;
        var playAccessibleVal = 0;
        var acreageP90Val = 0.0
        var acreageP50Val = 0.0;
        var acreageMeanVal = 0.0;
        var acreageP10Val = 0.0;
     
         for (var i = 0; i < view.length; i++) {
            //Get TECHPOS value.
            if (view[i].DataFieldCode == "TECHPOS")
                playTechPosVal = view[i].DataFieldValue;
     
            //Get PCTACCESSIBLE value.   
            if (view[i].DataFieldCode == "PCTACCESSIBLE")
                playAccessibleVal = view[i].DataFieldValue;
     
            //Get ACREAGEP90 value.   
            if (view[i].DataFieldCode == "ACREAGEP90")
                acreageP90Val = view[i].DataFieldValue;
     
            //Get ACREAGEP50 value.   
            if (view[i].DataFieldCode == "ACREAGEP50")
                acreageP50Val = view[i].DataFieldValue;
     
            //Get ACREAGEMEAN value.   
            if (view[i].DataFieldCode == "ACREAGEMEAN")
                acreageMeanVal = view[i].DataFieldValue;
     
            //Get ACREAGEP10 value.   
            if (view[i].DataFieldCode == "ACREAGEP10")
                acreageP10Val = view[i].DataFieldValue;
     
            //now assign calculated field values
     
            //set YTFP90 value.   
            if (view[i].DataFieldCode == "YTFP90") {
                view[i].DataFieldValue = playTechPosVal * playAccessibleVal * acreageP90Val;
                view[i].dirty = true;
            }
            //set YTFP50 value.   
            if (view[i].DataFieldCode == "YTFP50") {
                view[i].DataFieldValue = playTechPosVal * playAccessibleVal * acreageP50Val;
                view[i].dirty = true;
            }
            //set YTFMEAN value.   
            if (view[i].DataFieldCode == "YTFMEAN") {
                view[i].DataFieldValue = playTechPosVal * playAccessibleVal * acreageMeanVal;
                view[i].dirty = true;
            }
     
            //set YTFP10 value.   
            if (view[i].DataFieldCode == "YTFP10") {
                view[i].DataFieldValue = playTechPosVal * playAccessibleVal * acreageP10Val;
                view[i].dirty = true;
            }
        }
        //proceed with submit changes

  6. Daniel
    Admin
    Daniel avatar
    1455 posts
    Member since:
    Sep 2012

    Posted 25 Feb 2013 Link to this post

    Hello,

    The dataSource supports searching only by id or uid. Even if there was a method that could be used to search by custom criteria(like jQuery grep), the code will be the same. The data is stored as an array so the only way to find the needed items(except by index) is to iterate over the items in the array.
    I am not sure if I understand correctly the other issue but if all the items in the dataSource needs to be updated then you should use the data method instead.

    Regards,
    Daniel
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!

  7. Sachin
    Sachin avatar
    2 posts
    Member since:
    Jun 2011

    Posted 25 Apr 2013 Link to this post

    Hello Pinaki

    Could you please share your source code ? I am interested in the code that binds batch save event to the controller function

    Thanks
    Sachin

Back to Top