Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Objective:

To ensure that Form Grid data do not have duplicates确保表格表格数据不重复

Code:

Change 把表单表格 "gridId" to your form grid 改成 "Field Id".

To compare certain columns only instead of all columns, modify 要比较某些列而不是所有列,修改 '$(row).find(".grid-cell")' to '$(row).find("[KB:column_key=field1], [KB:column_key=field2]")'.  (Take note that this is separated by a comma.注意这是用逗号隔开的。)

Code Block
<script>
    $(document).ready(function(){
        var formGridFieldId = "gridId";

        //run when form grid value change
        $("[name="+formGridFieldId+"]").live("change", function(){
            var table = $(this).find("table");

            //get last added row
            var addedRow = $(table).find("tr.grid-row:last-child");

            var duplicate = false;

            //Loop all the row and compare all the field
            $(table).find("tr.grid-row").each(function(){
                var row = $(this);

                if ($(row).attr("id") != $(addedRow).attr("id")) {
                    var similar = true;

                    //Loop all field
                    //Change '$(row).find(".grid-cell")' to '$(row).find("[column_key=field1], [column_key=field2]")'
                    //if only want to compare to certain fields. Separate with comma.

                    $(row).find(".grid-cell").each(function(){
                        var field = $(this);

                        var columnKey = $(field).attr("column_key");
                        var value = $(field).text();

                        var newValue = $(addedRow).find("[column_key="+columnKey+"]").text();

                        if (value != newValue) {
                            similar = false;
                            return false;
                        }
                    });

                    if (similar) {
                        duplicate = true;
                        return false;
                    }
                }
            });

            //if record is duplicate, remove it
            if (duplicate) {
                $(addedRow).remove();
            }
        });
    });
</script>