put if clause in Editor

put if clause in Editor

suwarjono@daunbiru.comsuwarjono@daunbiru.com Posts: 10Questions: 2Answers: 0

Hi Support

i need sime if cluase in where editor. how could be done.

using (var db = new Database(settings.DbType, settings.DbConnection))
        {
            var response = new Editor(Db, "staff")
                .Model<StaffModel>()
                .Field(new Field("start_date")
                    .Validator(Validation.DateFormat(
                        Format.DATE_ISO_8601,
                        new ValidationOpts { Message = "Please enter a date in the format yyyy-mm-dd" }
                    ))
                    .GetFormatter(Format.DateSqlToFormat(Format.DATE_ISO_8601))
                    .SetFormatter(Format.DateFormatToSql(Format.DATE_ISO_8601))
                )
// need some if clause where in here
// if(x > 0) .where ("db","id","=")
//
                .Process(request)
                .Data();
 
            return Json(response);
        }

please give suggest

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi,

    The Editor .NET manual describes how a where condition can be used on the data set.

    Regards,
    Allan

  • suwarjono@daunbiru.comsuwarjono@daunbiru.com Posts: 10Questions: 2Answers: 0

    Hi allan,

    Yess you're right.

    But i want ask some thing like this
    // need some if clause where in here
    // if(x > 0) .where ("db","id","=")
    //

    Like my comment on my question above.

    Thanks

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Two options:

    1) Simply break the chain - you don't need to have the Editor configuration in one bit chain. You could easily do:

    editor.Field( ... );
    editor.Field( ... );
    if ( x > 0 ) {
      editor.Where( ... );
    }
    

    2). Use a lambda function for the Where method and add the condition in there.

    Allan

  • suwarjono@daunbiru.comsuwarjono@daunbiru.com Posts: 10Questions: 2Answers: 0

    hi allan,

    i've trying like you said, but its failed. what doyou think, where is my probblem?

    var request = System.Web.HttpContext.Current.Request;
    using (var db = new Database(setting.DbType, setting.DbConnection))
    {
    var response = new Editor(db, "a_table");
    response.Model<DrillModel>();
    response.LeftJoin("b_table", "b_table.id", "=", "a_table.id_a");
    response.Where("a_table.tgl", tg1, ">");
    response.Where("a_table.tgl", tg2, "<=");
    
    if (x>0) {
    response.Where("a_table.id", x, "=");
    }
    
    response.Process(request);
    response.Data();
     return Json(response, JsonRequestBehavior.AllowGet);
    
    }
    
    
    

    please give suggest
    thanks

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    In what way did it fail? I presume it gave an error message - what was that message? What is x - I presume that is defined higher up in the function?

    Allan

  • suwarjono@daunbiru.comsuwarjono@daunbiru.com Posts: 10Questions: 2Answers: 0

    Hi Allan,

    here my console.log error feedback

    Uncaught TypeError: Cannot read property 'length' of undefined
    

    i dont understand which code was wrong..

    please give advice.

    //br
    thanks

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I'm afraid I don't from the information available either! Can you give me a link to the page so I can debug it please.

    What is the JSON reply from the server just before this error happens?

    Allan

This discussion has been closed.