简体   繁体   English

根据多个下拉值更新文档

[英]Update documents based on multiple dropdown values

I am building an administration panel within a meteor app to be able to manage the users of the app. 我正在流星应用程序中构建一个管理面板,以便能够管理该应用程序的用户。 Currently I am displaying all the users with a dropdown for each user to change their role . 目前,我正在向所有用户显示一个下拉菜单,供每个用户更改其角色。

I need to update the changed roles only after I hit some confirmation button at the end. 只有在最后点击一些确认按钮后,我才需要更新更改的角色。

I have a template that gets rendered with the username and a dropdown with the current role of the user, my template is similar to this: 我有一个使用用户名呈现的模板,以及使用用户当前角色的下拉菜单,我的模板与此类似:

<div class="ui selection dropdown">
  <input type="hidden" name="role">
  <i class="dropdown icon"></i>
  <div class="default text">{{role}}</div>
  <div class="menu">
    <div class="item" data-value="admin">Admin</div>
    .
    .
    .
    <div class="item" data-value="user">User</div>
  </div>
</div>  

This renders as many as users I have in my database. 这将呈现与数据库中的用户数量一样多的用户。 I need to update all users who have their role changed after clicking a button. 单击按钮后,我需要更新其角色已更改的所有用户。

My current approach is to plug the user id to the dropdown: 我当前的方法是将用户ID插入下拉列表:

<div class="ui selection dropdown" id={{_id}}>

Then I have an event handler for the dropdown change and catch the value: 然后,我有一个用于下拉更改的事件处理程序,并捕获了该值:

Template.templateName.events({
  "change .ui.selection.dropdown": function(event, template) {
    var id = template.find(".ui.selection.dropdown").id;
    var role = template.find("input[name=role]").value;
    ...
  },

});

Now I am wondering if I should push those id,role pairs in some session key and update the users after clicking save button or there is a better and more effective alternatives for this ? 现在,我想知道是否应该在某些会话密钥中推送这些id,role对并在单击“ save按钮后更新用户,还是有更好,更有效的替代方法?

Instead of saving them in session variable and clearing session variable on template's destroyed callback, you can add an attribute to the changed element indicating that the element has changed. 您可以将属性添加到更改的元素中,以指示该元素已更改,而不必将它们保存在会话变量中并在模板的已destroyed回调中清除会话变量。 When save button is clicked, you can use attribute selector to get all the elements that were changed by this user. 单击“保存”按钮后,可以使用属性选择器获取该用户更改的所有元素。 See the code sample below for further reference. 请参阅下面的代码示例以获取更多参考。

Template.templateName.events({
  "change .ui.selection.dropdown": function(event, template) {
    var target = $(event.target);
    target.attr("data-changed", "data-changed");
  },
  "click #saveButtonId": function(event, template) {
    var changedElements = template.$(".ui.selection.dropdown[data-changed]");
    var changedItems = [];

    for (var i = 0; i < changedElements.length; i++) {
       var changedElement = changedElements[i];
       changedItems.push({ id: changedElement.id, value: changedElement.value });
       // or
       //changedItems.push([changedElement.id, changedElement.value]);

    }
    // here you can call a meteor method like
    // Meteor.call('updateUserRoles', changedItems); //this updateUserRoles will now get only changed items that you can update in your DB.
  }
});

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

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