none
Enable/Disable screen button based on criteria for currently selected list item RRS feed

  • Question

  • I've spent all day searching for the answer to this one and have been tearing what few hairs I have left out!  LightSwitch SL client is so easy to use, but I'm finding the HTML stuff to be very frustrating!

    I have a Lightswitch HTML Client app that will be used for our data entry staff to create qualification codes for use in our main student records system.  I also want to allow them to edit the details of the qualification, but only if the record is not being used in the main database.

    That's all fine - I've written the code to check whether it can be edited or not using a simple Web API call that returns true or false, and it seems to work fine.  What I can't seem to get working is the _canExecute code for the Edit button.  I would like it so that when a record in the list is selected, the application checks the selected record against the database table to see if the qualification is being used, and if it is not being used then enable the button, otherwise disable it.

    Please help me before I go completely bald!!!

    Thursday, April 25, 2013 2:21 PM

Answers

  • Hi,

    Here's my code to enable a button for a selected Northwind Customer if the Customer Country is US.

    myapp.BrowseCustomers.Method_canExecute = function (screen) {
        return screen.Customers.selectedItem &&
            screen.Customers.selectedItem.Country === "US";
    };

    So for your screen I think it can be something like

    myapp.BrowseMyEntities.MyMethod_canExecute = function (screen) {
        return screen.MyEntities.selectedItem &&
            invokeAPIAndCheck(screen.MyEntities.selectedItem);
    };

    Best regards,
    Huy Nguyen
    • Marked as answer by rokushakubo Friday, April 26, 2013 9:07 AM
    Thursday, April 25, 2013 8:01 PM

All replies

  • Hi,

    Here's my code to enable a button for a selected Northwind Customer if the Customer Country is US.

    myapp.BrowseCustomers.Method_canExecute = function (screen) {
        return screen.Customers.selectedItem &&
            screen.Customers.selectedItem.Country === "US";
    };

    So for your screen I think it can be something like

    myapp.BrowseMyEntities.MyMethod_canExecute = function (screen) {
        return screen.MyEntities.selectedItem &&
            invokeAPIAndCheck(screen.MyEntities.selectedItem);
    };

    Best regards,
    Huy Nguyen
    • Marked as answer by rokushakubo Friday, April 26, 2013 9:07 AM
    Thursday, April 25, 2013 8:01 PM
  • Thanks Huy.

    I eventually got it working but I had to use the _created method on the screen and the .isEnabled property of the button:

        screen.findContentItem("EditQualAimMethod").isEnabled = false;
        screen.FilteredLastQuals.addChangeListener("selectedItem", function () {
            var s = screen.FilteredLastQuals.selectedItem;
            var url = "../api/QualAims/" + s.QUAL_CODE + "/" + s.FUNDING_YEAR;
            $.getJSON(url, function (data) {
                if (data) {
                    screen.findContentItem("EditQualAimMethod").isEnabled = data;
                } else {
                    screen.findContentItem("EditQualAimMethod").isEnabled = false;
                }
                
            });
        });
      

    Gareth


    Friday, April 26, 2013 9:03 AM