小编use*_*234的帖子

如何在 Mustache 模板中循环遍历 Json 数组

我有一个包含 JSON 对象的数组:

{result" : "OK","data":[
                    {"name" : "henrytest",
                     "id" : "9a3d1faaaac742889a940a6d9df49d16"},
                    {"name" : "henrytest",
                     "id" : "9a3d1faaaac742889a940a6d9df49d16"}, 
                    {"name" : "henrytest",
                     "id" : "9a3d1faaaac742889a940a6d9df49d16"}
                   ]
 }
Run Code Online (Sandbox Code Playgroud)

我试图循环遍历数组以获取表中显示的 3 个字段。然而什么也没有显示。这是我的胡子模板:

<table style="width:100%;">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>User ID</th>

                            </tr>
                        </thead>
                        <tbody>
                            {{#data}}
                                <tr>

                                    <td>{{name}}</td>
                                    <td>{{id}}</td>

                                </tr>
                              {{/data}}

                        </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

我无法显示表中的任何字段。陷入困境..:( :(有什么想法可以实现这一点吗?

jquery mustache

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

如何在EXTJS中循环遍历数组时获取范围

我有一个数组,我需要迭代来获取一个值.

    someFunc: function(item) {
          var array = Ext.select(".item").elements;
          var elements;
          var itemId = item.id;
          Ext.each(array, function(ob){
            if (ob.id === itemId) {

              elements = ob.getElementsByTagName("input");
              Ext.each(elements, function(att){
                if (att.getAttribute("name") === "filter") {
                  console.log(att); 
                 this.callFunction();
    // here I want to call a function to do some stuff
    // but `"this"` here refers to input element
   //I should be able to call the function outside the someFunc, but I'm loosing scope
                    }

              });
            }


            });
        },

callFunctionL function() {

}
Run Code Online (Sandbox Code Playgroud)

如何在迭代数组数组时保持范围?

javascript extjs

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

如何将日期与"nd","rd","th"转换为yyyy-mm-dd格式Javasciript

我有一系列日期,如:

dates=['2nd Oct 2014','20th Oct 2014','20th May 2019']
Run Code Online (Sandbox Code Playgroud)

我希望将它们作为YYYY-MM-DD格式的数组返回.到目前为止我有这个:

function formatDate(dates) {
var t=[];
dates.map(function(date){
 var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;
    t.push([year, month, day].join('-'));
});
console.log(t);


    return t;
}
var dates = ['2nd Oct 2014','20th Oct 2014','20th May 2019']
 console.log(formatDate(dates));
Run Code Online (Sandbox Code Playgroud)

但这给了我:

[NaN-NaN-NaN,NaN-NaN-NaN,NaN-NaN-NaN]
Run Code Online (Sandbox Code Playgroud)

因为它无法用"th","nd"来识别日期.如何解决这个问题,以便我得到o/p为:

[2014-10-02,2014-10-20,2019-05-20]
Run Code Online (Sandbox Code Playgroud)

这里是小提琴:http://jsfiddle.net/nakpchvf/ …

javascript

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

如何在具有多个表的树中找到 &lt;td&gt; 的最大计数-JS

我在 dom 树中有多个表,我想计算每个表中的单元格数。

代码:

function tree() {
    var table = document.getElementsByTagName('table');
    for (var p = 0, tr; tr= table[p]; p++) {
      for (var i =0, row; row = tr.rows[i]; i++) {
         console.log(row.cells.length)
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

控制台日志(树())

html:

<table>
   <tr>
            <td>First</td>
            <td>row</td>
        </tr>
        <tr>
            <td>and</td>
            <td>second</td>
        </tr>
</table>
<table>
   <tr>
            <td>First</td>
            <td>row</td>
        </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

第一个有 4 个单元格,第二个有 2 个单元格,我想打印出表格中计数最大的单元格。因此,o/p 将是:

4
Run Code Online (Sandbox Code Playgroud)

我上面的js代码,返回一个表中所有单元格的总长度。如何遍历树中的表格并找到最大的单元格数?

谢谢!

javascript

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

未定义字符串时如何避免“未定义”出现在字符串中 - Javascript

我有 3 个动态变量,可能是nullor undefined

例子:

var str1= " hello";
var str2= " world";
var str= " how are you?";
Run Code Online (Sandbox Code Playgroud)

现在我将这些字符串连接在一起以在我的搜索列表中形成一个查询:

query = str1&& str1 + str2&& str2 + str3&& str3; //o/p: "hello world how are you?
Run Code Online (Sandbox Code Playgroud)

结果很好,但是在任何情况下,当 str 值之一为 null 或为空时,当我连接这些字符串时,我会得到这个:

query = str1&& str1 + str2&& str2 + str3&& str3; // o/p: "hello world undefined"
Run Code Online (Sandbox Code Playgroud)

我怎样才能避免这个 undefined 出现在我的字符串中?

javascript

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

如何在nvd3图例和图表之间添加间隙

我知道之前可能会问过这个问题,而且当我试图找到这个问题的答案时,我还没找到.类似于这里的小提琴:http://jsfiddle.net/rdesai/vfe2B/51/,我试图通过以下方式增加图例和图表之间的差距:

  graph.select(".nv-wrap.nv-legendWrap nv-legend nv-series").attr("transform", "translate(100,75)").style('padding','200px');
  graph.select(".nv-wrap.nv-lineChart").attr("transform", "translate(100,75)");
Run Code Online (Sandbox Code Playgroud)

通过这种方法,我可以移动图表的位置,但是当我点击其中一个图例(打开/关闭圆圈)时,图表会回到原来的位置.我也尝试过:

chart = nv.models.lineChart().options({
                width: 1050
            }).margin({top: 0, right:30, left:80, bottom: 150});
Run Code Online (Sandbox Code Playgroud)

但没有运气..关于如何解决这个问题的任何想法?谢谢!

javascript jquery nvd3.js

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

如何将点击处理程序应用于模板内的多个元素 -EXTJS

我想将点击事件应用于span标签内的标签div

items: [

        tpl: new Ext.XTemplate(
         '<div class="test"> Hello world<span class="icon-gear"></span></div>'
         ),
        listeners: {
          'afterrender': function(this) {
               this.el.on('click', this.onClick, this, { delegate: '.test' });
           },
           'click': function() { this.onSpanClick() }

      }]
Run Code Online (Sandbox Code Playgroud)

下面是点击函数:

onClick: function (e) {
   //some stuff here
},

onSpanClick: function() {
 //some stuff here
}
Run Code Online (Sandbox Code Playgroud)

现在,在上述情况下,当单击容器时class="test",它会进入 onClick() 并按预期工作。但是,当单击 span 元素时,它仍然会转到 ,onClick()而不是转到 onSpanClick()。有没有办法区分来​​自不同元素的点击?

谢谢!

javascript extjs

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

如何从字符串中获取单独的字符

我有一个字符串:

string = "abc_test_dashboard.json";
Run Code Online (Sandbox Code Playgroud)

字符串的值可能会有所不同:

"tes.test.dashboard.json"
"jhgwefj-gfjh.widget.json"
Run Code Online (Sandbox Code Playgroud)

最后一个"dashboard.json"和"widget.json"是静态的,可能是其中任何一个,具体取决于条件.

基本上我正在尝试从字符串中识别它的"仪表板"或"小部件".

我想做的事基于:

if ("dashboard.json") {//do some stuff}
else { // do something else
}
Run Code Online (Sandbox Code Playgroud)

我也意识到我可能有多个同名文件,因此我最终可能得到(1),(2)后缀,即:"abc_test_dashboard(1).json","abc_test_dashboard(2).json".有没有办法测试这种情况?

谢谢

javascript

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

标签 统计

javascript ×7

extjs ×2

jquery ×2

mustache ×1

nvd3.js ×1