小编The*_*pes的帖子

如何在React中轻松设置兄弟组件的状态?

我有一个可点击列表组件的开头,它将用于驱动一个select元素.正如你可以从下面看,onClickListItem,我路过一个子元素的状态(ListItem在这种情况下)父母(SelectableListCustomSelect组件).这工作正常.但是,我还想做的是更改兄弟组件(其他ListItems)的状态,以便在单击其中一个ListItem时切换其选定状态.

目前,我只是使用document.querySelectorAll('ul.cs-select li)抓取元素并将类更改为与所单击的索引不匹配时选择的ListItem.这在某种程度上有效.但是,在几次单击之后,组件的状态尚未由React更新(仅由客户端JS),并且事情开始崩溃.我想要做的是更改this.state.isSelected兄弟列表项,并使用此状态刷新SelectableList组件.任何人都可以提供一个比我下面写的更好的替代品吗?

var React = require('react');
var SelectBox = require('./select-box');

var ListItem = React.createClass({
    getInitialState: function() {
        return {
            isSelected: false
        };
    },

    toggleSelected: function () {
        if (this.state.isSelected == true) {
            this.setState({
                isSelected: false
            })
        } else {
            this.setState({
                isSelected: true
            })
        }
    },

    handleClick: function(listItem) {
        this.toggleSelected();
        this.props.onListItemChange(listItem.props.value);

        var unboundForEach = Array.prototype.forEach,
            forEach = Function.prototype.call.bind(unboundForEach);

        forEach(document.querySelectorAll('ul.cs-select li'), function (el) …
Run Code Online (Sandbox Code Playgroud)

siblings reactjs

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

填充表格数据的打印输出

我知道这可能很简单,但我在一个文件中有一些这样的数据:

Artichoke

Green Globe, Imperial Star, Violetto

24" deep

Beans, Lima

Bush Baby, Bush Lima, Fordhook, Fordhook 242

12" wide x 8-10" deep
Run Code Online (Sandbox Code Playgroud)

我希望能够格式化成一个漂亮的TSV类型的表,看起来像这样:

    Name  | Varieties    | Container Data
----------|------------- |-------
some data here nicely padded with even spacing and right aligned text 
Run Code Online (Sandbox Code Playgroud)

ruby arrays formatting

11
推荐指数
4
解决办法
2万
查看次数

Rails中的关联模型和SUM查询

我有两个Rails模型,一个孩子和一个家长说.

我知道我可以这样做:

Child.sum(:income, :conditions => "parent_id = #{@parent_id}")
Run Code Online (Sandbox Code Playgroud)

但我希望能够做到这一点:

Parent.children.sum(:income)
Run Code Online (Sandbox Code Playgroud)

但是如果我尝试的话,这给了我错误的价值.是否有更简洁的写作方式

Child.sum(:income, :conditions => "parent_id = #{@parent_id}")
Run Code Online (Sandbox Code Playgroud)

TIA

[ps:Rails 3开发环境]

sum associations ruby-on-rails-3

8
推荐指数
1
解决办法
7781
查看次数

应该返回true时in_array返回false

我有一个简单的脚本,它从表单中获取一个单词并评估它是否存在于文件(.txt)中.txt文件在每一行上都有一个单词或短语.文件中没有\ t或\ r \n.

但是,当我提交表单并POST文件中的第一个单词(例如"the")时,下面的脚本返回false,它应该返回true.

我知道这一点,因为当我打印出数组$文件时,我进入屏幕:

Array
(
    [0] => the
...
Run Code Online (Sandbox Code Playgroud)

所以有些不对劲......

$word = $_POST['word']);

// Get a file into an array.
$file = file('master.txt');

if (in_array($word, $file)) {
   echo "true";
}
else {
    echo "false";
}

echo "<pre>";
print_r($file);
echo "</pre>";
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我这里我出错了,因为文件()返回的数组看起来很干净,而POSTed字("the")是file()数组中的第一个值.我已经检查过以确保POST数据实际上也正确提交.

TIA.

php arrays

4
推荐指数
1
解决办法
566
查看次数

Rails应用程序中的Heroku PostgreSQL GROUP_BY错误

我有一个在开发中运行良好的rails应用程序(SQLite)但是当我通过Heroku部署它时会抛出很多错误,Heroku使用我收集的PostgreSQL.

返回错误消息:

        ActionView::Template::Error (PGError: ERROR:  
column "practices.id" must appear in the GROUP BY clause or be used in an aggregate function: 
SELECT "practices".* 
FROM "practices" WHERE ("practices".activity_id = 1) 
AND ("practices"."created_at" BETWEEN '2011-01-01' AND '2011-01-31') 
GROUP BY DATE(created_at) ORDER BY created_at DESC):
Run Code Online (Sandbox Code Playgroud)

当我调用以下内容时会抛出此内容:

def month_days_not_practiced(date = Date.today)
   p = practices.where(:created_at => date.at_beginning_of_month..date.at_end_of_month).group("DATE(created_at)").to_a.count
   days_in_month(date.year, date.month) - p
end
Run Code Online (Sandbox Code Playgroud)

我真的想保持代码干净,所以它适用于开发和生产数据库......任何人都可以解决这些问题吗?

我试过这个:

def month_days_not_practiced(date = Date.today)
   p = practices.where(:created_at => date.at_beginning_of_month..date.at_end_of_month).group("practices.id, DATE(created_at)").to_a.count
   days_in_month(date.year, date.month) - p
end
Run Code Online (Sandbox Code Playgroud)

无济于事......

TIA.

postgresql heroku ruby-on-rails-3

4
推荐指数
1
解决办法
1279
查看次数

如何测试我的Express应用程序的响应是否包含某些HTML标记/文本?

我正在运行一个Express Node服务器,并使用Mocha和Supertest来测试我的路由.

我希望能够测试我的一条Express路线的响应中是否存在某些文本,如下所示:

it('should display form text input', function(done) {
      request(app)
        .get('/')
        .end(function (err, res) {
          if (err) {
            return done(err);
          }
          res.text.should.include('class="text-input-wname');
          done();
        });
    });  
Run Code Online (Sandbox Code Playgroud)

但是,当我运行此测试时,我收到以下错误:

Uncaught TypeError: undefined is not a function
Run Code Online (Sandbox Code Playgroud)

res.text打印出很好的控制台.我知道should.include()是为了检查数组中是否存在元素,所以假定这可能不起作用.

但是,解析响应主体以检查某些文本是否存在的正确方法是什么?

mocha.js node.js express supertest

4
推荐指数
1
解决办法
2474
查看次数

调整 reportlab.platypus.Paragraph 的大小是否流畅?

我有以下代码尝试调整 reportlab Platypus flowable 文本的字体大小,直到它适合我给它的可用高度。但是我发现 Paragraph flowable 没有在递归的每个循环中保存它的 style.fontSize 属性,并且 python 阻止了无限递归。

def fits_space(w,h,aW,aH,p):
    """
    Determines if inputted text fits in the space given for a paragraph
    and if it doesn't, reduces the font-size until it does.

    """
    if w<=aW and h<=aH:
        # return font size to apply it to the para again
        print "final font_size: %d" % p.style.fontSize
        return p # now render the paragraph in the doctemplate
    else: 
        p.style.fontSize -= 1
        w,h = p.wrap(aW, aH)
        fits_space(w,h,aW,aH,p)

def renderPage(name, …
Run Code Online (Sandbox Code Playgroud)

python reportlab platypus

3
推荐指数
1
解决办法
5999
查看次数

你会如何整理这个控制器逻辑?

我在控制器中有一些逻辑,如果满足某些条件,则设置对象的状态:

if params[:concept][:consulted_legal] == 0 && params[:concept][:consulted_marketing] == 1
  @concept.attributes = {:status => 'Awaiting Compliance Approval'}
elsif params[:concept][:consulted_marketing] == 0 && params[:concept][:consulted_legal] == 1 
  @concept.attributes = {:status => 'Awaiting Marketing Approval'}
elsif params[:concept][:consulted_marketing] == 0 && params[:concept][:consulted_legal] == 0
  @concept.attributes = {:status => 'Awaiting Marketing & Legal Approval'}
else
  @concept.attributes = {:status => 'Pending Approval'}
end
Run Code Online (Sandbox Code Playgroud)

我在创建和更新操作之间共享.你会如何重构这种肮脏的东西?寻找最佳实践.

编程新手并热衷于清理我的代码.

TIA.

ruby refactoring controller ruby-on-rails

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

将业务规则转移到模型中

我之前问了一个问题,引起了一些很好的回应.

这是早先的问题

在那里给出的一些建议的背面,我尝试移动以下控制器逻辑

 if params[:concept][:consulted_legal] == 0 && params[:concept][:consulted_marketing] == 1
  @concept.attributes = {:status => 'Awaiting Compliance Approval'}
elsif params[:concept][:consulted_marketing] == 0 && params[:concept][:consulted_legal] == 1 
  @concept.attributes = {:status => 'Awaiting Marketing Approval'}
elsif params[:concept][:consulted_marketing] == 0 && params[:concept][:consulted_legal] == 0
  @concept.attributes = {:status => 'Awaiting Marketing & Legal Approval'}
else
  @concept.attributes = {:status => 'Pending Approval'}
end
Run Code Online (Sandbox Code Playgroud)

进入模型,如下:

def set_status
if status.blank?
  if (consulted_legal == true) && (consulted_marketing == true)
      status = "Pending Approval"
  elsif (consulted_legal == true) && …
Run Code Online (Sandbox Code Playgroud)

ruby refactoring controller model ruby-on-rails

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

在JQuery中使用appendTo()添加元素然后立即删除它...解决方案?

这可能是一个菜鸟问题,但是我如何实现下面的appendTo()函数并不能按预期工作.基本上,它添加了元素并立即再次删除它.它眨眼,你想念它的东西.

任何人都可以理解为什么会这样吗?

这是调用函数的位置:

<?php foreach ($words as $word) {
echo    "<li class='$word[0]'><a href='' onclick='add_to();'>$word</a></li>";
} 
Run Code Online (Sandbox Code Playgroud)

这里是函数本身(几乎取自jQuery教程网站:

function add_to () {
        $('<h1>Test</h1>').appendTo('.ad_text');
    }
Run Code Online (Sandbox Code Playgroud)

我的第一个想法是调用一个调用document.ready()的脚本,它会清除add_to()函数?该脚本位于add_to()之上,如下所示:

$(document).ready(function(){

        //when a link in the filters div is clicked...
        $('#filters a').click(function(e){

            //prevent the default behaviour of the link
            e.preventDefault();

            //get the id of the clicked link(which is equal to classes of our content
            var filter = $(this).attr('id');

            //show all the list items(this is needed to get the hidden ones shown)
            $('#content ul li').show();

            /*using the :not …
Run Code Online (Sandbox Code Playgroud)

php jquery appendto

0
推荐指数
1
解决办法
413
查看次数

JS:在文本区域问题中添加文本

嗨,我正在尝试实现在这个问题上提出的解决方案: 使用jQuery将文本插入textarea

但是不要紧张.

将一些虚拟文本添加到textarea元素时,此平面函数可以正常工作:

function add_to() {
        $('#ad_textarea').val($('#ad_textarea').val()+'test');
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试将此函数连接到变量时,它会中断:

function add_to(word) {
            $('#ad_textarea').val($('#ad_textarea').val()+word);
    }
Run Code Online (Sandbox Code Playgroud)

从这行代码调用时:

<?php foreach ($words as $word) {
    echo    "<li class='$word[0]'><a href='#' onclick='add_to('$word');'>$word</a></li>";
    } 
    ?>
Run Code Online (Sandbox Code Playgroud)

我查看了输出的代码,看起来很干净:

<li class='a'><a href='#' onclick='add_to('aardvark');'>aardvark</a></li>
Run Code Online (Sandbox Code Playgroud)

我最终试图让他们在文本区打印出来.谁能发现我的滑倒?

TIA

javascript jquery

0
推荐指数
1
解决办法
92
查看次数