Working with array of values in editor

Working with array of values in editor

dynasoftdynasoft Posts: 446Questions: 69Answers: 3
edited October 2019 in Free community support

Hi

I have a need to modify the label of checkboxes on an editor form. The field names are shown on the editor as per the server code under the MJOIN (field "ListName") but on the form the wording needs to be different as the pricelist names are followed by a space, a dash, a space and some text based on the value of PriceListLevel. The problem I have is the 'data: function' js code is not hit when creating a new row, only when opening an existing row and I'm trying to get the right syntax for iterating values in the CustomerVoiceCLIPriceLists0 array. Thank you.

JS:

ajax: {
    ...
},
table: '#tblDataTable',
template: '#EditorForm',
fields: [ 
    {
        label: '@(lblo.lblLists):',
        name: 'CustomerVoiceCLIPriceLists0[].id',
        type: "checkbox",
        data: function (row, type, val) {

            if (row != null) {
                $.each(row.CustomerVoiceCLIPriceLists0, function (i, e) {
                    if (e.PriceListType == 0)   //voice sales price lists
                    {
                        if (e.PriceListLevel == 0)
                        {
                            return e.PriceList + ' - @(lblo.lblBaseList)';
                        }
                        else
                        {
                            return e.PriceList + ' - @(Model.ContactNumber)';
                        }
                    }
                });
            }
        }
    }
],
i18n: {...}
});

Server:

using (Database db = new Database(SetGetDbType2, SetGetDbConnection))
{
    editor = new Editor(db, "CustomerVoiceCLI", "CustomerVoiceCLI.id")
        .Model<CustomerSNsDBModel.CustomerVoiceCLI>("CustomerVoiceCLI");
    editor.Field(new Field("CustomerVoiceCLI.id")
        .Set(false)
    );    
    editor.MJoin(new MJoin("CustomerVoiceCLIPriceLists")
        .Name("CustomerVoiceCLIPriceLists0")
        .Link("CustomerVoiceCLI.CustID", "CustomerVoiceCLIPriceLists.CustomerIndex")
        .Link("CustomerVoiceCLI.id", "CustomerVoiceCLIPriceLists.CustomerVoiceCLIIndex")
        .Model<CustomerSNsDBModel.CustomerVoiceCLIPriceLists>()
        .Order("CustomerVoiceCLIPriceLists.CustomerVoiceCLIIndex ASC")
        .Where(q =>
            q.Where("CustomerVoiceCLIPriceLists.PriceListType", 0)  //PriceListType indicates sales or purchase price list
        )
        .Field(new Field("id")
            .Options(new Options()
                .Table("GlobalPriceLists")
                .Value("id")
                .Label("ListName")
                .Where(q =>
                   q.Where("GlobalPriceLists.ListType", 0)  //ListType indicates voice or serv list
                )
                .Where(q =>
                    q.Where("GlobalPriceLists.ListInDB", 1)
                )
                .Where(q =>
                    q.Where("GlobalPriceLists.CustomerIndex", 0)
                )
                .Where(q =>
                    q.Where("GlobalPriceLists.CustomerIndex", lngContIdx)
                )
            )
            .Set(false)
        )
        .Set(false)
    );

    editor.PostRemove += (sender, e) => t = Task.Run(() => Delete(intContTpe, lngContIdx, lngItemIdx)); //delete sn
    editor.Debug(true);
    editor.Process(formData);
}

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Treating the data as an array and modifying the values of its elements does not change what is displayed in the editor. I use 'data: function (row, type, val) {' else and it works but not here. Am I missing something? Thanks.

                        label: '@(lblo.lblSalesPriceLists):',
                        name: 'CustomerVoiceCLIPriceLists0[].id',
                        type: "checkbox",
                        data: function (row, type, val) {
    
                            if (row != null) {
                                for (var i = 0; i < row.CustomerVoiceCLIPriceLists0.length; ++i) {
                                    if (row.CustomerVoiceCLIPriceLists0[i].PriceListType == 0)   //voice sales price lists
                                    {
                                        if (row.CustomerVoiceCLIPriceLists0[i].PriceListLevel == 1)
                                        {
                                            row.CustomerVoiceCLIPriceLists0[i].PriceList += ' (@(Model.ContactNumber))';
                                        }
                                        else
                                        {
                                            row.CustomerVoiceCLIPriceLists0[i].PriceList += ' (@(lblo.lblBaseList))';
                                        }
                                    }
                                };
                            }
                            return row;
                        }
    
  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Should read:

    I use:

    'data: function (row, type, val) {'

    elsewhere where there is no array of values and it works but not here.

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    I'd actually very much encourage you not to use the fields.data option as a function. It is possible, but you need to handle the set code as well.

    If all you need to do is modify the text shown for the checkbox label, use the Label method of the Options class on the server-side which is designed for exactly this sort of thing.

    Allan

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Shoudl add that all buttons on the datatable point to the editor:

                buttons: [
                    { extend: 'create', editor: editor },
                    { extend: 'edit', editor: editor },
                    { extend: 'remove', editor: editor }
                ],
    
  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    That's fine. I still think the server-side Options label rendering will be the way to do this. Did you give that a go?

    Allan

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Hi Allan

    I'm trying but am not sure how to use the Render method (I think that's what I have to use). Here's the code I have in server:

                        .Field(new Field("id")
                            .Options(new Options()
                                .Table("GlobalPriceLists")
                                .Value("id")
                                .Label("ListName")
                                .Where(q => q.Where(r => {
                                    r.Where("GlobalPriceLists.ListInDB", 1);
                                    r.Where("GlobalPriceLists.ListType", 0);
                                    r.Where("GlobalPriceLists.CustomerIndex", 0);
                                    //Show "ListName" as "ListName" + " - 1"
                                }).OrWhere(s => {
                                    s.Where("GlobalPriceLists.ListInDB", 1);
                                    s.Where("GlobalPriceLists.ListType", 0);
                                    s.Where("GlobalPriceLists.CustomerIndex", lngContIdx);
                                    //Show "ListName" as "ListName" + " - 2"
                                }))
                            )
                            .Set(false)
                        )
    

    I have to show the ListName differently based on the Where clause:

    If GlobalPriceLists.CustomerIndex = 0 then show "ListName" as "ListName" + " - 1"
    If GlobalPriceLists.CustomerIndex > 0 (ie has a value from lngContIdx) then show "ListName" as "ListName" + " - 2". There's this example but am not sure as I can't see what the syntax would be for .net: https://editor.datatables.net/manual/php/joins#Options-class. Thanks.

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    Two options:

    1. If you want to do the label changing conditions in SQL, then create a VIEW and read from that. Or,
    2. Do the label changing conditions in C# - e.g. based on the third example for .NET here
            .Label(new []{"ListName", "ListInDB", "ListType", "CustomerIndex"})
            .Render(row => {
              if (row["ListInDB"] == 1 && row["ListType"] == 0 && ... ) {
                return row['ListName"] + " - 1";
              }
              ...
            })
    

    Allan

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Thanks

This discussion has been closed.