0

I have a 5x5, table in HTML, that is, each tr has 5 td's, and each td has one input field, which i want to parse for value, also each td has data attributes for data-row and data-column. this is what i have come up with, but it's buggy, how do i do it?

tds = $('td')
marker = 0
thisSet = []
table = []

for td in tds
 thisRow = parseInt($(td).attr('data-row'))

 if marker == thisRow
  rc = "#{$(td).attr('data-row')}-#{$(td).attr('data-column')}"
  thisSet.push ({data: rc})
  console.log "marker:#{marker}, thisRow:#{thisRow}"
else
  rc = "#{$(td).attr('data-row')}-#{$(td).attr('data-column')}"
  thisSet.push ({data: rc})
  marker = thisRow 
  console.log "marker:#{marker}, thisRow:#{thisRow}"
  table.push thisSet
  thisSet = []

console.log table
console.log _.flatten(table).length

UPDATE: ok, worked a little more on it, now i have 4 rows parsed, not the 5th row, theres something missing, but 4 rows parse fine.

tds = $('td')
currentRow = 0
thisSet = []
table = []
for td in tds
  thisRow = parseInt($(td).attr('data-row'))
  rc = "#{$(td).attr('data-row')}-#{$(td).attr('data-column')}"

 if currentRow != thisRow
  table.push thisSet
  thisSet = []
  thisSet.push ({data: rc})
  currentRow = thisRow 
else
  thisSet.push ({data: rc})

console.log table
console.log _.flatten(table).length
5
  • For one thing, you're lacking the {} in your for...in loop and also your if() {} else {} logic. Commented Nov 3, 2013 at 5:13
  • @joshstrike note the coffeescript tag Commented Nov 3, 2013 at 5:14
  • @koala_dev ...Ahh. sorry. Missed that Commented Nov 3, 2013 at 5:15
  • So you want to end up with a multi-dimensional array corresponding to the table, with the contents of each element simply being "[row]-[column]"? Commented Nov 3, 2013 at 5:46
  • yes micha! something like this: [[Object { data="0-0"}, Object { data="0-1"}, Object { data="0-2"}, 2 more...], [Object { data="1-0"}, Object { data="1-1"}, Object { data="1-2"}, 2 more...], [Object { data="2-0"}, Object { data="2-1"}, Object { data="2-2"}, 2 more...], [Object { data="3-0"}, Object { data="3-1"}, Object { data="3-2"}, 2 more...]] Commented Nov 3, 2013 at 5:48

1 Answer 1

1

I might do it like this:

table = []
table.push([]) for num in [0...5]

tds = $('td')

for td in tds
  row = parseInt(td.attr(data-row))
  col = parseInt(td.attr(data-column))

  table[row][col] = { data: "#{row}-#{col}" }

console.log table
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.