Using the ExtJS Combobox to send a hiddenName value through a POST

I am in the process of configuring an ExtJS combobox to return the value instead of the display value.

My issue arises when using ajax to fetch data for the combobox field and simultaneously utilizing getForm().load to load the selected values. In my model, I have specified a display value and a code value, where the display value is assigned to 'name' and the code value is assigned to 'hiddenName'. However, upon submitting the data, it appears that the display value is being submitted instead of the code value from the 'hiddenName' field.

Interestingly, after changing the selection in the combobox, the code value is correctly submitted.

The following is the code snippet:

{
            xtype: 'combobox',
            fieldLabel: 'Type',
            name: 'DataType',
            hiddenName: 'DataTypeCde',
            store: Ext.create('Ext.data.JsonStore', {
                proxy: {
                    type: 'ajax',
                    actionMethods: { create: 'POST', read: 'POST', update: 'POST', destroy: 'POST' },
                    url: '@Url.Content("~/{URL}/DataType")',
                    reader: {
                        type: 'json'
                    }
                },
                fields: ['Id', 'Name']
            }),
            valueField: 'Id',
            displayField: 'Name',
            typeAhead: true,
            mode: 'remote',
            emptyText: 'Select a data type...',
            anchor: '96%'
        },

Another point to note is that when setting the 'name' to 'DataTypeCde', the code value is displayed correctly. However, once the ajax data for the combobox is loaded, the display value gets replaced by the original display value.

Therefore, I need either the 'hiddenName' to load the DataTypeCde value and submit it back or find a way for the combobox data to preload on form render.

If anyone can provide insight into achieving this or identify any flaws in my approach, it would be greatly appreciated.

Update: *Update4: (added POST response sample)*

Allow me to clarify further what I aim to accomplish.

(1) With the current code configuration, the following scenario unfolds... If DataType = Shopping and DataTypeCde = SP the combobox displays Shopping upon rendering, which is the desired behavior. However, upon submitting this data to the controller, "Shopping" is returned instead of "SP," which is undesired.

Example of POST response...

id=1&DataType=Shopping

(2) Setting the name field as "DataTypeCde" results in the following outcome... If DataType = Shopping and DataTypeCde = SP the combobox displays SP after rendering, which is not ideal as I prefer to show the text rather than the code. Yet, upon submitting the data to the controller, the code "SP" is returned as intended.

Example of POST response...

id=1&DataTypeCde=SP

(3) Opting for the second method of naming the field "DataTypeCde" leads to the code being shown until the combobox data loads. To address this issue, I enabled the "autoLoad" feature, causing the combobox data to load automatically at render. However, loading all comboboxes initially significantly slows down page load.

Example of POST response...

id=1&DataTypeCde=SP

(Q) My inquiry is... Is there a way to display the text value in the combobox without preloading the data but still have the code value posted back instead of the display value?

Update2:

Apologies for not mentioning earlier, I am utilizing ASP.NET MVC4 with ExtJs. When referring to returning the post response to the controller, I mean the MVC controller. Initially, I did not think the backend details were relevant to this query.

Update3:

To provide a more comprehensive view of the implementation, below is the code and data sample used to load the form.

    example2.getForm().load({
    url: '@Url.Content("~/{URL}/Data/")',
    params: {
        id: window.record.data['id']
    },
    method: 'POST'
});

The above fetches a collection of JSON objects similar to this...

{"success":true,"data":{"id":"1","DataType":"Shopping","DataTypeCde":"SP"}}

Here's the code to retrieve the data for the combo box...

store: Ext.create('Ext.data.JsonStore', {
            proxy: {
                type: 'ajax',
                actionMethods: { create: 'POST', read: 'POST', update: 'POST', destroy: 'POST' },
                url: '@Url.Content("~/{URL}/DataType")',
                reader: {
                    type: 'json'
                }
            },
            fields: ['Id', 'Name']
        }),

With the resulting JSON object structured as follows...

[{"Id":"SP","Name":"Shopping"}]

Thank you.

Answer №1

Having a bit of trouble understanding your requirements...

name This parameter name is used when including the field value in a form.submit(). If no name is configured, it will default to inputId. To exclude the field from form submit, set submitValue: false (works for any field type).

hiddenName The name of a hidden field synchronized with the combo value.

valueField Name of the field containing the value to submit along with the name property.

displayField Name of the field containing the display value.

In your setup, you need to submit two values:

submitValue: true

  • DataType with the value of the valueField (*To prevent, set submitValue: false)
  • DataTypeCde with the value of the displayField (To remove, delete the hiddenName)

Regarding the edit:

...submit this result back to the controller...

What do you mean by submitting to the controller? Your issue seems unusual and might be due to incorrect implementation. Is 'Shopping' the value of the Name field in your store and SD the Id field?

Edit

Is there a way to set the display value without loading data?

Not really... you can set the value property which displays but also commits the value.

but have the code value get posted instead of the display value.

No, there isn't a separate Code Value. There is only one valueField that represents the value.

Assuming the combo store contains {"id":"SP","Name":"Shopping"}:

The combo field follows HTML behavior with a name and a value on receiving data. By using hiddenName (e.g., DataType) along with the name (e.g., DataTypeCde), setting the field as DataType=SP would display Service. The value triggers a lookup in the store. Setting the combo with DataType=AP would display AP if the record is not found.

Your return should only include the combo name along with the valueField value, like:

{"success":true,"data":{"id":"1","DataType":"SP"}}

Using string as Id fields is not recommended.

  • The default idProperty is set to id, which may cause lookups to fail. Set the idProperty within the store reader to idProperty: 'Id'.

  • reader: 
    {
        type: 'json',
        idProperty: 'Id'
    }

    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

    Tips for sending AJAX forms with only certain fields provided

    I have a form that allows users to update their account information. I have implemented ajax requests to save changes without refreshing the page. However, I only want to submit certain fields from the form as some are disabled. The code snippet below is w ...

    Exploring the possibilities: Utilizing HTML5, File API, and jQuery to

    I have a form displayed in a dialog and it includes a "submit" button which, when clicked, utilizes jQuery to send the form data to the server via AJAX. However, I encountered an issue where the uploaded file was always null. After researching on Google, I ...

    Use ajax to add rows to the second-to-last table

    I am facing a situation where I have a table with 25 default rows. When scrolling to the bottom of the table, I want to dynamically insert another set of 25 rows. Everything is functioning correctly, but in a specific scenario, I need to preserve the last ...

    Encountered an error: "Type error undefined" while attempting to populate a form using AJAX and JSON

    Upon inspecting the development console, it's clear that my AJAX request was successful and I've received the necessary JSON data. However, I'm struggling to display it correctly as I keep encountering the error below: Uncaught TypeError: C ...

    In internet explorer with AJAX, the UI is refreshed by Javascript only when an alert() function is triggered

    Having an issue in Internet Explorer, works perfectly in Firefox. There is a javascript function that updates the UI (screen content) before an AJAX call. However, it does not update the UI unless an alert box prompt is used. Without the alert box, the UI ...

    Accessing the phone number input from a form in Rails and verifying its presence in the database. Implementing distinct ajax responses based on the existence of the value

    Looking for a way to verify phone numbers? The process involves users entering a phone number, submitting the form, and cross-referencing it with a database of phone number records to determine if it's valid. My goal is to provide different AJAX respo ...

    Using PartialViews with PagedList

    I am currently working with a partial view that contains tabular data. I have implemented PagedList for pagination. Everything functions as intended when using a normal view, however, the issue arises when attempting to integrate it into a partial view. U ...

    In search of the most efficient method for integrating an AJAX-powered TreeGrid feature

    Can anyone recommend an Ajax/TreeGrid implementation that meets the following criteria: Must support server side sorting Should be able to load leaf nodes on demand, meaning only children of open nodes are loaded Needs to support paging so that nodes are ...

    Guide to scraping a website using node.js, ASP, and AJAX

    I am currently facing an issue where I need to perform web scraping on this specific webpage form. This webpage is dedicated to vehicle technical reviews, and you can try inputting the car license CDSR70 for testing purposes. As mentioned earlier, I am u ...

    Issues with script execution on Ajax tab

    I'm currently using JQuery UI tabs to load content through Ajax. I have a situation where two tabs are supposed to load HTML content and execute a script to hide or show certain elements in the loaded content. However, I encountered an issue where the ...

    Inquiries regarding the integration of server-side and client-side validation methods

    Currently, I am utilizing PlayFramework to construct a user registration form that operates via Ajax without refreshing the page. To ensure accuracy and data validation, I am contemplating two methods for implementing client and server-side validation. O ...

    Creating dynamic animations for your elements with AJAX

    How can I apply animation to an element as soon as it appears? I want others with the same properties to remain unaffected. Here is my approach: $.each(data, function(i, obj) { if(obj['Ping'] == "FALSE"){ ...

    Django: Utilizing AJAX to send a POST request for a form submission and updating the

    Do you need assistance with creating a status view for your form? The current setup involves taking user input, submitting to a view where calculations are processed, and then redirecting to the output. However, the user must wait with no indication of pro ...

    A guide on retrieving nested objects from my API and parsing the resulting data in each iteration for individual elements

    Struggling to update my website with a new API, specifically with accessing nested objects in a loop. Still trying to wrap my head around ajax and JSON concepts. Take a look at the API output and JS code I've been working on for a better understanding ...

    Control for Star Ratings that allows decimal values to be included

    I am currently working in Visual Studio 2010 and using C# for coding. I need to create a star rating control where I have values ranging from 1 to 5, and I want to display the rating visually with stars. Here is a detailed explanation of what I am looking ...

    The system detected a missing Required MultipartFile parameter in the post request

    Can anyone explain to me why I am encountering the error mentioned above? I am unable to figure out the reason. Below is my code, please review it and suggest a solution for fixing this error. The objective is to upload multiple files to a specific locatio ...

    Is there a way to prevent a web page from refreshing automatically for a defined duration using programming techniques?

    I am currently working on a specific mobile wireframe web page that includes a timer for users to answer a question after the web content body has loaded. There are two possible outcomes when answering the question: 1) If the user fails to answer in time, ...

    Issues with Codeigniter Ajax Post functionality

    Hey everyone, I'm working on implementing a simple voting system for comments using jQuery Ajax to avoid page refreshing when someone likes or dislikes a comment. Here is the jQuery code I have so far: $(document).ready(function(){ $(".vote-btn" ...

    ES6 Generators: lack of informative stack trace when using iterator.throw(err)

    The ES6 approach: iterator.throw(err) is often explained as inserting an exception as if it happened at the yield statement within the generator. The challenge lies in the fact that the stack trace of this exception does not include any information about t ...

    Choose a selection in ExtJS by finding matching attributes

    Is there a convenient method to choose an item in an Ext.tree.Panel by matching it with an item based on the same attribute in an Ext.grid.Panel? For example, using something like: tree_dir.getSelectionModel().select(grid_file.getSelectionModel().getSelect ...