Pre-populate insert item with batch edit

1 posts, 0 answers
  1. Marin
    Marin  avatar
    22 posts
    Member since:
    Jul 2012

    Posted 23 Jul 2018 Link to this post

    PROJECT DESCRIPTION 
    After the user clicks the Add New Record button, you may want to preset some information for them in certain cells. If using validation or hint messages (empty messages/placeholders) in a template column does not suit your needs, you can use the batch edit manager client-side API and the OnBatchEditOpened event to set a value in the cell with your code:

    function OnBatchEditOpened(sender, args) {
        if (parseInt(args.get_row().getAttribute("id").split("__")[1]) < 0) {//this is the insert row - its index is below 0
     
            args.get_tableView().get_dataItems();//enumarate data items so they get created and you can use their API
            var dataItem = $find(args.get_row().getAttribute("id"));//get the current data item (row)
            var batchEditingManager = sender.get_batchEditingManager();//get the batch editing manager
     
            //avoid wiping existing user input if the user goes back to the cell
            if (batchEditingManager.getCellValue(dataItem.get_cell("myTargetColumn")) == "") {
                sender.remove_batchEditOpened(OnBatchEditOpened);//avoid recursion
                //use the batch edit manager API ot change cell values
                var desiredValue = 55;
                batchEditingManager.changeCellValue(dataItem.get_cell("myTargetColumn"), desiredValue);//pre-load the desired value
                setTimeout(function () {
                    sender.add_batchEditOpened(OnBatchEditOpened);//return the event handler after a bit so it can keep working
                    batchEditingManager.openCellForEdit(dataItem.get_cell("cellWhereTheUserShouldStart"));//open the next cell in case the cell we changed was the first - it would have lost focus
                }, 100);
            }
        }
     
        //note: if you use validation, review how this works as validation changes which cells can open for editing and where focus lies, so you may need to tweak this
    }

Back to Top