Different types of forms displayed together with a sole submission button in a separate section

https://i.sstatic.net/aAbD9.jpg

My webpage consists of two main sections: the red section which is a partial view containing actions for forms, and the blue section which contains tabbed forms. Each tab in the blue section is also a view, called using the kendo ajax tabstrip tool. The code looks like this:

 @(Html.Kendo().TabStrip()
            .Name("tabstrip")
            .Animation(animation =>
            {
                animation.Enable(false);
            })
            .SelectedIndex(0)
            .Items(tabstrip =>
            { 
              tabstrip.Add().Text("Customer Info")
                    .LoadContentFrom(Url.Content("CustomerInfo"));
                tabstrip.Add().Text("Customer Address")
                    .LoadContentFrom(Url.Content("CustomerAddress"));
                tabstrip.Add().Text("Customer Payment")
                    .LoadContentFrom(Url.Content("CustomerPayment"));
                tabstrip.Add().Text("Identity")
                    .LoadContentFrom(Url.Content("Identity"));
              })
)

For saving actions, I have a generic JavaScript method named SaveRecord in the root script file. In each tab's view, I call the click event by using the generic SaveRecord method as follows:

PartialButtonView

<input title="Save" type="button" value="Save" onclick="toolbarSaveAction();"  class="toolbarButton toolbarBtnMarginLeft" />

Root.js

function SaveRecord(action, controller, param, customData) {

    var form = $("[aria-expanded=true]").find("form");
    var validator = form.kendoValidator().data("kendoValidator");

    if (validator.validate()) {

        var data = form.serialize();

        $.ajax({
            url: '/' + controller + '/' + action,
            dataType: 'json',
            type: 'POST',
            data: data,
            success: function (response) {

                if (response !== null && !response.success) {

                    ..

                }
                else {
                         ..
                }

            },
            error: function (xhr, ajaxOptions, thrownError) {

                ..

            }
        });
    }
}

CustomerInfo View

<script>
        function toolbarSaveAction() {
            SaveRecord('CustomerInfo', 'Customer', ['customerId']);
        }
</script>

CustomerAddress View

<script>
        function toolbarSaveAction() {
            SaveRecord('CustomerAddress', 'Customer', ['customerId']);
        }
</script>

The issue I'm facing is that for each tab view, there is a toolbarSaveAction() method for the save click event. When switching between tabs, multiple views are loaded on the page, resulting in multiple toolbarSaveAction() methods. How can I ensure that the correct method is picked when clicking the save action?

Answer №1

Keep a hidden variable in the DOM and update it with the current tab's text when a tab is selected using Kendo

<input type="hidden" name="hdnSelectedTab" id="hdnSelectedTab"/>

Then in JavaScript

  $(document).ready(function() {
                function onSelect(e) {
                   $('#hdnSelectedTab').val($(e.item).find("> .k-link").text());
                }
         });

Now, have only one action as toolbarSaveAction

<script>
    function toolbarSaveAction(selectedTab) {
        SaveRecord('CustomerInfo', 'Customer', ['customerId'],selectedTab);
    }
</script>

Pass the tab value from the button like this:

<input title="Save" type="button" value="Save" onclick="toolbarSaveAction($('#hdnSelectedTab').val());"  class="toolbarButton toolbarBtnMarginLeft" />

Finally, in the function SaveRecord:

function SaveRecord(action, controller, param, customData,selectedTab) {
   switch(selectedTab) {
     case "Customer-Info":
                  // Implementation    
                   break;
     case "Identity":
                  // Implementation    
                   break;
     default:
            // Default implementation    
      }

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking to convert this single object into an array of objects within VueJS

So, I've encountered a bit of a pickle with the data from an endpoint that I need to format for a menu project I'm working on. It's all jumbled up and not making much sense right now. Any assistance would be greatly appreciated! This is the ...

Enhancing Slider Appearance in Material-UI (React)

I am currently working on a project that involves creating a slider with specific values using Material UI and React. I followed the basic implementation from the documentation, which seems to work without needing any additional CSS. However, when I integr ...

What is the process for generating Json using information from a form?

Recently, I embarked on a project that involves creating online tours via a form filled out by the administrator. The data submitted through the form is then mapped into a Mongoose Schema and transformed into JSON. In the createcontent.js file, I utilized ...

Press `Enter` to confirm your selection in the BootstrapVue message box input box

Using Vue version v2.6.12 BootstrapVue version v2.21.2 Is there a way to confirm by pressing Enter instead of manually clicking OK? let text this.$bvModal.msgBoxConfirm(<input vModel={text} />) https://i.sstatic.net/7XxOl.png ...

What is the best way to adjust the placement of a component to remain in sync with the v-model it is connected to?

I am encountering an issue with 2 sliders in my project. I have set it up so that when the lower slider's value is greater than 0, the top slider should automatically be set to 5. I am using a watcher function for this purpose. However, if I manually ...

Maximizing efficiency in JavaScript by utilizing jQuery function chaining with deferred, the .done() function

I am working on retrieving data from multiple functions and want to chain them together so that the final function is only executed when all the data has been successfully loaded. My issue arises when trying to use the .done() method, as it calls the func ...

Developing TypeScript applications often involves using JavaScript callbacks in order

I'm encountering some challenges implementing a JavaScript callback with namespace in a TypeScript file. I initially believed that I could directly copy JavaScript code into TypeScript, but Visual Studio's compiler is throwing multiple errors. D ...

Hiding and showing div elements using CSS, JavaScript, and PHP

Here is the current code snippet: <? while ($row1 = mysql_fetch_object($result1)) { echo '<a href="#" onclick="showhide("'.$row1->id.'");">Name</a>'; while ($row2 = mysql_fetch_object($result2)) { ...

Calendar: Display upcoming dates within the next week starting from the current week

Hey there! I have a calendar that includes next and previous buttons. When the user clicks on the next button, the schedule for the upcoming week will be displayed. However, if the user clicks again, nothing happens. My goal is to only show dates for the n ...

Is there a way to dynamically refresh the Bing web API search results on a React.js application after entering a search query in the search bar (in the SER

I've been using the Bing Web Search API to access data by typing a query into a search bar I created. However, I've been facing an issue where the data isn't displaying in the search results when I submit the query. I'm specifically try ...

The div on my webpage is not loading its content as expected when using JavaScript

I am having trouble continuously loading the content of a div from another page. Using alert worked fine, but the page data is not loading. JavaScript <script> $(document).ready(function(){ setInterval(function(){$("#loadAvailable").load("update.p ...

Removing elements in AngularJS using ngRepeat

Many have questioned how to implement item removal within the ngRepeat directive. Through my research, I discovered that it involves using ngClick to trigger a removal function with the item's $index. However, I haven't been able to find an exam ...

Is it possible for a jQuery selector to retain its object? What can be done to prevent it

One interesting scenario involves a dropdown element within a container. <div class='container' /> <script> var dropdown = "<select class='multi-dropdown'> ... </select>" </script> Every time the value ...

Isolating an array from an object?

I am working with a component that receives props: The data received is logged on the console. https://i.sstatic.net/F3Va4.png What is the best way to extract the array from this object? Before I pass the array to my component, it appears like this: h ...

Unraveling the Perfect Jest Stack Trace

Currently, I am in the process of debugging some tests that were written with jest using typescript and it's causing quite a headache. Whenever a test or tested class runs Postgres SQL and encounters an error in the query, the stack trace provided is ...

Step-by-step guide on building a route map using ngRoute in AngularJS

I am in the process of developing a web application similar to gdrive or dropbox. As I navigate through my folders via links, I require the path to be displayed in the URL. I want to update the path from the client side using AngularJS. For example: $st ...

Having trouble connecting to my MongoDB container using Node.js

I am facing an issue while trying to connect my local mongoDB docker, named "some-mongo", to my NodeJS backend server running on the same computer. The problem arises when attempting to establish the connection using the "mongoose" module. To launch my mo ...

Call a React component from an external JavaScript file

I am working on a react application that has a bundle.js file created using webpack containing all the necessary code. Recently, I started developing a separate dotnet application and I need to display the main 'App' component from my react appl ...

Troubleshooting JavaScript If-Else Statements

Having a bit of trouble with my if else structure. When I enter the correct star name like "Vega", it incorrectly shows me "Error" instead of the expected result "Lyra". Here's my code snippet: var stars = ["Polaris", "Aldebaran", "Deneb", ...

Tips for verifying the presence of a value in a select box

Are there alternative approaches to using a for loop to determine if a value is present in a select box with JavaScript? I am interested in something similar to document.getElementById('selbox').valueExists('myval'); ...