JQuery Dialog - set button text without using a key

I need to provide localization of the button text in the jQuery dialog, however the key for the button text is usually used in the jQuery dialog:

$(DialogDiv).dialog({ bgiframe: true, resizable: false, buttons: { Save : saveCallback, Cancel : cancelCallback} }); 

Is there a way to separately specify text without using a key as a text value? I am currently using this, however, I am not a fan of using localized values ​​as keys:

 var buttonCallbacks = {}; buttonCallbacks[com.i18n.getText("Save")] = function() {}; buttonCallbacks[com.i18n.getText("Cancel")] = function() {}; $(DialogDiv).dialog({ bgiframe: true, resizable: false, buttons: buttonCallbacks }); 

Thanks.

+4
source share
3 answers

If you look at the button options for the dialog, you will see that the second specified format takes an array of objects:

 $(DialogDiv).dialog({ bgiframe: true, resizable: false, buttons: [ { text: com.i18n.getText("Save"), click: saveCallback }, { text: com.i18n.getText("Cancel"), click: cancelCallback } ] }); 
+6
source

Just reached the top (1.8):

 var button = $('<button type="button"></button>') .text(name) // name is object key from each .click(function() { fn.apply(self.element[0], arguments); }) .appendTo(uiDialogButtonPane); 

So this is not like that.

Now we can assume that adding a callback after the show changes the buttons. It seems very hacky - I would suggest how you do it now.

+1
source

I can tell you how to make ex-post-facto changes after your initialization is complete, but for this, the constructor will require a fundamental change to the jQuery user interface library, which is quite possible, of course, with open source code. Functionality is not currently provided to you.

Instead, I propose the following, which will make the necessary changes after the dialog is initialized:

 $('.myDialogSelector').parent() .find('span.ui-button-text:contains("OriginalNameFromKey")') .html("New Button Text"); 

See the working script here .

Can I find out why the constructor format has any meaning for you? I find it difficult to imagine the features of use. The way you build your buttons mapping seems very good.

0
source

Source: https://habr.com/ru/post/1343227/


All Articles