使用Datatable行信息更新文本输入并从对话框中编辑行

时间:2013-10-01 14:09:51

标签: javascript jquery jquery-ui input datatables

我想要从对话框中编辑数据表。用户应选择一行,然后打开对话框。对话框文本输入应与所选行td匹配。从这里,您可以编辑/更改输入(通过单击编辑启用字段)并保存更改以更新表。 http://jsfiddle.net/BWCBX/5/我有工作代码来选择一行(在这种情况下删除它)操纵它。如何更新文本输入并从所述输入中编辑表格?提前谢谢。

    var oTable;

    /* Add a click handler to the rows - this could be used as a callback */
    $("#example tbody tr").click( function( e ) {
        if ( $(this).hasClass('row_selected') ) {
            $(this).removeClass('row_selected');
        }
        else {
            oTable.$('tr.row_selected').removeClass('row_selected');
            $(this).addClass('row_selected');
        }
    });


    /* Add a click handler for the delete row */
    $('#delete').click( function() {
        var anSelected = fnGetSelected( oTable );
        if ( anSelected.length !== 0 ) {
            oTable.fnDeleteRow( anSelected[0] );
        }
    } );


    /* Init the table */
    oTable = $('#example').dataTable( );



/* Get the rows which are currently selected */
function fnGetSelected( oTableLocal )
{
    return oTableLocal.$('tr.row_selected');
}

1 个答案:

答案 0 :(得分:2)

您可以像这样更新输入和数据 HTML将id添加到输入

<div id="dialog" title="Properties">
  <table>
        <tbody>
          <tr>
            <th>Rendering engine</th>
            <td><input id="rendering" type='text'  /></td>
          </tr>
          <tr>
            <th>Browser</th>
            <td><input id="browser" type='text'  /></td>
          </tr>
        </tbody>
      </table>
    <input type='button' value='Edit' class='editBtn' />
    <input type='button' value='Save Changes' class='saveBtn' />
</div>     

JS

var properties;//all td from .row_selected
$( "#opener" ).click(function() {
              properties=fnGetSelected( oTable ).find("td"); //update selected row
              $( "#dialog" ).dialog( "open" );
              $( ".saveBtn" ).hide();
              //update the input fields 
              $("#rendering").val(properties.eq(0).html());
              $("#browser").val(properties.eq(1).html());
              $( ".editBtn" ).show();
              $("div#dialog input:text").attr("disabled", "disabled");  
          });
//update the dataTable with the input values and close #dialog
$( ".saveBtn" ).click(function() {
    properties.eq(0).html($("#rendering").val());
    properties.eq(1).html($("#browser").val());
    $( "#dialog" ).dialog( "close" );
});    

http://jsfiddle.net/BWCBX/6/