Call js when call CREATE method

11 posts, 1 answers
  1. Gusev
    Gusev avatar
    83 posts
    Member since:
    Dec 2012

    Posted 04 Apr 2013 Link to this post

    Hello!
    I want create KEY(first object in my model), before call method Create!
    I dont want create KEY, If I edit object, so I can't  write this:
    Events(e => e.Edit("getKey()"))

    So,probably,I need this:
    .ToolBar(commands =>
            {
                commands.Create().HtmlAttributes(new { onclick = "getKey()" });
            })
    How I can this do?
    Because it's doesn't work :)

    Please help!
    And thanks for help)

  2. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2284 posts

    Posted 04 Apr 2013 Link to this post

    Hello Grigoriy,


    To achieve this, you could bind to the edit event of the Grid and check if the current operation is Create with the isNew() method of the model.
    E.g.
    .Events(e => e.Edit("edit"))

    function edit(e) {
      if (e.model.isNew()) {
          //custom logic
      }
    }

     

    Greetings,
    Dimiter Madjarov
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Gusev
    Gusev avatar
    83 posts
    Member since:
    Dec 2012

    Posted 05 Apr 2013 Link to this post

    Hello!
    Thanks for answer!

    Can I ask you?
    I need create Key = GUID when I click button "Add new object"
    I use this
    .Event(e => e.....)
    in Grid
    &
    js:
    function edit(e) {
            if (e.model.isNew()) {
                e.model.Key = guid();
            }
        }
    Wood is my Model in Grid,which have field -  Key
    and Next : (in popUp subGrid(Model WoodPeeling - ) I use this:
    .Read("Select", "WoodPeeling", new { IdParent = "${Id}", ParentKey = "${Key}" })
    and here I need a KEY,which I create in method  edit(e) : --------    here need : ParentKey = "${Key}"
    but Key is ""  
    what did I do wrong?

    In my opinion I have to call to "edit" event during initialization, but I do not know how to do it
  4. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2284 posts

    Posted 09 Apr 2013 Link to this post

    Hello Grigoriy,


    I tried to reproduce the issue, that you are mentioning, but to no avail. Could you please check if there are any JavaScript errors in the console? If you want to use the Kendo UI guid function in the current scenario, you should call kendo.guid() in the edit event handler. If you are still experiencing any issues, please send me a runnable sample project, so I could inspect it locally and assist you further.

    I am looking forward to hearing from you.

     

    Regards,
    Dimiter Madjarov
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  5. Gusev
    Gusev avatar
    83 posts
    Member since:
    Dec 2012

    Posted 10 Apr 2013 Link to this post

    Hello!
    I attach KendoProject with My problem inside!
    But I delete all script,because  was need 2 mb :)

    next I attach five screenshots:
    console -> my new object (then I click button "Add new object") has field: ShipCountry = "GOTOCONTROLLER" (I set this value in js method , then call event edit)
    index_event -> I subscribe to an event
    index_isnew -> I change value ShipCountry = "GOTOCONTROLLER" 
    index_read_with_new_country -> It's my method read with new string Key = #= ShipCountry #
    visual_studio -> but my string Key  = " bla bla bla" , not "GOTOCONTROLLER"

    Please Help!
  6. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2284 posts

    Posted 12 Apr 2013 Link to this post

    Hi Grigoriy,


    To get the ShipCountry value in the Create Action, you could pass it to the template method when you are rendering it, just like the OrderID property.
    E.g.
    function edit(e) {
        if (e.model.isNew()) {
            e.model.ShipCountry = "GOTOCONTROLLER";
        }
        e.container.find("#popupGridWrapper").html(template({ OrderID: e.model.id, ShipCountry: e.model.ShipCountry }));
    }

    Wish you a great day!

     

    Regards,
    Dimiter Madjarov
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  7. Gusev
    Gusev avatar
    83 posts
    Member since:
    Dec 2012

    Posted 15 Apr 2013 Link to this post

    Hello!
    Thanks for your answer! 
    And I have second problem(last),similar to the previous :)
    1) Grid:
    Model Wood has fields: Id , Name, Key
    @(Html.Kendo().Grid<Wood>()
            .Name("Wood")
            .Events(e => e.Edit("edit"))
            //Here other
            .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("Wood").Window(w => w.Title("Edit")))
            //Here other
    )
    2) Event edit:
    function edit(e) {
            if (e.model.isNew()) {
                e.model.Key = guid();
            }
        }
    3) EditorTemplates: (Wood)
    @model Wood
    <h2>Wood Edit</h2>
    @Html.HiddenFor(o => o.Id)
    @Html.LabelFor(o => o.Name)
    @Html.EditorFor(o => o.Name)
       
    @(Html.Kendo().Grid<WoodPeeling>()
            .Name("WoodPeeling")
            // Other
              .DataSource(....
              // Ajax and i.e. Read,Update and i.e
                    .Create("Create", "WoodPeeling", new {ParentKey = "${Key}" })
              //
            )
           //Other
            .ToClientTemplate()
    )
    And I want Create object in WoodPeeling with ${Key}  from Wood (Key - which created , when we call method edit in Wood)

    How I can this do?
  8. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2284 posts

    Posted 17 Apr 2013 Link to this post

    Hi Grigoriy,


    In the current scenario, you could pass the generated guid key as a data to the template, when you are rendering it, as demonstrated in the previous post.

     

    Greetings,
    Dimiter Madjarov
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  9. Gusev
    Gusev avatar
    83 posts
    Member since:
    Dec 2012

    Posted 17 Apr 2013 Link to this post

    I don't understand, how I can this do!
    Because my WoodPeeling.html() does not have template (only its)
    e.container.find("#WoodPeeling").html(//here:
     I need replace html()?

    Or 
    e.container.find("#WoodPeeling").getKendoGrid().dataSource //here
    push {Id : ....} ?
  10. Answer
    Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2284 posts

    Posted 19 Apr 2013 Link to this post

    Hi Grigoriy,


    I am not sure that I understand the exact case. If the approach from the previous posts does not apply, you could use the Data method of the Grid's DataSource Action Builder, which defines a JavaScript function, that will return additional data for the Action. Please check the documentation on this topic.
    E.g.
    DataSource(source => source.Ajax()
        ...
        .Create(create => create.Action("Create_Order", "Orders").Data("additionalData"))

    function additionalData() {
        return {
            parentKey: guid()
        };
    }

    If this approach does not work in the current case, please send me the modified solution, so I could assist you further.

     

    All the best,
    Dimiter Madjarov
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  11. Gusev
    Gusev avatar
    83 posts
    Member since:
    Dec 2012

    Posted 19 Apr 2013 Link to this post

    Hello!
    Thanks for your help!
    all good works!
    If I will find error, I will write)
    thanks!)
Back to Top