pre-remove event

pre-remove event

eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0

hey, i want to cancel event of remove with pre-remove only for specific case
i want to delete the row form the join table but not from the main table,
exmple:
i have daytrip table,trip table, and daytrip_trip table, in specific case i want to use the delete function to delete
only the data from the daytrip_trip table, the join table, how can i do this?
this is my code:

 [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        public ActionResult GetDataDayTrip(Enums.JsonSortType type, int id)
        {
            var settings = Properties.Settings.Default;
            var formData = HttpContext.ApplicationInstance.Context.Request;
            using (var db = new DataTables.Database(settings.DbType, settings.DbConnection))
            {
                var editor = new Editor(db, "DayTrip", "DayTrip_Id");
                if (type == PegasusOperation.Common.Classes.Enums.JsonSortType.DayTripByTrip)
                {
                    editor.Where(q =>
                    q.Where("DayTrip.DayTrip_Id", "(SELECT DayTrip_Id FROM Trip_DayTrip WHERE Trip_Id= " + id + ")", "IN", false)
                    );
                }
                editor.Model<Model.DayTripVM>("DayTrip")
                   .Field(new Field("DayTrip.DayTrip_Id")
                    .Validator(Validation.Numeric())
                )
                 .Field(new Field("DayTrip.DisplayName_Heb")
                    .Validator(Validation.MaxLen(200))
                    
                )
                 .Field(new Field("DayTrip.DisplayName_Eng")
                    .Validator(Validation.MaxLen(200))
                )
                 .Field(new Field("DayTrip.HeaderName_Heb")
                    .Validator(Validation.MaxLen(200))
                )
                .Field(new Field("DayTrip.HeaderName_Eng")
                    .Validator(Validation.MaxLen(200))
                )
                .Field(new Field("DayTrip.TripName")
                    .Validator(Validation.Unique(new ValidationOpts
                    {
                        Message = "שם הטיול קיים, אנא רשמו שם יחודי אחר"
                    }))
                    .Validator(Validation.MaxLen(50))
                    .Validator(Validation.NotEmpty(new ValidationOpts
                    {
                        Message = "חובה לתת שם ליום טיול"
                    }))
                )
                .Field(new Field("DayTrip.CountryId")
                    .Options(new Options()
                        .Table("Countries")
                        .Value("Country_Id")
                        .Order("Countries.Country_Id")
                        .Label("Country_Name"))
                 .Validator(Validation.Numeric())
                )
                 .Field(new Field("DayTrip.StartCity")
                    .Validator(Validation.MaxLen(20))
                )
                 .Field(new Field("DayTrip.FinishCity")
                    .Validator(Validation.MaxLen(20))
                )
                .Field(new Field("DayTrip.CitysTrip")
                    .SetValue(2)
                )
                .Model<Model.Countries>("Countries")
                .LeftJoin("Countries", "Countries.Country_Id", "=", "DayTrip.CountryId");
                editor.MJoin(new MJoin("Trip")
                .Link("DayTrip.DayTrip_Id", "Trip_DayTrip.DayTrip_Id")
                .Link("Trip.Trip_Id", "Trip_DayTrip.Trip_Id")
                .Model<Model.Trip>());
                editor.Debug(true);

                if (type == PegasusOperation.Common.Classes.Enums.JsonSortType.DayTripByTrip)
                {
                    editor.WriteCreate += (sender, e) => AddDayTrip(db, editor,e.Id, id);
                    editor.PreRemove += (sender, e) => RemoveDayTrip(db, editor, e.Id, id);
                }
                //// Pre functions
                //editor.PreEdit += (sender, e) => prev_Values = Common.Classes.Functions.getPrevValues(db, editor.Table()[0], "DayTrip_Id", e.Id);
                //editor.PreRemove += (sender, e) => prev_Values = Common.Classes.Functions.getPrevValues(db, editor.Table()[0], "DayTrip_Id", e.Id);


                //Post functions
                //editor.PostCreate += (sender, e) => Functions._LogChange(db, editor.Table()[0], "create", e.Id, e.Values, prev_Values);
                //editor.PostEdit += (sender, e) => Functions._LogChange(db, editor.Table()[0], "edit", e.Id, e.Values, prev_Values);
                //editor.PostRemove += (sender, e) => Functions._LogChange(db, editor.Table()[0], "remove", e.Id, e.Values, prev_Values);

                editor.Process(formData);
                DtResponse data = editor.Data();
                return Json(data, JsonRequestBehavior.AllowGet);
            }
        }


        public void AddDayTrip(DataTables.Database db,Editor editor,object dayTripId,int TripId)
        {
            Dictionary<string, dynamic> keyValue = new Dictionary<string, dynamic>();

            keyValue.Add("DayTrip_Id", Convert.ToInt32(dayTripId));
            keyValue.Add("Trip_Id", TripId);

            db.Insert("Trip_DayTrip", keyValue);

        }

        public bool RemoveDayTrip(DataTables.Database db, Editor editor, object dayTripId, int TripId)
        {
            Dictionary<string, dynamic> keyValue = new Dictionary<string, dynamic>();

            keyValue.Add("DayTrip_Id", Convert.ToInt32(dayTripId));
            keyValue.Add("Trip_Id", TripId);

            db.Delete("Trip_DayTrip", keyValue);
            return false;
        }

Answers

  • eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0
    edited October 2019

    Hey one more question i have Allen, about select 2 issue
    i working with select 2 ajax remote data, all work when i create new row, but in edit mode the value the selected already didn't shown and instead there are place-Holder
    , what can i do to make it work? i have at lest 3 select 2 ajax remote call in this view?

    one last,
    i have table with 2 column of city, start_city and finish_city
    how can i get the values for both field from city table with left join? i cant get this

    sorry for all the question, but i am very close to finish my project! thanks

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    i want to cancel event of remove with pre-remove only for specific case
    i want to delete the row form the join table but not from the main table,

    I'm afraid that is not something that the Editor libraries provide the ability to do at the moment. You can either cancel the deletion(s) for a row, or not. There is no ability to selectively target only specific tables (the left joined ones).

    You'd need a bit of custom code to do that if that is what you wanted I'm afraid.

    i working with select 2 ajax remote data, all work when i create new row, but in edit mode the value the selected already didn't shown and instead there are place-Holder, what can i do to make it work? i have at lest 3 select 2 ajax remote call in this view?

    Yes - when using Select2 it makes an initialValue request to the server to get the value and label to display. Not ideal, but I've not found a better way to handle that yet.

    i have table with 2 column of city, start_city and finish_city
    how can i get the values for both field from city table with left join?

    Use two different field instances - one for each field:

    new Field("city.start_city"),
    new Field("city.finish_city")
    

    Allan

  • eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0

    Hi Allan i didnt understand what mean

    new Field("city.start_city"),
    new Field("city.finish_city")
    this what i have in my code:

    .LeftJoin("Cities", "Cities.City_Id", "=", "DayTrip.StartCity")
    .LeftJoin("Cities", "Cities.City_Id", "=", "DayTrip.FinishCity")
    

    i have 2 values that connect to same key in table city,
    how can i get each value from city table based on their value?

  • eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0

    Yes - when using Select2 it makes an initialValue request to the server to get the value and label to display. Not ideal, but I've not found a better way to handle that yet.

    hey allan how can i do this? you have exmple? or a little stuff about this?

  • eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0
    edited October 2019

    ok Allan, i figure a way to handle the remove and the select 2 issue
    what i left is the left join with cities!

  • eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0

    Allan can you help me please?
    what i left is the left join with cities!

    .LeftJoin("Cities", "Cities.City_Id", "=", "DayTrip.StartCity")
    .LeftJoin("Cities", "Cities.City_Id", "=", "DayTrip.FinishCity")
    

    i have 2 values that connect to same key in table city,
    how can i get each value from city table based on their value?

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    Hi,

    Sorry for not being clear enough before. My point was that you have joined the tables already, but you need to also read from the joined tables (otherwise nothing is read from them!)

    i.e.

    .Field( new Field("C1.start_city") )
    .Field( new Field("C2.finish_city") )
    .LeftJoin("Cities as C1", "C1.City_Id", "=", "DayTrip.StartCity")
    .LeftJoin("Cities as C2", "C2.City_Id", "=", "DayTrip.FinishCity")
    

    Note that I've used an alias for the joins so you can join the same table twice (differently).

    Allan

  • eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0

    Hey Allan sorry but i still cant understand, how i can read data from left join table?
    code:

    .Model<Model.CitiesVM>("Cities")
    .Field(new Field("C1.startCity"))
    .Field(new Field("C2.finish_city"))
    .LeftJoin("Cities as C1", "C1.City_Id", "=", "DayTrip.StartCity")
    .LeftJoin("Cities as C2", "C2.City_Id", "=", "DayTrip.FinishCity");
    

    i must used before the left join the model ?

  • eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0

    ok, i got it, now it works
    thanks allan

This discussion has been closed.