Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
530
Add row to child grid giving the id of the parent row
posted

i have a hierarchical grid..

root grid displays the companys, and the child grid displays the users.

the class company is described by idcompany,name.

the class users is discribed by iduser,name, idcompany..

is it possible when adding a user programatically add in the correct grid,,

like get the reference of the father row by giving the id, then get the reference of the child grid, and then add the row user?

Thanks 

Parents
  • 530
    posted

    so i manage to get it working somehow, but still i am having some problems..

    it is possible to add a user to a chosen company, but the problem now is that after i do the commit and the savechanges it doesnt detect the add row user...

     i have pagging and sorting , and filtering define on the grid

    what i do is loop throught the all rows of the grid, then i try to find the id of the row that matches the id of the comapny.

    then i expand that row..

    after i do that i fetch all childrens, i do this because there mighty be more that one subgrid open.

    when i loop through the subgrids i fecth the id of the father and compare to the id of company, then i add the row user..

    this is working but after i call the savechanges method it doesnt detect the transactions..

    is it possible doing this way , or should i use the api that already exists...

    in my page  i have 4 buttons on top of the grid..one for add user, one for add company, one for confiming the delete, and another rollback. 

    add user call a igdialog with the fields username,password, name and the company(this is a igcombo with the key  refering to the id of the company, and the value refering to the name of the company )

    i forgot to mention that the add company is working fine, and after i call save changes it detects the changes..

    here is my code for the add user botton:

    var indexPag = 0;
    var dataRow = 0;
    var found = 0;
    var i = 0;
    var ParteDecimal = 0;


    var iRecordCount = $("#Grid_Utilizadores_By_Empresa").igGrid("totalRecordsCount");
    var iPageSize = $("#Grid_Utilizadores_By_Empresa").igGridPaging("pageSize");
    var resDivisao = iRecordCount % iPageSize;
    ParteDecimal = parseInt(iRecordCount / iPageSize);
    if (resDivisao > 0)
    ParteDecimal++;
    $("#Grid_Utilizadores_By_Empresa").igGridPaging("pageIndex", 0);
    for (i = 0; i < ParteDecimal; i++) {

    if (found == 0) {

    var Rows = $("#Grid_Utilizadores_By_Empresa").igGrid("allRows");
    $.each(Rows, function (ix, row) {
    rowId = $(row).attr("data-id");
    alert("rowId:" + rowId);
    alert("idermpresa:" + IDEmpresa);
    if (rowId == IDEmpresa) {
    dataRow = $(row).attr("data-row-idx");
    alert("dataRow:" + dataRow);
    found = 1;
    $("#DialogBox_AddOrEdit_Utilizador").igDialog("close");
    var parentGrid = $("#Grid_Utilizadores_By_Empresa").igHierarchicalGrid("rootWidget"),
    rowDomElement = parentGrid.allRows()[dataRow];
    $("#Grid_Utilizadores_By_Empresa").igHierarchicalGrid("expand", rowDomElement);


    }
    });
    if(found==0)
    $("#Grid_Utilizadores_By_Empresa").igGridPaging("pageIndex", i+1);

    }

    alert("found:" + found);

    }

    var childGrids = $("#Grid_Utilizadores_By_Empresa").igHierarchicalGrid("allChildren");
    $.each(childGrids, function (ix, grid) {
    var id = $(grid).closest("tr").prev().attr("data-id");
    //alert("gridid:" + id);
    if(id==IDEmpresa)
    $(grid).igGridUpdating("addRow", { Nome: "Milk", UserName: "saasa" });
    $("#Grid_Utilizadores_By_Empresa").igHierarchicalGrid("commit");
    $("#Grid_Utilizadores_By_Empresa").igHierarchicalGrid("saveChanges");
    });

    here is the code descibing the model on controller

    public ActionResult Index()
    {
    //initializar o model da grid
    GridModel grid = CreateGridModel();
    return View(grid);
    }
    // Take grid creation outside as it will be used in all methods:
    private GridModel CreateGridModel()
    {
    GridModel grid = new GridModel();
    // definir propriadades e as colunas

    grid.AutoGenerateColumns = false;
    grid.Columns = new List();
    grid.Columns.Add(new GridColumn("IDEmpresa", "IDEmpresa", "number", "0px"));
    grid.Columns.Add(new GridColumn("Nome da Empresa", "NomeEmpresa", "string", "200px"));
    grid.Columns.Add(new GridColumn("Nif", "Nif", "string", "100px"));
    grid.Columns.Add(new GridColumn("Tipo", "Tipo", "string", "100px"));

    //multicolumns header
    GridColumn group = new GridColumn();
    group.HeaderText = "Criada Por:";
    group.Group = new List();
    group.Group.Add(new GridColumn("Utilizador", "CriadaPeloIDU", "string", "100px"));
    group.Group.Add(new GridColumn("Empresa:", "CriadaPeloIDUDaEmpresa", "string", "100px"));
    grid.Columns.Add(group);
    //add feature
    grid.Features.Add(new GridMultiColumnHeaders() { Inherit = true });

    grid.PrimaryKey = "IDEmpresa";
    grid.AutoGenerateLayouts = false;
    // Criar layout da subgrid
    GridColumnLayoutModel layout = new GridColumnLayoutModel();
    layout.Key = "Utilizadores";
    layout.PrimaryKey = "UserId";
    layout.AutoGenerateColumns = false;
    layout.Columns = new List();
    layout.Columns.Add(new GridColumn("UserId", "UserId", "number", "0px"));
    layout.Columns.Add(new GridColumn("Username", "UserName", "string", "100px"));
    layout.Columns.Add(new GridColumn("Nome", "Nome", "string", "100px"));
    //multicolumns header
    GridColumn groupChild = new GridColumn();
    groupChild.HeaderText = "Criada Por:";
    groupChild.Group = new List();
    groupChild.Group.Add(new GridColumn("Utilizador", "CriadoPeloIDU", "string", "100px"));
    groupChild.Group.Add(new GridColumn("Empresa:", "CriadoPeloIDUDaEmpresa", "string", "100px"));
    layout.Columns.Add(groupChild);

    // Adicionar paging, filtering, updating a subgrid
    ////selection
    //GridSelection Selection = new GridSelection();
    //Selection.Mode = SelectionMode.Row;
    //Selection.MultipleSelection = true;
    //layout.Features.Add(Selection);
    ////row selectors checkbox
    //GridRowSelectors ChkSelection = new GridRowSelectors();
    //ChkSelection.EnableCheckBoxes = true;
    //ChkSelection.EnableRowNumbering = false;
    //layout.Features.Add(ChkSelection);
    //filtering
    GridFiltering layoutFiltering = new GridFiltering();
    layoutFiltering.Type = OpType.Remote;
    layout.Features.Add(layoutFiltering);

    //paging
    GridPaging layoutPaging = new GridPaging();
    layoutPaging.VisiblePageCount = 2;
    layoutPaging.PageSize =10;
    layoutPaging.ShowPageSizeDropDown = false;
    layoutPaging.ShowPagerRecordsLabel = false;
    layoutPaging.Type = OpType.Remote;
    layout.Features.Add(layoutPaging);
    //Updating
    GridUpdating layoutUpdating = new GridUpdating();
    layoutUpdating.EnableAddRow = false;
    layoutUpdating.EnableDeleteRow = true;
    layoutUpdating.EditMode = GridEditMode.None;
    layout.Features.Add(layoutUpdating);

    //adicionar o laytout da subgrid a grid
    grid.ColumnLayouts.Add(layout);

    //most importantly turn Load On demand on and define response Urls:
    grid.LoadOnDemand = true;
    grid.DataSourceUrl = this.Url.Action("BindParent");
    grid.ColumnLayouts[0].DataSourceUrl = this.Url.Action("BindChild");
    grid.UpdateUrl = Url.Action("EditingSaveChanges");
    ////selection
    //GridSelection SelectionRoot = new GridSelection();
    //SelectionRoot.Mode = SelectionMode.Row;
    //SelectionRoot.MultipleSelection = true;
    //grid.Features.Add(SelectionRoot);
    ////row selectors checkbox
    //GridRowSelectors ChkSelectionRoot = new GridRowSelectors();
    //ChkSelectionRoot.EnableCheckBoxes = true;
    //ChkSelectionRoot.EnableRowNumbering = false;
    //grid.Features.Add(ChkSelectionRoot);
    //sorting
    GridSorting sorting = new GridSorting();
    sorting.Type = OpType.Remote;
    grid.Features.Add(sorting);
    //paging
    GridPaging paging = new GridPaging();
    paging.PageSize = 10;
    paging.ShowPageSizeDropDown = false;
    paging.ShowPagerRecordsLabel = false;
    paging.Type = OpType.Remote;
    grid.Features.Add(paging);
    //updating
    GridUpdating Updating = new GridUpdating();
    Updating.EnableAddRow = false;
    Updating.EnableDeleteRow = true;
    Updating.EditMode = GridEditMode.None;
    grid.Features.Add(Updating);
    //filtering
    GridFiltering filtering = new GridFiltering();
    filtering.Type = OpType.Remote;
    grid.Features.Add(filtering);
    //And lastly set ID so the client widget can be normally interacted with (see the events in the view)
    grid.ID = "Grid_Utilizadores_By_Empresa";
    // Return the finished model
    return grid;
    }

    public JsonResult BindParent()
    {
    MvcIdonic DB = new MvcIdonic();
    //create a model
    GridModel grid = CreateGridModel();
    //set its datasource
    //get the companys
    grid.DataSource = ListarEmpresas();
    //and use the GetData() method to return the results
    return grid.GetData();
    }

    public JsonResult BindChild(string path, string layout)
    {
    MvcIdonic DB = new MvcIdonic();
    //get id of the company from the path

    string[] arrSplitVarPath = path.Split(':');
    int IDEmpresa = Int32.Parse(arrSplitVarPath[1]);
    //create a model
    GridModel grid = CreateGridModel();
    //set its datasource
    //get user data
    grid.DataSource = ListarUtilizadoresByIDEmpresa(IDEmpresa);
    // grid.DataSource = (from UProfile in DB.UserProfiles orderby (UProfile.UserId) select (UProfile));
    //and use the GetData() method to return the results
    return grid.GetData(path, layout);
    }

    and here is the update function

    [ActionName("EditingSaveChanges")]
    public ActionResult EditingSaveChanges()
    {

    MvcIdonic DB = new MvcIdonic();
    GridModel m = new GridModel();
    JsonResult result = new JsonResult();
    Dictionary response = new Dictionary();

    List<Transaction> categoryTransactions = m.LoadTransactions(HttpContext.Request.Form["ig_transactions"]);
    foreach (Transaction t in categoryTransactions)
    {
    if (t.layoutKey == null)
    {
    if (t.type == "deleterow")
    {

    }
    else if (t.type == "newrow")
    {
    bool contactExists = DB.Empresas.Any(Emp => Emp.NomeEmpresa.Equals(t.row.NomeEmpresa) && Emp.Rem == 0);
    if (contactExists)
    {
    response.Add("Success", 2);

    }
    else if (DB.Empresas.Any(Emp => Emp.NIF == t.row.Nif && t.row.Nif != ""))
    {
    response.Add("Success", 3);

    }

    else
    {
    Empresa Emp = new Empresa();
    Emp.NomeEmpresa = t.row.NomeEmpresa;
    Emp.NIF = t.row.Nif;

    Emp.TipoCliente = GetIDDescTipo(t.row.Tipo);
    Emp.TStamp = GetDateTimeFromSqlSytem();
    Emp.IDU = WebSecurity.CurrentUserId;
    DB.Empresas.Add(Emp);
    DB.SaveChanges();
    response.Add("Success",1);
    }
    }

    }
    }
    List<Transaction> productTransactions = m.LoadTransactions(HttpContext.Request.Form["ig_transactions"]);
    foreach (Transaction t in productTransactions)
    {
    if (t.layoutKey == "Utilizadores")
    {
    if (t.type == "deleterow")
    {

    }
    else if (t.type == "row")
    {

    }
    }
    }

    result.Data = response;
    return result;
    }

    I cant resolve the problem,,,i have change the code and tryed again...nothing..

    i cant found any info about this problem...

    i am gona create a ajax call to add the user...it is not the best way, but i cant loose more time for now in this one problem...

    if anyone knows what might be the problem, do tell me...

    i had changed the code by enabling add row in both parent and child row..what happens is that if i had a user then add a company it detects both transaction, if i add only a comapny, it detects, if add only user it does nothing..

    i think i am not declaring the model in the rigth way..

    so please tell me what the problem is...

Reply Children