How to use dynamic data in a where-clause?

How to use dynamic data in a where-clause?

qwewqqwewq Posts: 5Questions: 2Answers: 0

Hi,

in Editor (.net) I have a Select List where the Options are dynamicly filled from database.
The result from the SQL query was shown in the "Options-List". Only results from the 'needle' manufacturer Apple are shown.
It works perfect:

    public IHttpActionResult AllArticles()
    {
        var request = HttpContext.Current.Request;
        var settings = Properties.Settings.Default;

        using (var db = new Database(settings.DbType, settings.DbConnectionFTMG))
        {
            var response = new Editor(db, "AllArticles")
                .Model<AllArticlesModel>()
                .TryCatch(false)
                .Field(new Field("AllArticles.Manufacturer"))
                .Field(new Field("AllArticles.PriceGroup"))
                .Field(new Field("AllArticles.ProductGroup")
                    .Options("ProductGroups", "ProductGroup", "Title", q => q
                    .Where("ProductGroups.Manufacturer", "Apple", "LIKE"))
                )
                .LeftJoin("ProductGroups", "AllArticles.MatKlasse", "=", "ProductGroups.MatKlasse")
                .Field(new Field("AllArticles.MatKlasse"))

                .Process(request)
                .Data();

            return Json(response);
        }
    }

But how can I fill this 'needle' dynamicly? How can this 'neelde' get the Value of the Field "AllArticles.Manufacturer" in the actually row?

Thx,
Marco.

Answers

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    Hi Marco,

    What you need to do is use the dependent() method to get the data from the server when it is required. The client-side doesn't know in advance what row will be edited, hence the need to make an Ajax request when the row is edited, in order to see what options are available.

    You would also need another route in your server-side code that will accept that request and return the data from the server having made an SQL query to get the data.

    Regards,
    Allan

  • qwewqqwewq Posts: 5Questions: 2Answers: 0

    Hi allan,

    thx for your help. I'll try it.
    I was hoping that I can make it like:
    "Now I'm in row[32]. In this row[32] I need the value of the column[2] 'manufacturer'.
    Somthing like "get the value of row[32], column[2]" - for each row.

    Thank you Allan.

  • qwewqqwewq Posts: 5Questions: 2Answers: 0

    Hi allan,

    now i try it like this:

           {
                label: "ProductGroups:",
                name: "ProductGroups",
                type: "select",
                options: [4, 5, 6]
            }
    

    I try to use the dependent() function

        editor.dependent('ProductGroups', function (val, data, callback) {
            var manufacturer = editor.get('AllArticles.Manufacturer');
    
            jQuery.ajax({
                url: 'AllArticles.aspx/getProductGroups',
                type: "POST",
                dataType: "json",
                data: "{'manufacturer': '" + manufacturer + "'}",
                contentType: "application/json; charset=utf-8",
                success: function (data2) {
                    //alert(data2.d);
                    callback(data2);
                },
                error: function (data2) {
                    alert(data2.status + ' ' + data2.statusText)
                }
            });
        });
    

    I get this json string back from code behind:

    {
      "options": {
        "ProductGroups": [
          "hallo 1",
          "hallo 2",
          "hallo 3",
          "hallo 4",
          "hallo 5",
          "hallo 6",
          "hallo 7",
          "hallo 8"
        ]
      }
    }
    
    

    If I use this json, the Select 'ProductGroups' will not be updated.
    If I use your example options it works:

                   callback({
                        options: {
                            ProductGroups: [7, 8, 9]
                        }
                    });
    

    Can you tell me what it's wrong with my json string?

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    Your code looks like it should work. Can you link to the page so I can debug it and see what is going wrong please.

    Allan

This discussion has been closed.