SAC scripting for Toggle button

SAC scripting for Toggle button

Show/hide table tab on switch button toggle:

//on switch button

if (SWH_TogglePCodes.isOn()){
TABS_IS.setSelectedKey("With_Exclusion");
Application.showMessage(ApplicationMessageType.Info,"IPR is excluded");
} else {
TABS_IS.setSelectedKey("Without_Exclusion");
Application.showMessage(ApplicationMessageType.Info,"IPR is included");
}

Code explanation:
यह JavaScript / SAP Analytics Cloud (SAC) script Toggle Button के आधार पर Tab Selection और Message Display को control करने के लिए उपयोग की जाती है। यह code खास तौर पर SAC Application Designer, User Interaction Control और Dynamic Dashboard Behavior के लिए बहुत उपयोगी है।

इस code में SWH_TogglePCodes.isOn() एक toggle switch की स्थिति check करता है। अगर toggle ON होता है, तो TABS_IS.setSelectedKey("With_Exclusion") के जरिए TabStrip का “With_Exclusion” tab अपने आप select हो जाता है। इसके साथ ही Application.showMessage() का उपयोग करके user को एक information message दिखाया जाता है, जिसमें बताया जाता है कि IPR excluded है। इससे user को तुरंत पता चल जाता है कि system किस mode में काम कर रहा है।

अगर toggle OFF होता है, तो else block run होता है। इस स्थिति में “Without_Exclusion” tab select किया जाता है और एक अलग message दिखाया जाता है, जो बताता है कि IPR included है। इस तरह एक ही toggle से dashboard का view और logic दोनों control हो जाते हैं।


Filter to Exclude Product codes 1:

//apply on switch button

var ExcludePCodes = ArrayUtils.create(Type.string);
//Current set of p-codes needed to exclude/include - 5000,5001,72,72A,72CL,72I,72NW
ExcludePCodes = ["5000","5001","0072","72A","72CL","72NW","72I"];
if(Switch_1.isOn()===true)
{
// Get selected members from Input Control
// Apply background filter to Table
console.log(ExcludePCodes);

Table_30.getDataSource().removeDimensionFilter("CFIREPCOD");
Table_30.getDataSource().setHierarchy("CFIREPCOD",'__FLAT__');
Table_30.getDataSource().setDimensionFilter("CFIREPCOD",{values: ExcludePCodes, exclude: true});

Table_48.getDataSource().removeDimensionFilter("CFIREPCOD");
Table_48.getDataSource().setHierarchy("CFIREPCOD",'__FLAT__');
Table_48.getDataSource().setDimensionFilter("CFIREPCOD",{values: ExcludePCodes, exclude: true});

Table_49.getDataSource().removeDimensionFilter("CFIREPCOD");
Table_49.getDataSource().setHierarchy("CFIREPCOD",'__FLAT__');
Table_49.getDataSource().setDimensionFilter("CFIREPCOD",{values: ExcludePCodes, exclude: true});

Table_50.getDataSource().removeDimensionFilter("CFIREPCOD");
Table_50.getDataSource().setHierarchy("CFIREPCOD",'__FLAT__');
Table_50.getDataSource().setDimensionFilter("CFIREPCOD",{values: ExcludePCodes, exclude: true});

console.log("True");
}
else
{
Table_30.getDataSource().removeDimensionFilter("CFIREPCOD");
//Table_30.getDataSource().refreshData();

Table_48.getDataSource().removeDimensionFilter("CFIREPCOD");
//Table_48.getDataSource().refreshData();

Table_49.getDataSource().removeDimensionFilter("CFIREPCOD");
//Table_49.getDataSource().refreshData();

Table_50.getDataSource().removeDimensionFilter("CFIREPCOD");
//Table_50.getDataSource().refreshData();

console.log("False");
}

Code explanation:
यह SAP Analytics Cloud (SAC) Application Designer की script Switch Button के आधार पर Table Filters को Dynamically Apply या Remove करने के लिए उपयोग की जाती है। यह code खास तौर पर तब काम आता है जब business user को एक ही switch से कुछ P-Codes को Exclude या Include करना हो।

इस script में सबसे पहले ExcludePCodes नाम का एक String Array बनाया गया है, जिसमें वे सभी P-Codes रखे गए हैं जिन्हें report से हटाना है, जैसे 5000, 5001, 0072, 72A आदि। इसके बाद Switch_1.isOn() की मदद से यह check किया जाता है कि switch ON है या OFF।

अगर switch ON होता है, तो CFIREPCOD dimension पर पहले से लगे सभी filter हटाए जाते हैं। फिर Hierarchy को __FLAT__ set किया जाता है ताकि सभी code एक level पर दिखें। इसके बाद setDimensionFilter() का उपयोग करके दिए गए P-Codes को exclude: true के साथ filter किया जाता है। यह logic चार अलग-अलग tables (Table_30, Table_48, Table_49, Table_50) पर एक साथ लागू होता है।

अगर switch OFF होता है, तो सभी tables से CFIREPCOD का filter हटा दिया जाता है और पूरा data दोबारा दिखने लगता है।


Filter to Exclude Product codes 2:

//apply on switch button

// write below code on switch to call method:
Methods.FilterPCodes([Table_48, Table_30, Table_49, Table_50],"CFIREPCOD",Switch_1.isOn());

// create a method & write below code in it to filter in/filter out:
var ExcludePCodes = ArrayUtils.create(Type.string);

//Current set of p-codes needed to exclude/include - 5000,5001,72,72A,72CL,72I,72NW
ExcludePCodes = ["5000","5001","72","72A","72CL","72NW","72I"];

/*
var counter = 0;
var bool = 0;
if(gNewPCodes.length<1){
for (var l=0; l<gPCodes.length;l++){
bool = 0;
for (var k=0; k<ExcludePCodes.length; k++){
if ( (ExcludePCodes[k] !== gPCodes[l]) ){
bool = bool+1;
}
}
if (bool === ExcludePCodes.length){ 
gNewPCodes[counter] = gPCodes[l];
counter++; }
}
}
console.log(gNewPCodes);
*/

var len = TableNames.length;

if (OnOrOff){
//console.log("true");
for(var i=0;i<len;i++){
TableNames[i].getDataSource().setHierarchy(DimensionName,'__FLAT__');
TableNames[i].getDataSource().setDimensionFilter(DimensionName,{values: ExcludePCodes, exclude: true});
}
Application.showMessage(ApplicationMessageType.Info,"This might take some time as P-Codes: \"5000,5001,72,72A,72CL,72NW\" are being excluded from the data.");
} else {
//console.log("false");
for(var j=0;j<len;j++){
TableNames[j].getDataSource().removeDimensionFilter(DimensionName);
}
Application.showMessage(ApplicationMessageType.Info,"All P-Codes are now included in the data.");
}

Code explanation:
यह SAP Analytics Cloud (SAC) Application Designer की advanced scripting का उदाहरण है, जिसमें Switch Button के ज़रिए multiple tables पर एक common filter method लागू किया गया है। इस code का मुख्य उद्देश्य है P-Codes को एक साथ Exclude या Include करना, बिना हर table पर अलग-अलग script लिखे।

सबसे पहले Switch Button पर एक single line लिखी जाती है:
Methods.FilterPCodes([Table_48, Table_30, Table_49, Table_50], "CFIREPCOD", Switch_1.isOn());
यह line एक custom method को call करती है, जिसमें tables की list, dimension name और switch की ON/OFF value भेजी जाती है। इससे code reusable और maintainable बनता है।

Method के अंदर ExcludePCodes नाम का string array बनाया गया है, जिसमें वे सभी P-Codes हैं जिन्हें report से हटाना है, जैसे 5000, 5001, 72, 72A, 72CL आदि। जब Switch ON होता है, तब सभी tables के Data Source पर __FLAT__ hierarchy set की जाती है और setDimensionFilter() के ज़रिए इन P-Codes को exclude: true के साथ filter किया जाता है।

जब Switch OFF होता है, तब removeDimensionFilter() का उपयोग करके सभी tables से filter हटा दिया जाता है और पूरा data दोबारा दिखने लगता है। साथ ही Application.showMessage() से user को जानकारी भी दी जाती है।


Show / Hide Widget

if (Switch_1.isOn()) {
Table_1.setVisible(true);
} else {
Table_1.setVisible(false);

Code explanation:
यह SAC scripting code एक switch button का उपयोग करके table की visibility को नियंत्रित करता है। जब switch (Switch_1) ON होता

है, तो Table_1.setVisible(true) command table को users के लिए visible बनाता है। जब switch OFF होता है, तो Table_1.setVisible(false) table को view से छिपा देता है। यह simple conditional logic users को dashboard elements को interactive रूप से दिखाने या छिपाने की अनुमति देता है, जिससे interface clean और focused रहता है। यह विशेष रूप से तब उपयोगी होता है जब users specific components, जैसे detailed data tables को केवल आवश्यकता पड़ने पर ही देखना चाहते हैं—SAP Analytics Cloud applications में user experience और visual clarity दोनों को बेहतर बनाता है।


Apply / Remove Filter

var ds = Table_1.getDataSource();
if (Switch_1.isOn()) {
ds.setDimensionFilter("Country", ["India"]);
} else {
ds.removeDimensionFilter("Country");
}
Application.refreshData();

Code explanation:
यह SAC scripting code switch button की स्थिति के आधार पर किसी table पर data filter लागू या हटाता है। ds variable, getDataSource()

का उपयोग करके Table_1 के data source को पुनः प्राप्त करता है। यदि switch (Switch_1) ON है, तो "Country" dimension पर एक filter लागू होता है, जो केवल "India" का data दिखाता है। यदि switch OFF है, तो filter हटा दिया जाता है, और सभी देशों को फिर से display करता है। अंत में, Application.refreshData() latest filter changes को दर्शाने के लिए table को refresh करता है। यह interactive logic users को filter को आसानी से ON और OFF करने की अनुमति देता है, जिससे SAP Analytics Cloud dashboard में data analysis का flexibility बेहतर होता है।


Enable / Disable User Input

var editable = Switch_1.isOn(); InputField_1.setEnabled(editable); InputField_2.setEnabled(editable);

Code explanation:
यह SAC scripting code switch button की स्थिति के आधार पर input field को editable योग्य बनाता है या नहीं, इसे नियंत्रित करता है।

"editable" variable, isOn() method का उपयोग करके Switch_1 की ON/OFF state को store करता है। यदि switch ON (true) है, तो InputField_1 और InputField_2 दोनों enabled हो जाते हैं, जिससे users value type या modify कर सकते हैं। यदि switch OFF (false) है, तो input field enabled हो जाते हैं, जिससे कोई भी edit नहीं हो पाता। यह approach data entry method—जैसे "View" और "Edit" स्थितियों के बीच switch करने—को नियंत्रित करने के लिए उपयोगी है, जिससे यह सुनिश्चित होता है कि users केवल तभी input modify कर सकते हैं जब स्पष्ट रूप से अनुमति दी गई हो, जिससे SAC applications में security और data consistency बेहतर होती है।


Toggle Application Theme

if (Switch_1.isOn()) {
Application.setTheme("sap_fiori_3_dark");
} else {
Application.setTheme("sap_fiori_3");
}

Code explanation:
इस SAC scripting code का उपयोग किसी application में switch button का उपयोग करके light और dark रंग की theme के बीच switch करने

के लिए किया जाता है। जब switch (Switch_1) ON होता है, तो application theme "sap_fiori_3_dark" में बदल जाती है, जिससे बेहतर visibility या aesthetic संबंधी preference के लिए dark mode enable हो जाता है। जब switch OFF होता है, तो यह default "sap_fiori_3" light रंग की theme पर वापस आ जाता है। Application.setTheme() function page को reload किए बिना selected theme को dynamic रूप से लागू करता है। यह सुविधा SAP Analytics Cloud में dashboard को different viewing conditions या users preference के अनुकूल बनाकर, personalization की अनुमति देकर users experience को बेहतर बनाती है।


Refresh Data Manually

if (Switch_1.isOn()) {
Application.showBusyIndicator("Refreshing data...");
Application.refreshData();
Application.hideBusyIndicator();
Switch_1.setOn(false); // Reset to OFF
}

Code explanation:
यह SAC scripting code data को manual रूप से refresh करने के लिए एक switch button का उपयोग करता है। जब switch (Switch_1)

ON होता है, तो script सबसे पहले Application.showBusyIndicator() का उपयोग करके एक busy indicator message "Refreshing data..." display करती है ताकि users को सूचित किया जा सके कि processing जारी है। फिर, यह Application.refreshData() के साथ application के सभी data sources को refresh करता है। Refresh पूरा होने के बाद, busy indicator को Application.hideBusyIndicator() का उपयोग करके छिपा दिया जाता है। अंत में, Switch_1.setOn(false) के साथ switch को OFF कर दिया जाता है ताकि वह active न रहे। यह SAC application के भीतर data को interactive रूप से refresh करने के लिए एक clear, user-friendly प्रक्रिया बनाता है।


Control Visibility of Multiple Widgets

var state = Switch_1.isOn();
Chart_1.setVisible(state);
Table_1.setVisible(!state);

Code explanation:
यह SAC scripting code switch button की स्थिति के आधार पर दो widget—Chart_1 और Table_1—की visiblity को नियंत्रित करता है।

Variable state, isOn() method का उपयोग करके Switch_1 की ON/OFF condition को store करती है। यदि switch ON (true) है, तो Chart_1 visible हो जाता है और Table_1 छिप जाता है। यदि switch OFF (true) है, तो chart छिप जाता है और table दिखाई जाती है। यह simple logic users को एक ही switch का उपयोग करके विभिन्न data views (उदाहरण के लिए, chart view vs table view) के बीच toggle करने की अनुमति देता है, जिससे SAP Analytics Cloud में एक interactive और user-friendly dashboard अनुभव बनता है।


Conditional Action

if (Switch_1.isOn()) {
    Application.showMessage(ApplicationMessageType.Info, "Alert Mode Activated!"); } else {
    Application.showMessage(ApplicationMessageType.Info, "Alert Mode Deactivated!");
}

Code explanation:
यह SAC scripting code switch button की स्थिति के आधार पर एक information message प्रदर्शित करता है। जब switch (Switch_1)

ON होता है, तो यह "Alert Mode Activated!" संदेश display करता है, जो दर्शाता है कि alert mode enabled है। जब switch OFF होता है, तो यह "Alert Mode Deactivated!" display करता है, जो दर्शाता है कि alert mode disabled कर दिया गया है। Screen पर user-friendly notification display करने के लिए Application.showMessage() function का उपयोग ApplicationMessageType.Info parameters के साथ किया जाता है। यह script users को immediate feedback प्रदान करती है, जिससे उन applications में interactivity और clarity में सुधार होता है जहाँ alert या monitoring mode dynamic रूप से toggle किए जाते हैं।


Multi-Action Switch

if (Switch_1.isOn()) {
Application.showBusyIndicator("Switching to Advanced Mode...");
Chart_1.setVisible(true);
Table_1.setVisible(true);
Application.hideBusyIndicator();
} else {
Chart_1.setVisible(false);
Table_1.setVisible(false);
}

Code explanation:
यह SAC scripting code एक switch button का उपयोग करके chart और table की visibility को नियंत्रित करता है। जब switch (Switch_1)

ON होता है, तो system एक busy indicator संदेश "Switching to Advanced Mode..." display करता है, Chart_1 और Table_1 को visible बनाता है, और फिर प्रक्रिया पूरी होने पर busy indicator को छिपा देता है। जब switch OFF होता है, तो chart और table दोनों छिप जाते हैं। यह logic users को basic और advanced dashboard modes के बीच आसानी से toggle करने में मदद करता है, जिससे एक interactive अनुभव मिलता है और visibility change process के दौरान बेहतर user feedback process के लिए busy indicator का उपयोग करते हुए एक smooth transition बनाए रखा जाता है।

एक टिप्पणी भेजें

Please do not enter any spam link in the comment box.

और नया पुराने