user2162331
user2162331

Reputation: 99

LightSwitch HTML Client save calculated results to new field

I'm using Visual Studio 2013 LightSwitch to attempt to build an HTML application. After trying for several hours to get a "computed" column/field to show up in the HTML client, I realized it's not possible. I thought about building a trigger to fire on the table, but that won't get pushed when I publish the application. So, without using a RIA client, what's the best way to calculate using two or more datasource fields and store in another? It can be triggered when the user clicks the save button or even when the fields contain data that can be computed.

For example, Table Users (before):

First_Name (string)   Last_Name (string)   Stored_Full_Name (string)
John                  User                 NULL
Jane                  Doe                  NULL

When the user clicks save, or when first_name and last_name are not null, "Last_Name, First_Name" is stored as a single column in the Stored_Full_Name column in the same table.

For example, Table Users (after):

First_Name (string)   Last_Name (string)   Stored_Full_Name (string)
John                  User                 User, John
Jane                  Doe                  Doe, Jane

I'm assuming the same technique could be used to calculate costs or other items?

Any example code would be greatly appreciated.

Upvotes: 0

Views: 1389

Answers (2)

Ozziemedes
Ozziemedes

Reputation: 311

This is pretty easy. Create a string-typed screen variable called "FullName" with display text of "Full Name". Edit the Screen's "create" method and assign the value you want to the variable in JavaScript. - in the example above you'd want something like the following:

screen.FullName = screen.Last_Name.value + ", " + screen.First_name.value;

Then drag the variable from the data tab on the left onto your screen into your Users table.

You might need to update the computed value as part of your Save pipeline, but the create() method should fire any time the screen is redrawn. If you want to save this into a nullable column within your Users table you can do so by simply assigning the field value to the new value, but I'm not sure why you'd bother as this breaks your table's normalization. In the function where you pre-process saved changes on the client-side, add the following code to make this so anyway:

screen.FullNameTableField = screen.FullName.value;

I use a very similar technique for creating in-row "Edit" and "Delete" record-level table entries.

HTH

Upvotes: 1

Chielus
Chielus

Reputation: 632

For simple computed properties, you can always create functions on the Users prototype. It's explained in this blog post: http://lobfactory.wordpress.com/2013/05/03/computed-fields-in-the-html5-client/

Upvotes: 1

Related Questions