Yii2:有没有一种简单的方法可以从Javascript加载现有的ActiveForm值?
我在一个单页应用程序上有一个现有的yii2activeform(如下所示),我想通过AJAX将新值加载到其中。是否已经有一种简单的方法可以做到这一点,或者我需要制作自己的Javascript函数来做到这一点Yii2:有没有一种简单的方法可以从Javascript加载现有的ActiveForm值?,javascript,model-view-controller,yii2,active-form,Javascript,Model View Controller,Yii2,Active Form,我在一个单页应用程序上有一个现有的yii2activeform(如下所示),我想通过AJAX将新值加载到其中。是否已经有一种简单的方法可以做到这一点,或者我需要制作自己的Javascript函数来做到这一点 <form> <input type="text" name="Conversation[cv_timestamp]"> <input type="text" name="Conversation[cv_type]"> <in
<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>
那么,如果我理解正确,您希望控制器通过ajax保存模型数据吗 如果是这样的话,你应该看看 基本上,您将拥有:
- 索引:模型列表
- 视图:返回模型的详细信息
- 创建:创建新模型
- 更新:更新现有模型
- 删除:删除现有模型
- 选项:返回允许的HTTP方法
通过不同的动词将它们用作API。我最终制作了自己的Javascript函数。欢迎改进
// 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);
}
})
});
}
//通过Javascript加载带有新模型属性的ActiveForm
//
//表单字段的名称必须如下所示:
//
//@param{(string | jQuery object)}formSelector-带选择器的字符串或jQuery对象
//@param{object}models:object,其中键与第一级表单字段名匹配,值是与第二级匹配的模型属性,例如:{Contact:{firstname:'John',lastname:'Doe'},}
函数loadActiveForm(formSelector,模型){
if(!(jQuery的formSelector实例)){
formSelector=$(formSelector);
}
$.each(型号、功能)(型号名称、型号){
$.each(模型、函数(attributeName、attributeValue){
$input=formSelector.find(':input[name=“”+modelName+'['+attributeName+']“]);
如果($input.length>1){
如果($input.first()是(':radio')){
$input.each(函数(){
if($(this).val()==attributeValue){
$(this.prop('checked',true.)。单击();
如果($(this).closest('.btn')。长度>0){
$(this).closest('.btn')。按钮('toggle');
}
}
});
}否则{
警报('在loadActiveForm中,输入有多个标记,但它们不是单选按钮');
}
}否则{
$input.val(属性值);
}
})
});
}
No,我希望通过ajax将模型数据加载到表单中。据我所知,ActiveController只处理从后端到前端的数据,即RESTful Web服务本身。我需要的是填充表单。在您的情况下,您应该调用activecontroller的视图,然后通过js用响应填充表单。我已经用vue(任何其他库/框架/任何可以工作的东西)完成了这项工作。