Javascript 我如何组织这种逻辑
请帮帮我 我想创建可编辑的数据表。在表的顶部有一个按钮“Add”,它使用javascript将行添加到表中。在每一行应该是Buton(链接)“保存”和“删除” 如何向服务器发送post请求 对不起,我太迟钝了 在application.js中,我有这样一个函数Javascript 我如何组织这种逻辑,javascript,ruby-on-rails,ajax,Javascript,Ruby On Rails,Ajax,请帮帮我 我想创建可编辑的数据表。在表的顶部有一个按钮“Add”,它使用javascript将行添加到表中。在每一行应该是Buton(链接)“保存”和“删除” 如何向服务器发送post请求 对不起,我太迟钝了 在application.js中,我有这样一个函数 function AddElementsToPage(elements) { /*elements - is array with name elements whose will be add to page*/ this.ad
function AddElementsToPage(elements) {
/*elements - is array with name elements whose will be add to page*/
this.add_row_to_data_table = function(obj,attributes,selector_table){
last_row_of_table = selector_table.find('tbody tr').last();
var new_row = "<tr><th scope='row'></th>";
if ($.isEmptyObject(last_row_of_table.html()))
{
$.each(attributes, function(index, item){
input_tag = "<input id=\""+obj+"_"+item+"\" name=\""+obj+"["+item+"]\" size=\"10\" type=\"text\" />";
new_row = new_row + "<td>"+input_tag+"</td>";
})
alert(new_row);
selector_table.append(new_row+"</tr>");
}else
{
alert("not empty");
}
}
我的新.js文件
var obj = new AddElementsToPage();
obj.add_row_to_data_table("sch_of_working",["date","hour"], $('.data_table'))
我的索引
= link_to content_tag('span', "Add" , :class=>"add"), new_sch_of_work_information_path, :remote => true
%table{:border=>"1", :class=>"data_table", :style=>"width:450px"}
%thead
%th{:style=>"width:5%;"} №
%th{:style=>"width:10%;"} Date
%th{:style=>"width:10%;text-align:center;"} schedule_code
%th{:style=>"width:2%;",:class=>"transperent_right_border"}
%th{:style=>"width:2%;"}
%tbody
%tr
%th{:scope=>"row"}
%td=@sch.date
%td=@sch.schedule_code
%td{:style=>"text-align:center"}= link_tag "save", sch_of_work_informations, #how can send post request in here
%td{:style=>"text-align:center"}
我想这是你会看到的答案。按照railscasts嵌套表单中的教程进行操作,应该会对您有所帮助。基本上你会有一个添加行按钮,它会在javascript中添加html行,你甚至可以有一个删除行按钮。完成后,只需按表单的“保存”按钮即可完成所有更改。这是一个非常普遍的问题。请发布一些代码,向我们展示您的尝试,并询问一些具体问题。Utkanos,我想创建可编辑的数据表。当我单击表顶部的“添加”按钮时,它应该是带有输入标记的“添加行”。这一行应该包含一个按钮保存(或链接)。我现在不知道如何组织这个,但你还没有发布任何代码,也没有向我们展示你在做什么。你实际上是在要求一个完整的解决方案,而这不是问题所在。向我们展示你所做的尝试,如果你在某些特定方面需要帮助,我们可以提供帮助。同样,你的问题中没有任何东西可以证明ruby或Ajax是正确的。请具体回答我的问题
= link_to content_tag('span', "Add" , :class=>"add"), new_sch_of_work_information_path, :remote => true
%table{:border=>"1", :class=>"data_table", :style=>"width:450px"}
%thead
%th{:style=>"width:5%;"} №
%th{:style=>"width:10%;"} Date
%th{:style=>"width:10%;text-align:center;"} schedule_code
%th{:style=>"width:2%;",:class=>"transperent_right_border"}
%th{:style=>"width:2%;"}
%tbody
%tr
%th{:scope=>"row"}
%td=@sch.date
%td=@sch.schedule_code
%td{:style=>"text-align:center"}= link_tag "save", sch_of_work_informations, #how can send post request in here
%td{:style=>"text-align:center"}