locked
Do something based on list selection RRS feed

  • Question

  • Hi,

    I have a custom visual webart with a button. On the same site I have a xlstlistviewwebpart showing some list's content. Now I have enable the button when anything is selected from the list... Similarly I need to disable it, when nothing is selected. What is the best way to do it


    Regards 


    Thursday, December 8, 2011 10:23 AM

Answers

  • Found a solution:

    $(".s4-itm-cbx").bind("click", function () {
    
        var buttonsShouldBeEnabled = false;
    
        $(".s4-itm-cbx").each(function () {
            if (this.checked) {
    
                buttonsShouldBeEnabled = true;
            }
        }
        );
    
        var allInput = document.getElementsByTagName('input');
        var buttonBd = null;
        var buttonGoHub = null;
    
        for (i = 0; i < allInput.length; i++) {
            if (allInput[i].id.endsWith('btnGoBd')) {
                buttonBd = allInput[i];
            }
    
            if (allInput[i].id.endsWith('btnGoHub')) {
                buttonGoHub = allInput[i];
            }
    
            if (buttonBd != null && buttonGoHub != null) {
                break;
            }
        }
    
        if (buttonsShouldBeEnabled) {
            if (buttonBd != null) {
                buttonBd.disabled = false;
            }
    
            if (buttonGoHub != null) {
                buttonGoHub.disabled = false;
            }
        }
        else {
            if (buttonBd != null) {
                buttonBd.disabled = true;
            }
    
            if (buttonGoHub != null) {
                buttonGoHub.disabled = true;
            }
        }
    
    });
    
    String.prototype.startsWith = function (str) {
        return (this.match("^" + str) == str);
    };
    
    String.prototype.endsWith = function (pattern) {
        var d = this.length - pattern.length;
        return d >= 0 && this.lastIndexOf(pattern) === d;
    };
    
    

    • Proposed as answer by pbojkowski Friday, December 9, 2011 2:31 AM
    • Marked as answer by Qiao Wei Thursday, December 15, 2011 12:01 PM
    Friday, December 9, 2011 2:30 AM

All replies

  • Does the SharePoint API at all support this use case?
    Thursday, December 8, 2011 1:38 PM
  • You can easily achieve this by using JQuery or javascript.

    By using jquery, write  a function, when ever the item checkbox is selected, enable the button on another webpart. By defualt, you can disable that button.


    Regards,
    Shantha Kumar .T | MCPD - SharePoint Developer 2010 | MCITP - SharePoint Administrator 2010
    (B) Shantha Kumar's Blog | (T)Follow me
    Thursday, December 8, 2011 5:11 PM
  • Exists any sample that works with xlstlistviewwebpart?
    Thursday, December 8, 2011 8:50 PM
  • Found a solution:

    $(".s4-itm-cbx").bind("click", function () {
    
        var buttonsShouldBeEnabled = false;
    
        $(".s4-itm-cbx").each(function () {
            if (this.checked) {
    
                buttonsShouldBeEnabled = true;
            }
        }
        );
    
        var allInput = document.getElementsByTagName('input');
        var buttonBd = null;
        var buttonGoHub = null;
    
        for (i = 0; i < allInput.length; i++) {
            if (allInput[i].id.endsWith('btnGoBd')) {
                buttonBd = allInput[i];
            }
    
            if (allInput[i].id.endsWith('btnGoHub')) {
                buttonGoHub = allInput[i];
            }
    
            if (buttonBd != null && buttonGoHub != null) {
                break;
            }
        }
    
        if (buttonsShouldBeEnabled) {
            if (buttonBd != null) {
                buttonBd.disabled = false;
            }
    
            if (buttonGoHub != null) {
                buttonGoHub.disabled = false;
            }
        }
        else {
            if (buttonBd != null) {
                buttonBd.disabled = true;
            }
    
            if (buttonGoHub != null) {
                buttonGoHub.disabled = true;
            }
        }
    
    });
    
    String.prototype.startsWith = function (str) {
        return (this.match("^" + str) == str);
    };
    
    String.prototype.endsWith = function (pattern) {
        var d = this.length - pattern.length;
        return d >= 0 && this.lastIndexOf(pattern) === d;
    };
    
    

    • Proposed as answer by pbojkowski Friday, December 9, 2011 2:31 AM
    • Marked as answer by Qiao Wei Thursday, December 15, 2011 12:01 PM
    Friday, December 9, 2011 2:30 AM