小编Jam*_*een的帖子

在Meteor中使用AutoForm的可编辑表格

如何在Meteor中创建包含输入字段的表格.我使用了http://autoform.meteor.com/update-each中的示例,但它们只使用了1个输入字段.

该功能适用​​于此代码:

  <tbody>
    {{#each persons}}
      {{#autoForm id=makeUniqueID type="update" collection=Collections.Persons doc=this autosave=true}}
      <tr>
        <td>{{> afFieldInput name="fullName" label=false}}</td>
        <td>{{> afFieldInput name="email" label=false}}</td>
        <td>{{> afFieldInput name="address" label=false}}</td>
        <td><button type="submit" class="btn btn-xs btn-danger">Delete</button></td>
      </tr>
      {{/autoForm}}
    {{/each}}
  </tbody>
Run Code Online (Sandbox Code Playgroud)

但它<form>围绕每个创建了一个元素<tr>,它搞砸了我的HTML.这样做的正确方法是什么?

javascript node.js meteor meteor-autoform

7
推荐指数
1
解决办法
1503
查看次数

在Django中使用最新的相关对象进行批注

我有一个模型Conversation和一个模型Message.

该模型Message具有会话的外键,文本字段和日期字段.

如何列出所有会话,并为每个会话获取最新消息和最新消息的日期?

我猜它就像是

Conversation.objects.annotate(last_message=Max('messages__date'))
Run Code Online (Sandbox Code Playgroud)

但它只会给我最新的约会.我想要last_message包含最后一条消息的文本和它的创建日期.也许我需要使用prefetch_related?

python django django-models django-queryset

7
推荐指数
2
解决办法
905
查看次数

使用openpyxl从命名范围读取值

如何使用openpyxl从Excel中的命名区域读取值?

我在http://openpyxl.readthedocs.org/en/latest/_modules/openpyxl/workbook/names/named_range.html找到了源代码,但我无法弄清楚如何正确使用它.

openpyxl

6
推荐指数
1
解决办法
9275
查看次数

在Django模板中包含url变量

我在模板文件中有一个"创建新对象"按钮.

我想在我的网站上包含几个位置的按钮,但我希​​望包含带有链接的模板.

我试过了

{% include "snippets/icon_add.html" with link="/create_new_item/" only %}
Run Code Online (Sandbox Code Playgroud)

但我想利用它的好处{% url 'create_new_item' %}.

我能做点什么吗

{% include "snippets/icon_add.html" with link={% url 'create_new_item' %} only %}
Run Code Online (Sandbox Code Playgroud)

python django django-templates django-template-filters

6
推荐指数
1
解决办法
2165
查看次数

在 matplotlib 中翻转绘图

我正在用 matplotlib 创建一个情节

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 5, 0.1);
y = np.sin(x)
plt.plot(x, y)
Run Code Online (Sandbox Code Playgroud)

我可以翻转绘图,使 y 轴倒置,所有正值都为负,反之亦然吗?

我知道我可以乘以 -1 并使用,invert_yaxis但我想知道是否有一个函数可以在不改变值的情况下翻转它。

python numpy matplotlib

6
推荐指数
1
解决办法
2万
查看次数

在Meteor中改变钩子中的反应变量值

我有

Template.templateName.onCreated(function() {
  this.variableName = new ReactiveVar;
  this.variableName.set(true);
});
Run Code Online (Sandbox Code Playgroud)

templateName我有一个autoform.我需要设置无功变量variableNamefalse的时候autoform提交.

我试过了

AutoForm.hooks({
  myForm: {
    onSuccess: function(operation, result) {
      this.variableName.set(false);
    },
  }
});
Run Code Online (Sandbox Code Playgroud)

但它不起作用,因为this.它没有templateName像帮助器和事件那样引用模板.如果我使用会话,它会起作用,因为它们不限于特定模板.

如何更改autoform hook中的反应变量?

我也试过了

AutoForm.hooks({
  myForm: {
    onSuccess: function(operation, result) {
      this.template.variableName.set(false);
      this.template.parent.variableName.set(false);
      this.template.parent().variableName.set(false);
      this.template.parentData.variableName.set(false);
      this.template.parentData().variableName.set(false);
      this.template.parentView.variableName.set(false);
      this.template.parentView().variableName.set(false);
    },
  }
});
Run Code Online (Sandbox Code Playgroud)

使用console.log(this.template)它时会打印一个对象.如果我使用console.log(this.template.data)我得到

Object {id: "myForm", collection: "Meteor.users", type: "update", doc: Object, validation: "submitThenKeyup"…}
Run Code Online (Sandbox Code Playgroud)

我使用反应变量variableName来确定是显示可编辑的表单还是显示用户的数据的良好表示.也许有另一种更好的方法来做到这一点.

javascript node.js meteor meteor-autoform

6
推荐指数
1
解决办法
1082
查看次数

map()、reduce() 和 filter 与 forEach()

刚刚学习MapReduce,所以想知道这样写有什么好处吗

const initialValue = 0;

if (this.items) {
  return this.items.filter(function (item) {
    return item && item.quantity && item.price;
  }).reduce(function(previousValue, currentValue) {
    return previousValue + currentValue.quantity * currentValue.price ;
  }, initialValue);
} else {
  return initialValue;
}
Run Code Online (Sandbox Code Playgroud)

而不是仅仅

let total = 0;
if (this.items) {
  this.items.forEach(function(item) {
    if (item && item.quantity && item.price) {
      total += item.quantity * item.price;
    }
  });
}
return total;
Run Code Online (Sandbox Code Playgroud)

javascript performance functional-programming mapreduce

6
推荐指数
2
解决办法
5003
查看次数

选择运营商的最佳组合以查找目标编号

我有一系列操作和目标号码.

这些行动可能是

+ 3
- 3
* 4
/ 2
Run Code Online (Sandbox Code Playgroud)

我想通过使用这些操作找出我能够接近目标号码的距离.

我从0开始,我需要按顺序遍历操作,我可以选择使用该操作或不使用它.

因此,如果目标数字是13,我可以使用+ 3* 4获得12,这是我可以达到目标数字13的最接近的数字.

我想我需要计算所有可能的组合(我猜计算的数量因此是2 ^ n,其中n是操作的数量).

我试过用java做这个

import java.util.*;

public class Instruction {
    public static void main(String[] args) {
        // create scanner
        Scanner sc = new Scanner(System.in);

        // number of instructions
        int N = sc.nextInt();

        // target number
        int K = sc.nextInt();

        //
        String[] instructions = new String[N];

        // N instructions follow
        for (int i=0; i<N; i++) {
            //
            instructions[i] = sc.nextLine();
        }

        // …
Run Code Online (Sandbox Code Playgroud)

java algorithm recursion dynamic-programming

6
推荐指数
1
解决办法
951
查看次数

React Router 中嵌套路由中的默认组件

在 React Router 中我有一个嵌套路由

<Route path='about' component={{main: About, header: Header}}>
  <Route path='team' component={Team} />
</Route>
Run Code Online (Sandbox Code Playgroud)

所以现在当我去 时它会显示团队/about/team

但是如何设置访问时显示哪个组件呢/about

我努力了

<Route path='about' component={{main: About, header: Header}}>
  <IndexRoute component={AboutIndex} />
  <Route path='team' component={Team} />
</Route>
Run Code Online (Sandbox Code Playgroud)

<Route path='about' component={{main: About, header: Header}}>
  <Route path='/' component={AboutIndex} />
  <Route path='team' component={Team} />
</Route>
Run Code Online (Sandbox Code Playgroud)

但它不起作用。

我的“关于”组件如下所示

class About extends React.Component {
  render () {
    return (
      <div>
        <div className='row'>
          <div className='col-md-9'>
            {this.props.children}
          </div>
          <div className='col-md-3'>
            <ul className='nav nav-pills nav-stacked'>
              <li className='nav-item'><IndexLink …
Run Code Online (Sandbox Code Playgroud)

javascript jsx reactjs react-router

6
推荐指数
3
解决办法
7361
查看次数

如何使用 Mongoose 确定用户是否喜欢某个帖子

我正在尝试制作一个功能,以便用户可以喜欢帖子(用户喜欢或不喜欢帖子,即没有不喜欢或投票)。

我的帖子是具有架构的 MongoDB 集合中的对象

{
  title: String,
  text: String
}
Run Code Online (Sandbox Code Playgroud)

虽然我的用户有架构

{
  username: String,
  password: String
}
Run Code Online (Sandbox Code Playgroud)

例如,我可以在帖子中创建一个数组,其中包含喜欢特定帖子的用户的 ID。它看起来像

{
  title: String,
  text: String,
  likedByUsers: [ObjectID]
}
Run Code Online (Sandbox Code Playgroud)

或者我可以使用用户喜欢的帖子的 id 为用户创建一个数组,这将类似于

{
  username: String,
  password: String,
  postsLiked: [ObjectID]
}
Run Code Online (Sandbox Code Playgroud)

但是,我希望用户会喜欢数千个帖子,因此我可能会面临 MongoDB 中对象大小的限制。

所以我想我应该创建一个新的收藏集

{
  userId: ObjectID,
  postId: ObjectID
}
Run Code Online (Sandbox Code Playgroud)

但是,我正在考虑如何在我的应用程序中检索它。

我是否应该存储用户(已登录)喜欢的所有帖子的 ID 数组,并且在打印每个帖子时,我会查看该帖子是否在用户喜欢的帖子中,然后显示“已经喜欢”按钮?

或者,我可以在请求查看所有帖子时发送用户 ID,然后为每个帖子添加一个字段,表示“isLiked”,表示是否可以找到 Liked 集合中的对象,同时匹配帖子和用户ID?

javascript database-design mongoose mongodb node.js

6
推荐指数
1
解决办法
3459
查看次数