简体   繁体   English

Yii2:是否有一种简单的方法可以使用Javascript的值加载现有的ActiveForm?

[英]Yii2: Is there an easy way to load an existing ActiveForm with values from Javascript?

I have an existing Yii2 ActiveForm (like the one below) on a Single-Page-App where I want to load new values into it via AJAX. 我在Single-Page-App上有一个现有的Yii2 ActiveForm(如下图所示),我想通过AJAX将新值加载到其中。 Is there already a simple way to do that, or do I need to make my own Javascript function to do that? 是否已经有一种简单的方法可以执行此操作,或者我需要创建自己的Javascript函数来执行此操作?

<form>
    <input type="text" name="Conversation[cv_timestamp]">
    <input type="text" name="Conversation[cv_type]">
    <input type="text" name="Contact[ct_firstname]">
    <input type="text" name="Contact[ct_lastname]">
</form>

So, if I understand you correctly, you want your controllers to save your model data via ajax? 因此,如果我理解正确,您是否希望控制器通过Ajax保存模型数据?

If that's the case, you should take a look at ActiveControllers . 如果是这样,您应该看看ActiveControllers

Basically, you will have: 基本上,您将拥有:

  • index: list of models 索引:型号清单
  • view: return the details of a model 视图:返回模型的详细信息
  • create: create a new model 创建:创建一个新模型
  • update: update an existing model 更新:更新现有模型
  • delete: delete an existing model 删除:删除现有模型
  • options: return the allowed HTTP methods 选项:返回允许的HTTP方法

exposed via different verbs to use them as an API. 通过不同的动词公开以将其用作API。

I ended up making my own Javascript function. 我最终制作了自己的Javascript函数。 Improvements are welcome. 欢迎改进。

 // Load ActiveForm with new model attributes via Javascript
 //
 // Form fields must have been named like this: <input name="Contact[firstname]"> <input name="Contact[lastname]">
 //
 // @param {(string|jQuery object)} formSelector - String with selector or a jQuery object
 // @param {object} models : Object where keys match the 1st level form field names and the values are the model attributes that match the 2nd level, eg.: {Contact: {firstname: 'John', lastname: 'Doe'}, }

function loadActiveForm(formSelector, models) {
    if (!(formSelector instanceof jQuery)) {
        formSelector = $(formSelector);
    }

    $.each(models, function(modelName, model) {
        $.each(model, function(attributeName, attributeValue) {
            $input = formSelector.find(':input[name="'+ modelName +'['+ attributeName +']"]');
            if ($input.length > 1) {
                if ($input.first().is(':radio')) {
                    $input.each(function() {
                        if ($(this).val() == attributeValue) {
                            $(this).prop('checked', true).click();
                            if ($(this).closest('.btn').length > 0) {
                                $(this).closest('.btn').button('toggle');
                            }
                        }
                    });
                } else {
                    alert('In loadActiveForm an input had multiple tags but they are not radio buttons.');
                }
            } else {
                $input.val(attributeValue);
            }
        })
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM