找到你要的答案

Q:Google Channel API - Jquery update table

Q:谷歌渠道API jQuery更新表

I am attempting to create a live timing table due to restrictions I have to use the google channel api. So I can receive messages fine that all works I am trying to add or update code but my issues is the common one of added elements not being accessible using jquery I know this is because of the way the dom works but I am looking for a way round it here it my current code I know it won't work but all the answers I find talk aboit buttons I am not using buttons or clicks it just updates onMessage. All I am looking to do is add a row if it doesn't exist and update a row if it does but the issues is that it is all live and the dom doesn't see them because they are added afterwards.

You can asume that the name is unique for each entry so it can be used as a id.

if($('#tr_' + messageParsed.name).length)
{
    html = "<td>" + messageParsed.name + "</td><td>" + messageParsed.lap + "</td><td>" + messageParsed.pos + "</td><td>" + messageParsed.s1 + "</td><td>" + messageParsed.s2 + "</td><td>" + messageParsed.s3 + "</td>";
    console.log('Update');
    $('#tr_' + messageParsed.name).html(html);
}
else {
    html = "<tr id=\"tr_" + messageParsed.name + "\"><td>" + messageParsed.name + "</td><td>" + messageParsed.lap + "</td><td>" + messageParsed.pos + "</td><td>" + messageParsed.s1 + "</td><td>" + messageParsed.s2 + "</td><td>" + messageParsed.s3 + "</td></tr>";
    $("#times").append(html);
 }

Here is the html for my table it's super simple as everything that is loaded into the table comes from the channel api, somone was bound to ask for it so I thought I would add it straigt away.

<table class="table table-striped">
    <thead>
       <tr>
            <th>Name</th>
            <th>Lap</th>
            <th>Position</th>
            <th>Sector 1</th>
            <th>Sector 2</th>
            <th>Sector 3</th>
        </tr>
    </thead>
    <tbody id="times">
    </tbody>
</table>

我试图创建一个实时时序表由于限制我不得不使用谷歌通道API。所以我可以接收消息好所有作品我试图添加或更新代码,但我的问题是常见的一种添加元素不使用jQuery,我知道这是因为路上的DOM的作品但是我想找一个方法在这里我目前的代码,我知道它不会工作,但所有的答案我发现谈话aboit按钮我不使用按钮或点击它只是更新消息。所有我想做的就是添加一行如果不存在更新的如果它不但是问题是所有生活和DOM没有看到他们因为他们是后来加上去的。

你可以认为这个名字是每一项独特的可作为ID。

if($('#tr_' + messageParsed.name).length)
{
    html = "<td>" + messageParsed.name + "</td><td>" + messageParsed.lap + "</td><td>" + messageParsed.pos + "</td><td>" + messageParsed.s1 + "</td><td>" + messageParsed.s2 + "</td><td>" + messageParsed.s3 + "</td>";
    console.log('Update');
    $('#tr_' + messageParsed.name).html(html);
}
else {
    html = "<tr id=\"tr_" + messageParsed.name + "\"><td>" + messageParsed.name + "</td><td>" + messageParsed.lap + "</td><td>" + messageParsed.pos + "</td><td>" + messageParsed.s1 + "</td><td>" + messageParsed.s2 + "</td><td>" + messageParsed.s3 + "</td></tr>";
    $("#times").append(html);
 }

这里是我的HTML表超级简单,所有加载到表来自通道API,某人会问了所以我想我会把它直了。

<table class="table table-striped">
    <thead>
       <tr>
            <th>Name</th>
            <th>Lap</th>
            <th>Position</th>
            <th>Sector 1</th>
            <th>Sector 2</th>
            <th>Sector 3</th>
        </tr>
    </thead>
    <tbody id="times">
    </tbody>
</table>
answer1: 回答1:

For anyone that is interested in how I solved this I worked out a way using java variables to check if they exist and generate a new table every time it's not that efficient but it works.

 if($.inArray(messageParsed.name, timing) !== -1)
        {
          var elmNum = $.inArray(messageParsed.name, timing);
          timingStore[elmNum] = messageParsed;
        }
        else {
          timing.push(messageParsed.name);
          timingStore.push(messageParsed);
        }
        // String Construction
        var htmlString;
        console.log(timingStore)
        for(var i = 0; i < timingStore.length; i++)
        {
          htmlString += "<tr><td>" + timingStore[i].name + "</td><td>" + timingStore[i].lap + "</td><td>" + timingStore[i].pos + "</td><td>" + timingStore[i].s1 + "</td><td>" + timingStore[i].s2 + "</td><td>" + timingStore[i].s3 + "</td></tr>";
        }
        $('#times').html(htmlString)

对于任何人都是怎么解决这个我工作了用java变量是否存在和生成新表每次不是有效的但它的作品感兴趣。

 if($.inArray(messageParsed.name, timing) !== -1)
        {
          var elmNum = $.inArray(messageParsed.name, timing);
          timingStore[elmNum] = messageParsed;
        }
        else {
          timing.push(messageParsed.name);
          timingStore.push(messageParsed);
        }
        // String Construction
        var htmlString;
        console.log(timingStore)
        for(var i = 0; i < timingStore.length; i++)
        {
          htmlString += "<tr><td>" + timingStore[i].name + "</td><td>" + timingStore[i].lap + "</td><td>" + timingStore[i].pos + "</td><td>" + timingStore[i].s1 + "</td><td>" + timingStore[i].s2 + "</td><td>" + timingStore[i].s3 + "</td></tr>";
        }
        $('#times').html(htmlString)
javascript  jquery  channel-api