检查存储中是否存在类似记录以避免重复

发布于 2024-10-01 03:27:04 字数 469 浏览 2 评论 0原文

我有一个包含以下字段的 JsonStore:

id
date
time
type

我有一个表单,收集三个字段(datetimetype)并插入一个新字段记录到store中(我单独保存store)。我想检查商店中是否已存在具有相同字段值组合的记录,以避免重复条目。

我已经设法在另一家商店中检查重复的 ID,如下所示:

find = DepartmentMemberStore.find('member_id', value_from_form);
if (find != -1) {
    // popup error window
    return;
} else {
    // add new record to store
}

我不知道如何检查商店以查看多个字段值是否匹配。

I have a JsonStore with the following fields:

id
date
time
type

I have a form that collects the three fields (date, time, type) and inserts a new record into the store (I save the store separately). I would like to perform a check if a record with the same combination of field values already exists in the store to avoid duplicate entries.

I have managed to check for duplicate ID's in another store like this:

find = DepartmentMemberStore.find('member_id', value_from_form);
if (find != -1) {
    // popup error window
    return;
} else {
    // add new record to store
}

I don't know how to check a store to see if multiple field values match.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-10-08 03:27:04

对于这种情况,我使用了 Store 的 findBy( Function fn, [Object range], [Number startIndex] ) 。为存储中的每条记录调用函数 fn ,并将当前记录及其相应的 id 传递给该函数。因此,您可以使用当前记录的字段来与每个表单字段进行比较。

以下是适合您情况的示例:

var recordIndex = DepartmentMemberStore.findBy(
    function(record, id){
        if(record.get('date') === date_from_form && 
           record.get('time') === time_from_form &&
           record.get('type') === type_from_form){
              return true;  // a record with this data exists
        }
        return false;  // there is no record in the store with this data
    }
);

if(recordIndex != -1){
    alert("We have a duplicate, abort!");
}

I've used Store's findBy( Function fn, [Object scope], [Number startIndex] ) for this situation. The function fn is called for every record in the store, and the current record and its corresponding id are passed into the function. Thus you can use the current record's fields to compare against each form field.

Here's an example for your situation:

var recordIndex = DepartmentMemberStore.findBy(
    function(record, id){
        if(record.get('date') === date_from_form && 
           record.get('time') === time_from_form &&
           record.get('type') === type_from_form){
              return true;  // a record with this data exists
        }
        return false;  // there is no record in the store with this data
    }
);

if(recordIndex != -1){
    alert("We have a duplicate, abort!");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文