# A table based grid - please let me know if I'm reinventing the wheel here

**URL:** <https://www.ruby-forum.com/t/a-table-based-grid-please-let-me-know-if-im-reinventing-the-wheel-here/176976>\
**Category:** Rails\
**Created:** [October 9, 2009, 8:24am UTC](https://www.ruby-forum.com/t/a-table-based-grid-please-let-me-know-if-im-reinventing-the-wheel-here/176976 "2009-10-09T08:24:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![C\_K\_Kashyap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@C\_K\_Kashyap](https://www.ruby-forum.com/u/C_K_Kashyap)\
**Post date:** [October 9, 2009, 8:24am UTC](https://www.ruby-forum.com/t/a-table-based-grid-please-let-me-know-if-im-reinventing-the-wheel-here/176976/1 "2009-10-09T08:24:14Z")

</div>

Hi Folks,  
Since I did not find an editable grid that suits my requirement (of  
being light weight and editable) I started writing  
my own - here’s what I’m trying -

A helper function like this that renders a grid -

def mygrid(row,col)  
table=’

’  
table \<\< “”  
1.upto col do |c|  
table \<\< “”  
end  
table \<\< “”  
1.upto row do |r|  
table \<\< “”  
1.upto col do |c|  
table \<\< “”  
end  
table \<\< “”  
end  
table \<\< “

| Col #{c} |
| --- |
| \<input id=“col\_#{r}\_#{c}” style=“border:none;”  
type=“text”  
onkeydown=“handle\_keyboard(event,#{r},#{c},#{row})”\> |

”  
end

And the supporting js function -

function handle\_keyboard(e,r,c,max\_r){  
var code=e.keyCode;  
if(code==13 || code==40){  
r=r+1;  
if(r\>max\_r)r=1;  
id=‘col\_’+r+"_"+c;  
document.all[id].focus()  
}  
if(code==38){//up  
r=r-1;  
if(r\<1)r=1;  
id='col_’+r+"\_"+c;  
document.all[id].focus()  
}  
}

I intend to add Ajax to it - but before that I wanted to understand if  
I’m doing all this and there’s a better solution already out there.

–  
Regards,  
Kashyap

---

<div class="post-metadata">

**Author:** ![C\_K\_Kashyap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@C\_K\_Kashyap](https://www.ruby-forum.com/u/C_K_Kashyap)\
**Post date:** [October 9, 2009, 8:01pm UTC](https://www.ruby-forum.com/t/a-table-based-grid-please-let-me-know-if-im-reinventing-the-wheel-here/176976/2 "2009-10-09T20:01:57Z")

</div>

C K Kashyap wrote:

> Hi Folks,  
> Since I did not find an editable grid that suits my requirement (of  
> being light weight and editable) I started writing  
> my own - here’s what I’m trying -
> 
> A helper function like this that renders a grid -  
> […]

I would suggest that you might want to put this into a couple of  
partials instead. Or at least use content\_tag.

[…]

> ```
> table << "<td><input id=\"col_#{r}_#{c}\" style=\"border:none;\"
> 
> ```

Why not use a class instead of an inline style?

> type="text"  
> onkeydown="handle\_keyboard(event,#{r},#{c},#{row})"\>"

Get your JS out of your HTML. It belongs in a separate file.

[…]

> –  
> Regards,  
> Kashyap

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
