【问题标题】:Clicking on sort, removes data from datatable. Reactjs Javascript单击排序,从数据表中删除数据。 Reactjs Javascript
【发布时间】:2017-10-19 09:58:17
【问题描述】:

我在反应中使用数据表。当我点击排序(在 tHead 中)时,它会删除所有数据并显示 table 中没有可用数据

我在这里的想法是,tbody 是在 Datatable 初始化之前呈现的。或类似的东西。

这是我在PasteBin中的代码

这是componentDidMount

的一些代码
componentDidMount(){
        employessData = thisclass.props.data;

        thisclass.setState({
            stateReady: true
        })
        var self = this;
        $('#mytableEmp').dataTable({
            "columnDefs": [
                { "width": "20px", "targets": 0 }
            ],
            "pagingType": "numbers",
            "bAutoWidth": false,
            "bDestroy": true,       
            "fnDrawCallback": function() {              
                self.forceUpdate();         
            }
        });
        $('#checkboxhead').removeAttr('area-sort').removeClass('sorting_asc');
        $(".checkAllCBEmp").click(function () {
            $('#mytableEmp tbody input[type="checkbox"]').prop('checked', this.checked);
        });

        $("#searchtabletb").on("keyup", function() {
            var value = $(this).val();
            value = value.toLowerCase();
            console.log(value);
            $("#mytableEmp tr").each(function(index) {
                if (index != 0) {
                    var row = $(this);

                    var id = row.find("td").text();
                    id = id.toLowerCase();
                    if (id.indexOf(value) === -1) {
                        $(this).hide();
                    }
                    else {
                        $(this).show();
                    }
                }
            });
        });
}

这是 render 函数的代码。

  render () {

    return (


                <table className="table table-stripped" id="mytableEmp">
                  <thead>
                    <tr className="table-head-row">
                      <th id="checkboxhead" className="firsttd">
                        <div className="round-cb">
                            <input type="checkbox" id="cb1_emp" className="checkAllCBEmp" />
                            <label htmlFor="cb1_emp"></label>
                        </div>
                      </th>
                      <th>Title</th>
                      <th>Type</th>
                      <th>Created on</th>
                      <th>From</th>
                      <th>To</th>
                      <th>Processed</th>
                      <th>Amount</th>
                      <th>Status</th>
                      <th id="detailarrowhead" className="lasttd"></th>
                    </tr>
                  </thead>
                  <tbody>
                  {
                    this.state.stateReady ? 
                        employessData.map((heading, index) => {
                        return(<tr role="row" className="tablerow" key={index}>
                          <td className="firsttd">
                            <div className="round-cb">
                                <input type="checkbox" value="cb1" id={heading.userId} />
                                <label htmlFor={heading.userId}></label>
                            </div>
                          </td>
                          <td>

                            <div className="emp-avatar">
                                <img src={heading.profile_picture} />
                            </div>

                            <div className="emp-title-div">
                             <div className="emp-title">
                               <div>
                                 {heading.name}
                               </div>
                             </div>
                           </div>
                          </td>
                          <td>c</td>
                          <td>Texto 2</td>
                          <td>Texto 2</td>
                          <td>Texto 2</td>
                          <td>Texto 2</td>
                          <td>Texto 2</td>
                          <td><span className="badge badge-red">Rejected</span></td>
                          <td className="lasttd"><Link to="/emp-profile" className="table-details-btn" onClick={() => {this.addlocalid(heading.userId)}}>View User</Link></td>
                        </tr>)
                    })
                    :
                    ""
                  }
                  /tbody>
                </table>

    )
  }

【问题讨论】:

  • 附带的问题是“我真的应该将 React 与 jQuery 一起使用来构建我的 DOM”吗?答案是“不”。

标签: javascript jquery reactjs datatable


【解决方案1】:

employessData 的范围很奇怪,所以它是thisclass

您将希望在 render 中执行此操作:

let employessData = this.props.data;
this.state.stateReady ? 
    employessData.map(...);

然后了解该组件接收到的this.props.data 是否/为什么为空(此处不可见)。

【讨论】:

    【解决方案2】:

    我暂时已经解决了这个问题。

    我在以下功能中添加了 3 秒的延迟

    setTimeout(function(){    
    $('#mytableEmp').dataTable({
                    "columnDefs": [
                        { "width": "20px", "targets": 0 }
                    ],
                    "pagingType": "numbers",
                    "bAutoWidth": false,
                    "bDestroy": true,       
                    "fnDrawCallback": function() {              
                        self.forceUpdate();         
                    }
                });
    }, 3000);
    

    【讨论】:

    • 这意味着您正在通过执行不到 3 秒的 Ajax 调用获取数据。不仅这个修复方法很丑陋,而且如果有一天它需要 3.1 秒,桌子就会再次变空。
    • 没错,我能理解。但是我现在没有其他解决方案。你能推荐一个吗?谢谢。
    • 由于我们无法从您上面的代码中看到您如何将道具 data 传递给您的组件,因此没有解释为什么 this.props.data 变为空。在任何情况下,修复变量的范围肯定会有所帮助。删除这些全局employessDatathisclass
    猜你喜欢
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多