简体   繁体   English

ExtJS4.2-FieldContainer内部的字段始终标记为脏。

[英]ExtJS4.2 - Fields inside of FieldContainer is always marked dirty.

Good day. 美好的一天。 I have a web application where a user fills out several forms. 我有一个Web应用程序,用户可以在其中填写几种表格。 These forms can then be edited and I want to log the edits in my database as well in a form of an audit trail by getting all the dirty fields in the form and logging the name and value in the database. 然后可以编辑这些表单,并且我想通过将表单中的所有脏字段都记录在数据库中并记录namevalue ,以审核跟踪的形式将所做的编辑记录在数据库中。

Initially, I had a problem with Radio Buttons that had the same name since they were marked as dirty but I eventually managed to solve that. 最初,我对同名单选按钮有疑问,因为它们被标记为肮脏,但最终我设法解决了这个问题。 My issue now, however, are the fields inside a FieldContainer. 但是,我现在的问题是FieldContainer中的字段。 Even if I unmark them as dirty, when I run the code that checks for dirty fields, the fields inside the FieldContainer are marked as dirty. 即使我将它们取消标记为脏,当我运行检查脏字段的代码时,FieldContainer中的字段也被标记为脏。

Here's the code that I run on preloading of the form to make my buttons clean: 这是我在预加载表单时运行的代码,以使按钮保持整洁:

form.items.filterBy(function(c){

    if(c.getXType()==='radiofield'){
        c.originalValue = c.getValue();
        c.checkDirty();
    }
    else{
    }
    if(c.getXType()==='fieldcontainer'){
        c.items.filterBy(function(d){
            if(d.getXType()==='checkboxfield' || d.getXType()==='textfield' ||
               d.getXType()==='combobox'){

                console.log("itemID of field in fieldContainer = " + d.getItemId());

                d.originalValue = d.getValue();
                d.checkDirty();

                console.log("component dirty state = " + d.isDirty());
            }
        });
    }

});

What my function does is it iterates through every child view element of the form. 我函数的作用是遍历表单的每个子视图元素。 As you can see, I have two if conditions. 如您所见,我有两个if条件。 My first if condition checks if the element is a radio button, if so, I just mark it as clean. 我的第一个if条件检查该元素是否为单选按钮,如果是,则将其标记为干净。 The second if statement checks if the element is a field container. 第二条if语句检查元素是否为字段容器。 If so, I check all the children view elements in the field container and I look for check boxes, text fields, and combo boxes, then mark them as clean. 如果是这样,我将检查字段容器中的所有子级视图元素,然后查找复选框,文本字段和组合框,然后将其标记为干净。 Here's the console log that I got from that: 这是我从中获得的控制台日志:

itemID of field in fieldContainer = amountFinanced
component dirty state = false
itemID of field in fieldContainer = unitTerm
component dirty state = false

I counter checked with my View and those are the right itemIDs and they are marked as clean. 我用我的View进行了检查,发现这些是正确的itemID,并将它们标记为干净。

Now, I create my audit trail records on button click. 现在,我在单击按钮时创建我的审计跟踪记录。 The general idea is that I get the active form, go through the fields and check which ones are dirty. 总体思路是,我得到活动表格,仔细检查各个字段并检查哪些是脏的。 Here's my code: 这是我的代码:

form2.items.filterBy(function(c){
    if(c.getXType()==='radiofield' || c.getXType() ==='combobox' ||
       c.getXType()==='radiogroup' || c.getXType() ==='textfield' ||
       c.getXType()==='datefield' || c.getXType() ==='fieldset' ||
       c.getXType()==='fieldcontainer' || c.getXType() ==='checkbox' ){

        if(c.getXType()!='fieldcontainer'){
            if(c.isDirty()){
                var entry = '';
                if(c.getXType()==='radiofield'){
                    entry = c.getName() + ": " + c.getSubmitValue();
                }
                else{
                    entry = c.getName() + ": " + c.getRawValue();
                }

                changes.push(entry);
            }
        }
        else{
            c.items.filterBy(function(d){
                if(d.getXType()==='radiofield' || d.getXType() ==='combobox' ||
                   d.getXType()==='radiogroup' || d.getXType() ==='textfield' ||
                   d.getXType()==='datefield' || d.getXType() ==='fieldset' ||
                   d.getXType()==='fieldcontainer' || d.getXType() ==='checkbox' ){

                    if(d.getXType()!='fieldcontainer' && d.getXType()!='label'){
                        console.log("item id = " + d.getItemId());
                        console.log("item name = " + d.getName());
                        console.log("component dirty state = " + d.isDirty());

                        if(d.isDirty()){

                            var entry = '';
                            if(d.getXType()==='radiofield'){
                                entry = d.getName() + ": " + d.getSubmitValue();
                            }
                            else{
                                entry = d.getName() + ": " + d.getRawValue();
                            }
                            changes.push(entry);
                        }
                    }
                }
            });
        }
    }
});

This is a bit lengthier than the previous one. 这比上一个更长。 I start by going through all the view elements inside the form and I filter through all the element XTypes that I used. 我首先浏览表单中的所有view元素,然后筛选使用的所有元素XType。 If I come across something that's not a field container, I check if it's dirty, if it's dirty, I take the name and value of the field and put it in an array (that I will later on save to my database). 如果遇到了不是字段容器的东西,我会检查它是否脏了,是否脏了,我将字段的namevalue放在数组中(稍后将其保存到数据库中)。 However, if it's a field container, I check the elements in that field container to check if those are dirty then I'll save them. 但是,如果它是一个字段容器,我将检查该字段容器中的元素以检查它们是否脏,然后将它们保存。

Now. 现在。 I also log the item IDs as well as the name of the fields inside the fieldcontainer. 我还记录了项目ID以及fieldcontainer内字段的名称。 I checked if marking the fields inside the fieldset as clean (as I did when I loaded the forms) would help. 我检查了将字段集中的字段标记为干净(如加载表单时所做的那样)是否有帮助。 However, upon checking the console for logs, I saw that the fields inside the fieldcontainer was marked as dirty even if I didn't change them. 但是,在检查控制台的日志后,我发现即使没有更改它们,fieldcontainer内的字段也被标记为脏字段。 Here is my log: 这是我的日志:

item id = amountField
item id = amountFinanced
item name = AMOUNT_FINANCED
component dirty state = true
item id = termField
item id = unitTerm 
item name = TERM
component dirty state = true

As you can see, I am able to go through the field container (amountField and termField respectively), navigate to the field (amountFinanced - textfield, and unitTerm - comboBox), get their names, and see if they're dirty. 如您所见,我能够遍历字段容器(分别为amountField和termField),导航到字段(amountFinanced-textfield和unitTerm-comboBox),获取它们的名称,并查看它们是否脏了。

Now I'm confused, I was able to mark those fields as clean earlier, why is it that when I proceed to checking all the dirty fields, they're now marked as dirty even if I didn't change anything? 现在我很困惑,我能够将这些字段标记为较干净,为什么当我继续检查所有脏字段时,即使我没有进行任何更改,现在也将它们标记为脏字段?

Is there a better way to check if the fields inside a field container (and a field set) are dirty? 有没有更好的方法来检查字段容器(和字段集)内的字段是否脏? I've been stuck with this problem for several hours now and the frustration is getting to me. 我已经被这个问题困扰了几个小时了,沮丧的情绪越来越大了。

Thanks for any help. 谢谢你的帮助。

Okay I'm an idiot. 好吧,我是个白痴。 The said fieldsets are initialized from another controller since the values are taken from another store/database table. 所述字段集是从另一个控制器初始化的,因为这些值是从另一个存储/数据库表获取的。 I had to run the code below in the controller where I get the values from my store and load them into the form. 我必须在控制器中运行以下代码,从存储中获取值并将它们加载到表单中。

However, in my other form that contain fieldsets, it seems that you have to run this code to make sure they aren't marked as dirty every time even if no changes were made: 但是,在另一种包含字段集的形式中,似乎您必须运行以下代码以确保即使没有进行任何更改,也不会每次都将它们标记为脏:

form.items.filterBy(function(c){

    if(c.getXType()==='radiofield'){
        c.originalValue = c.getValue();
        c.checkDirty();
    }
    if(c.getXType()==='fieldcontainer'){
        c.items.filterBy(function(d){
            if(d.getXType()==='checkboxfield' || d.getXType()==='textfield' ||
               d.getXType()==='combobox'){ //add whatever XType you have inside

                console.log("itemID of field in fieldContainer = " + d.getItemId());
                console.log("component dirty state before = " + d.isDirty());

                console.log("original value = " + d.originalValue);
                console.log("get value = " + d.getValue());
                console.log("get raw value = " + d.getRawValue());


                d.originalValue = d.getValue();
                d.checkDirty();

                console.log("component dirty state after = " + d.isDirty());


            }
        });
    }

});

Of course you can remove the console logs. 当然,您可以删除控制台日志。

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

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