locked
Conditional (?) Data Binding in Lightswitch 2013 RRS feed

  • Question

  • Hello,

    I was planning to use cascading details pickers to achieve a certain function in my Inventory system. I need a simple, quick way to choose device details.

    My plan was to cascade, similar to a car dealership might with their inventory:

    Yard > Make > Model > Year, etc.

    Except I would be using device details such as:

    Type (laptop, desktop etc) > Manufacturer > > Model

    Then it dawned on me that if I have a model, which I do (the Details picker I have now looks at a table that contains ALL of these columns) so when I type 'laptop' into the Details Picker search box, the results are actually all laptop MODEL NUMBERS in that table.

    This is excellent, because each model number will always be unique to a Make, Manufacturer and a Type. For instance an E540 will always be a Lenovo > Laptop.

    My question is:

    How can I select 'E540' in the Details Picker on my Edit screen, and then the Manufacturer and Type 'fields' automatically be selected based on that selection, and those details be put into the item 'field' on the screen for their respective columns?

    I would love to tell you what I have done so far, but I am not sure where to begin.

    Any advice is MUCH appreciated. If anything needs to be done on the SQL end first, I can arrange that and go from there.

    Thank you!

    Wednesday, January 13, 2016 7:59 PM

Answers

  • Yes, you're on the right track, but you don't have to create duplicate properties or bind anything - with proper data normalization and relationships it's very much automatic.

    You would add a relationship between the tables and LS would automatically create navigation properties on both sides of the relationship.  For example, DeviceType is related to OrderLines one-to-many.  This automatically creates a DeviceType single-value navigation property in OrderLines table and a OrderLines collection navigation property in DeviceTypes table.

    Now on AddEditOrderLines screen you simply drag DeviceType nav prop onto the screen and you have a picker that will populate the navigation/relationship property.  Also, in screen designer you can manage included data to include the related data from device types table.  Now you can display the Model, Manufacturer, etc. on the screen even with out those fields being in the OrederLines table.

    Additionally, in code, you can use the reference properties to access values in DeviceTypes table like OrderLine.DeviceType.Model will return the model value from the device types table.

    HTH,

    Josh

    • Edited by joshbooker Friday, January 15, 2016 4:18 PM
    • Marked as answer by CreedCor Friday, January 15, 2016 6:46 PM
    Friday, January 15, 2016 4:13 PM

All replies

  • Can this be done with a New Trigger create on the entity in the Server Explorer?

    Thank you

    Thursday, January 14, 2016 6:48 PM
  • Upon changing one details picker, you can set the value of another details picker by using dataBind like so:

    myapp.AddEditOrders.Model_postRender = function (element, contentItem) {
        //when model picker value changes
        contentItem.dataBind("value", function (newValue) {
            if (newValue) {
                //set Make to that of selected Model
                contentItem.screen.Make = newValue.Make;
            }
            
        })
    };

    HTH,

    Josh

     
    Friday, January 15, 2016 1:05 PM
  • Thank you for the reply!

    Can the value be set to whatever is already in a column in this table?

    For example I have a table called Device Type. In the Device Type table (or entity) are several columns, one is Type (laptop, desktop, etc) another is Manufacturer (Lenovo, Apple, etc) another is Model Number (E540, X1, etc).

    All this data already exists in the table. I want to be able to choose 'E540' and the Type and Manufacturer 'fields' be populated from that row in the table to show Lenovo and laptop on the screen.

    I hope that makes sense, and thank you so much for the code sample! You have been a huge help!!

    Friday, January 15, 2016 1:53 PM
  • Yes you can but how depends on your tables and relationships.  You have a table called DeviceTypes having string props: Model, Mfr, Type.  Is that correct?  Now tell us about the other table that you are entering data into.  Does it have a relationship to DeviceTypes?  Does it also have these same string fields? 
    Friday, January 15, 2016 2:33 PM
  • So you're entering new data into the DeviceType table.  Your DeviceType picker is a list of rows in that same table.  What property is the picker bound to?  A screen property, called what?

    Please know you may have undesirable results doing it this way since you could have device types:

    E525 | Lenovo | Laptop

    E525 | Dell | Laptop

    The proper way to handle this would be to have separate tables with relationships between to represent the hierarchy.  Manufacturer related to Model related to Type.  That way you'd have reference properties such as Manufacturer.Models collection and Model.Type property.

    That's not to say you cannot do it with you're one-table setup.  How depends on what the picker is bound to.

    Friday, January 15, 2016 3:40 PM
  • So having two tables... 

    Say I have one table called DeviceType and in it is a column called 'StrType', populated with values like 'laptop', 'projector', etc...

    On my other table called editOrderLine (which the LS Screen is built on) I have a 'Type' column with no value.

    I would need to data bind the 'type' property in editOrderLine to the 'StrType' column in DeviceType, and then it would work, since the Details Picker is already choosing values from the DeviceType table.

    It is making sense now... but now I need to know how to edit data bindings. That should be native to LS without code, yes?

    Now that I get this conceptually, I think it would be editing post render code. Do you have a code example for data binding as follows:

    Bind the StrManufacturer property on the OrderLine table to the StrManufacturer propterty of the DeviceType table?

    The goal is to display the value of StrManufacturer in the DeviceType table in the StrManufacturer column of the OrderLine table.

    • Edited by CreedCor Friday, January 15, 2016 4:04 PM
    Friday, January 15, 2016 3:54 PM
  • Yes, you're on the right track, but you don't have to create duplicate properties or bind anything - with proper data normalization and relationships it's very much automatic.

    You would add a relationship between the tables and LS would automatically create navigation properties on both sides of the relationship.  For example, DeviceType is related to OrderLines one-to-many.  This automatically creates a DeviceType single-value navigation property in OrderLines table and a OrderLines collection navigation property in DeviceTypes table.

    Now on AddEditOrderLines screen you simply drag DeviceType nav prop onto the screen and you have a picker that will populate the navigation/relationship property.  Also, in screen designer you can manage included data to include the related data from device types table.  Now you can display the Model, Manufacturer, etc. on the screen even with out those fields being in the OrederLines table.

    Additionally, in code, you can use the reference properties to access values in DeviceTypes table like OrderLine.DeviceType.Model will return the model value from the device types table.

    HTH,

    Josh

    • Edited by joshbooker Friday, January 15, 2016 4:18 PM
    • Marked as answer by CreedCor Friday, January 15, 2016 6:46 PM
    Friday, January 15, 2016 4:13 PM
  • So confusing. I already have this relationship between DeviceType and OrderLine, set up in SQL...

    On the AddEditOrderLine screen designer, it looks like this...

    On the screen itself, the DeviceType picker is working...

     


    I just don't understand how to bind the Manufacturer and Model Number columns in the OrderLine table to the StrManufacturer and StrModelNumber columns in the DeviceType table, in order have them be automatically chosen.

    Or, should I somehow be USING the columns from the DeviceType table in the screen designer, instead of trying to bind the columns from the two separate tables?

    I added a data item and chose DeviceType > all... then dragged the StrType property to the screen designer...

    I had high hopes for this, but it doesn't render on the screen...

    So, I read more about your recommendations and selected Custom Control for the DeviceType Picker that is working, and that is exactly what I need to show...


    Again, high hopes...but...

    Render errors when that control is set to anything but Summary.

    • Edited by CreedCor Friday, January 15, 2016 5:12 PM
    Friday, January 15, 2016 4:44 PM
  • Sorry don't have time for a complete answer, but...

    Or, should I somehow be USING the columns from the DeviceType table in the screen designer, instead of trying to bind the columns from the two separate tables?

    Yes, if you don't need to change those values, you can use them from DeviceTypes like so:

    change binding on strType text box to:

    OrderLine.DeviceType.strType

    If that doesn't work then, check out Manage Included Data  on OrderLines query to be sure DeviceType is included.

    You can remove the DeviceTypes Query from the screen. 

    gotta run, let us know how you make out.

    HTH,

    Josh

    Friday, January 15, 2016 5:06 PM
  • Ok, thank you.

    I got this far...

    "

    You would add a relationship between the tables and LS would automatically create navigation properties on both sides of the relationship.  For example, DeviceType is related to OrderLines one-to-many.  This automatically creates a DeviceType single-value navigation property in OrderLines table and a OrderLines collection navigation property in DeviceTypes table.

    Now on AddEditOrderLines screen you simply drag DeviceType nav prop onto the screen and you have a picker that will populate the navigation/relationship property."

    I can display the summary of the DeviceType table on the AddEditOrderLine screen, in a Detail Picker, as I always could.

    Achieving this part would be perfect...

    "Also, in screen designer you can manage included data to include the related data from device types table.  Now you can display the Model, Manufacturer, etc. on the screen even with out those fields being in the OrederLines table."

    But it is not working out for me. All the data bindings look correct, but there is no way to edit the Manage Included Data on this screen because there is no query. The relationship is there because the picker works when set to summary. Not sure =(.

    Thank you for your help.


    • Edited by CreedCor Friday, January 15, 2016 6:06 PM
    Friday, January 15, 2016 5:45 PM
  • I figured this out while brainstorming over my lunch break. So simple.

    I had to add another control group to the screen. I have one that is the details picker, and another that is a rows layout that lists the columns I wanted.

    So easy. Lesson learned.

    Friday, January 15, 2016 6:45 PM