LightSwitch 问题总结

1.在浏览界面想修改数据并保存提交,刷新该页?

msls.showMessageBox("确定该采购订单已完成?", {
        title: "完成订单",
        buttons: msls.MessageBoxButtons.yesNo
    }).then(function (result) {//选择yes执行
        if (result == msls.MessageBoxResult.yes) {
            screen.PurchaseOrderInfo.BillState = "2";
            myapp.applyChanges().then(function () {//保存成功执行
                
                //window.location.reload();// 刷新页面 会刷新default.htm js变量清空
         myapp.showViewPurchaseOrderItem(screen.PurchaseOrderInfo);//调用显示该页,页面对象赋值

            });
        }
    });

2.下拉列表给页面控件赋值?

//通过下拉列表选择仓库 给界面赋值
    var comStorage = screen.findContentItem("ListBoxStorage");
    var lblStorId = screen.findContentItem("StorId");
    var lblStorName = screen.findContentItem("StorName");
    comStorage.dataBind("value", function (newValue) {
        if (newValue !== undefined && newValue !== null) {
            lblStorId.stringValue = newValue.StorId;
            lblStorName.stringValue = newValue.StorName;
        }
    });

3.密码框?

var txtPwd;
myapp.LoginScreen.created = function (screen) {
    // 获取自己添加的自定义控件.
    txtPwd = screen.findContentItem("ParamPwdCstuom");

};
//自定义控件的render代码
myapp.LoginScreen.ParamPwdCstuom_render
= function (element, contentItem) { // 密码框的HTML代码. var pwdComponent = $('<input id="txt_pwd" type="password" />'); // 用密码框的HTML代码 添加到自定义控件中
pwdComponent.appendTo($(element)); pwdComponent.change(
function () { var text = $(this).val();
     //由于自定义控件的数据绑定到某参数,所以要将input中的值赋值到自定义控件中 txtPwd.stringValue
= text; }); pwdComponent.keydown(function () { // 添加回车触发事件  
if (event.keyCode == "13") { var text = $(this).val(); txtPwd.stringValue = text; } }); };

 4.不在实体间建立关系,但是需要在客户端级联显示下拉列表?

由于LightSwitch是在服务端返回的所有数据中筛选显示和操作,所以指定参数的调用都是通过查询完成的。

级联查询简单来说就是通过查询实现,自动赋值给查询字段,然后结果集以添加数据项的形式显示,并将数据项的自动填充选项绑定到查询结果集,将查询结果集的参数绑定到要赋值的控件或者实体属性。具体请参阅LightSwitch如何创建级联下拉列表

 5.返回主页

myapp.navigateHome();

此方法可以在自定义方法中调用,目前发现在create中使用无效,初步判断是在页面生成过程中,不能导航。

 6.LightSwitch中,主表A 明细表B ,修改明细表B不会触发表A的更新和验证事件,只需要给某个字段重新赋值即可将该表标记为已修改。

 7.JS创建数据 ,触发LS Server端的SaveChange事件

screen.MyConfigData = new myapp.MyConfigData();
    screen.MyConfigData.Id = "1";
    screen.MyConfigData.SupplFullName = "fullname";
    screen.MyConfigData.SupplShortName = "shortname";
    myapp.applyChanges().then(function () {

        //window.location.reload();
    });

 8.在LightSwitch中自定义变量

  ①.在default.htm也中定义  

var globalSuppl = {
            ID: "",//供应商编号
            Name: "",//供应商名称
            ShortName: "",//供应商简称
            Clear: function () {
                this.ID = "";
                this.Name = "";
                this.ShortName = "";
            }
        };

  ②.在Screen页中定义  

        myapp.myState = { id: "2222" };
        myapp.myState.id;

9.避免浏览器后退按钮后退到修改前的页面

场景:修改界面,保存成功后,点击浏览器后退按钮可以回到修改前状态。

解决方法:

//showViewPurchaseOrderItem为当前页显示方法,用时自己替换
myapp.showViewPurchaseOrderItem(screen.PurchaseOrderInfo);

保存成功后,自动调用上述语句,再次点击后退按钮,同样回到该页面,页面数据是修改后的。

 10.删除操作

添加一个自定义按钮,编辑自定义方法的执行代码

myapp.AddEditOrderDetail.Delete_execute = function (screen) {
    screen.OrderDetail.deleteEntity();
    return myapp.commitChanges().then(null, function fail(e) {
        myapp.cancelChanges();
        throw e;
    });
};

11.获取页面数据条数

myapp.BrowsePurchaseOrder.created = function (screen) {        
//PurchaseOrderData Server端订单数据名称
//QueryNotCompletedSupplOrder 自定义的订单查询
myapp.activeDataWorkspace.PurchaseOrderData.QueryNotCompletedSupplOrder(globalSuppl.ID).execute().then(function (results) {
        var TotalCountOfOrders = CountOrders(results);
        //TotalOrdersCount 自添加的数据项-本地属性名称
        screen.TotalOrdersCount = TotalCountOfOrders.toString();

    });

};
//计算订单个数
function CountOrders(Orders) {
    var TotalOrders = 0;
    var orders = Orders.results;
    orders.forEach(function (order) {
        TotalOrders = TotalOrders + 1;
    });
    return TotalOrders;

}

 12.显示Label,自定义样式

可以替换掉自定义控件的Html

$(element).parent().html("<b style='color:blue;font-size:large'>标题</b>");

 13.自定义方法触发页面验证

 //WinJs 中的 then(Function onComplete, [Function onError], [Function onProgress])
 myapp.applyChanges().then(function () {
        // all validation is satisfied
        // 所有的验证均通过
        msls.showMessageBox("Successfully Registered.", {
            title: "Successfully Registered",
            buttons: msls.MessageBoxButtons.ok
        }).then(function (result) {
            if (result === msls.MessageBoxResult.ok) {
                myapp.showHome()
            }
        });

    }, function fail(e) {
        // validation is not satisfied. Show validation messages
        // If error occurs, show the error.
        msls.showMessageBox(e.message, { title: e.title }).then(function () {
            // Discard Changes
            screen.details.dataWorkspace.ApplicationData.details.discardChanges();

        });
    });
  

 14.Integer型主键不能等于0

原文地址:https://www.cnblogs.com/wangning-aaron/p/4036523.html