关灯

Output order changes after the event triggered

  [复制链接]
区块链技术 发表于 2018-12-31 21:44:25 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
 
" candidatesResults.append(candidateTemplate); }); }

Please Help !!!

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x

I'm new to solidity so for educational purpose I'm developing EVoting application. I've written a smart contract for candidate registration and also developed UI for getting the registered candidate list in the form of table on web page.

I've written event such that after registering new candidate the event triggers and by using which I'm adding that record to the table dynamically.

Now what happen is whenever event triggers I get newly added candidate in the table but the order of candidates changes i.e. table rows take random positions like instead of 1,2,3,4 it shows 3,4,2,1 etc.

Then I need to refresh page several times to get it done properly.

This is my table code,

# Name Age Street Address District State Pincode Vote Count

This is the JavaScript to add records to table,

App.contracts.RegisterCandidate.deployed().then(function(instance) 
      {
        registerCandidateInstance = instance;
        return registerCandidateInstance.candidatesCount();
      }).then(function(candidatesCount) {
        var candidatesResults = $("#candidatesResults");
        candidatesResults.empty();


        for (var i = 1; i <= candidatescount;="" i++)="" {="" registercandidateinstance.getcandidate(i).then(function(candidate)="" var="" id="candidate[0].toNumber();" name="candidate[1];" age="candidate[2].toNumber();" street_address="candidate[3];" district="candidate[4];" state="candidate[5];" pincode="candidate[6].toNumber();" votecount="candidate[7].toNumber();" render="" candidate="" result="" candidatetemplate="
" +="" "<="" th="">" + name + "" + age + "" + street_address + "" + district + "" + state + "" + pincode + "" + voteCount + "



上一篇:Just in time conversion to ETH in dApp
下一篇:Contract methods calls return empty array
版权申明:此文如未标注转载均为本站原创,自由转载请注明 本贴地址:https://xuesq.cn/thread-109395-1-1.html 上篇帖子: 声音 | V神回复Tuur Demeester:绝对的不可逆性是错误的,分叉不一定导致灾难 ... 下篇帖子: Contract methods calls return empty array order changes after
回复

使用道具 举报

 

精彩评论1

正序浏览
区块链技术 发表于 2018-12-31 22:02:37 | 显示全部楼层
 

Since your calls to getCandidate() are asynchronous - you're calling each item without waiting for the previous result - you can't be sure that the order you request the data will match the order you get the data back.

Add some extra logic to make sure your table shows the rows in the right order. For instance:

  • Add an attribute to each row called data-id, set to the ID of the candidate you'd adding.
  • When you add a new row, instead of just appending it to the end, iterate the rows in the table backwards until you find a row with a lower data-id than the ID of the row you are adding (or until you run out of rows)
  • Add your new row after that row.

Alternatively, if your contract has a call to tell you the number of candidates and you know there are no gaps, you might be better calling that first, rendering the table with the correct number of rows but no data, and then making the calls for data for each candidate, and using it to populate the row with the right index (eg if you get candidate #6, populate row #6 in your table).

回复

使用道具 举报

 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

排行榜

关注我们:微信订阅号

官方微信

APP下载

全国服务邮箱:

admin@xuesq.cn

公司地址:重庆南岸区学府大道

运营中心:重庆微生派网络工作室

邮编:610066 Email:1401171837#qq.com

Copyright   ©2017-2018  中比特Powered by©xuesq.cn!技术支持:中比特    ( 蜀ICP备16009247号-1 )