Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

  • Tree Grid doesn't save new nodes to database

    I am using SmartGwt Power 4.1p 2015-09-30 , Chrome 46.0.2490.80 OSX ElCapitan.
    I'm developing TreeGrid and want to edit, add items inline in grid. It works but new added records arenít save in database. After refetch I can see only changed data not inserted.
    Following is a sample codes what I tried to test. Sample is based on BuiltInDs.
    Pls help me to improve this sample.


    public void initTreeGrid() {
    treeGrid = new TreeGrid();
    treeGrid.setWidth100();
    treeGrid.setHeight(400);

    treeGrid.setLoadDataOnDemand(false);
    treeGrid.setDataSource(DataSource.get("employees"));
    treeGrid.setAutoFetchData(true);
    treeGrid.setCanEdit(true);
    treeGrid.setCanRemoveRecords(true);
    treeGrid.setAutoSaveEdits(false); // "true" + treeGrid.SaveAllEdits() also doesn't work

    TreeGridField nameField = new TreeGridField("Name", 150);
    TreeGridField idField = new TreeGridField("EmployeeId");
    TreeGridField parentIdField = new TreeGridField("ReportsTo");
    // btw: why ReportsTo field is invisible in grid?
    TreeGridField jobField = new TreeGridField("Job", 150);
    TreeGridField employeeStatusField = new TreeGridField("EmployeeStatus",
    150);
    TreeGridField genderField = new TreeGridField("Gender");
    TreeGridField maritalStatusField = new TreeGridField("MaritalStatus");

    treeGrid.setFields(nameField, idField, parentIdField, jobField,
    employeeStatusField, genderField, maritalStatusField);

    insertBtn = new IButton("Insert");
    insertBtn.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
    insertNode(0);
    }
    });
    }

    private int insertNode(int insertChild) {
    DataSource seqDs = DataSource.get("employees_seq");
    DSRequest request = new DSRequest();
    request.setAttribute("insertChild", insertChild);
    seqDs.fetchData(null, new DSCallback() {
    public void execute(DSResponse response, Object rawData,
    DSRequest request) {
    Record r[] = response.getData();
    String newId = r[0].getAttributeAsString("nextval");
    int insertChild = request.getAttributeAsInt("insertChild");
    Tree tree = treeGrid.getTree();

    String parentName = tree.getParentIdField();
    String idName = tree.getIdField();

    TreeNode selNode = treeGrid.getSelectedRecord();
    treeGrid.deselectAllRecords();

    String parentId = null;
    if (selNode != null) {
    if (insertChild == 1) parentId = selNode.getAttribute(idName);
    else parentId = selNode.getAttribute(parentName);
    } else {
    parentId = tree.getRootValue();
    }

    TreeNode newNode = new TreeNode();
    newNode.setAttribute(parentName, parentId);
    newNode.setAttribute(idName, newId);
    tree.add(newNode);
    if (insertChild == 1) treeGrid.openFolder(selNode);
    int recordIndex = treeGrid.getRecordIndex(newNode);
    treeGrid.scrollToRow(recordIndex);
    treeGrid.startEditing(recordIndex);
    }
    }, request);
    return 0;
    }
    Attached Files

  • #2
    See docs for TreeGrid.startEditing() - you seem to have partially implemented the strategy explained there, except you've got no code that would specially handle the "update" operation that will be passed to the server for a new node, so your code as it stands won't work for that reason.

    Comment


    • #3
      When I add new record to tree with setAutoSaveEdits(false) id value is black in grid. So it looks like saved to database even autosave is false. When I add other then id values to new record those are blue. Next I try to save data database UPDATE .... WHERE ID=... are generated and there is no error but no data are updated because there are not such records in database.
      So, i think update is OK but inserts are ommited.
      When i use startEditNew instead startEdit it works properly but records initially are added under last tree element and it is annoying. Unfortunately documentation to startEdit is realy short and there is no working sample. I still don't know how to do it in simple way. I made some workaround, but I think TreeGrid could manage simple add/insert operation natively.

      Last edited by Sanmargar; 6th Nov 2015, 15:57.

      Comment


      • #4
        If you add a record to the Tree it would be expected to be black, because, as the docs note, what you have done is directly add a record to the client-side cache which the Tree believes already exists on the server.

        This again is why you should expect the first save from the Tree for that node to use operationType "update" and not "add", and your server code needs to handle this.

        Comment


        • #5
          OK. I found my mistake. It works. I only had to change type="sequence" to integer in ds.xml and use treeGrid.addData(newNode) instead tree.add(newNode). I generate new value myself so type should be integer. Nevertheless I still don't know how to run it smoothly with AutoSave=false.
          Last edited by Sanmargar; 9th Nov 2015, 15:36.

          Comment

          Working...
          X