Insert Row in Flexsheet - Adding default values

Posted by: kuhnimanagam_raznin on 28 January 2019, 5:05 pm EST

  • Posted 28 January 2019, 5:05 pm EST

    Team,

    I am finding difficulty with the insert row method in Flexsheet. As you have suggested in one of your earlier post I am using the insertRow method to add a row.Then adding the default values by cloning the object as below:

    flexsheet.insertRows(index);
    let row=flexSheet.rows[index+1];
    $.extend(row.dataItem,item);

    But while doing so the getter and setter methods are not getting called.Since I am using aTypescript object with getter and setter methods,what would be the best way to go about this? Also,just wanted to confirm if this is the recommended way of inserting a row.

    Thanks,
    Raznin
  • Replied 29 January 2019, 4:10 pm EST

    Hi Raznin,

    If we understand correctly, then you have getter/setter props defined on the item now when you extend the object you are expecting the getter/setter to be copied on the row.dataItem object as well.
    The issue with the approach is that getter/setter are not copied to the new object automatically, we need to define them on row.dataItem explicitly.
    Please refer to the following code snippet:
    let descriptor = Object.getOwnPropertyDescriptor(Item.prototype, "propName");
    Object.defineProperty(row.dataItem, "propName", descriptor);

    A better and recommended solution in this case would be to just simply assign a newItemCreator function on the source collectionView which return an object of the appropriate type for the row.dataItem.
    Please refer to the following code snippet and sample:
    let source = new wjcCore.CollectionView(getData(50));
    source.newItemCreator = () => {
    return new Item(-1, "default country");
    };
    flexSheet.addBoundSheet("sheet", source);

    https://codesandbox.io/s/k3yx6w23lr
    >>>>>>>>>>>>>>
    Also,just wanted to confirm if this is the recommended way of inserting a row.
    <<<<<<<<<<<<<<
    Yes indeed, insertRows() method is the recommended way of inserting new rows. As for the default values assigning a newItemCreator function is the recommended approach.

    ~Sharad
  • Replied 30 January 2019, 10:16 am EST

    Thanks for the response Sharad!

    I tried this fix in our solution and it seems to work.But we are using ObservableArray as dataSource instead of CollectionView. Is there a similar functionality like newItemCreator in Observable array that we can use.So that the changes we need to make to the application are minimal for this fix.

    Thanks,
    Raznin
  • Replied 30 January 2019, 3:09 pm EST

    We are sorry but there isn't any property like newItemCreator for ObservableArrays. What you can do is simply wrap your ObservableArray in CollectionView and everything should work just fine.
    Please refer to the following code snippet:
    let observableArray = new wjcCore.ObservableArray(getData(50));
    flexSheet.addBoundSheet(
    "sheet",
    new wjcCore.CollectionView(observableArray, {
    newItemCreator: () => {
    return new Item(-1, "default country");
    }
    })
    );
  • Replied 1 February 2019, 3:36 am EST

    Thanks Sharad!


    The approach seems to work. But the Formulas are not updating after Sort and Insert Rows.

    Thanks,
    Raznin
  • Replied 3 February 2019, 4:00 pm EST

    Hi Raznin,

    We are able to replicate the issue at our end and hence forwarded it to the concerted team for further investigation of the issue(Internal tracking Id: 365642). We will let you know as we get an update on this.

    Thanks for reporting the issue.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels