我正在使用数据表来显示来自 mySQL 数据库的数据。我想要做的是捕获当前行的值并将其显示在模态中以进行编辑。
这就是我将数据输入数据表的方式,
$(document).ready(function() {
$('#loading-span7').fadeOut();
$('#loading-span5').fadeOut();
$('#add-company').hide();
$.fn.dataTableExt.sErrMode = 'throw';
$('#company').dataTable({
"bProcessing" : true,
"bServerSide" : true,
"sAjaxSource" : "/index.php/company/profile_data",
"sDom" : "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
"sPaginationType" : "bootstrap",
"aoColumns" : [{
"mData" : "id"
}, {
"mData" : "name"
}, {
"mData" : "package_id"
}, {
"mData" : function(oObj) {
return "<a href=\"#\" data-id=\"" + oObj.id + "\" data-name=\"" + oObj.name + "\" role=\"button\" class=\"btn edit-company\">Edit</a></td>";
}
}, {
"mData" : function(oObj) {
return "<a href=\"#\" data-id=\"" + oObj.id + "\" data-name=\"" + oObj.name + "\" role=\"button\" class=\"btn view-users\" >Users</a></td>";
}
}]
});
$.extend($.fn.dataTableExt.oStdClasses, {
"sWrapper" : "dataTables_wrapper form-inline"
});
这是模态,
<div id="edit_company" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
×
</button>
<h3 id="myModalLabel"><span id="company-label"></span>Company</h3>
</div>
<div class="modal-body">
<form id="company-form">
<fieldset>
<label>Name</label>
<input type="text" placeholder="Type something…" name="company-name">
<label>Package</label>
<select name="package" name="package-id" id="package-id">
</select>
</fieldset>
</form>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">
Close
</button>
<button class="btn btn-primary">
Save changes
</button>
</div>
</div>
这是我试图实现目标的脚本,但是问题是只有数据表的第一行被捕获到模态中
$('#company .edit-company').live('click', function() {
var id = $(this).attr('id');
$('#company-label').html('Edit ');
$('#company-form')[0].reset();
$.ajax({
url : '/index.php/company/profile_data',
type : 'POST',
data : 'companies[]=' + id,
beforeSend : function() {
$('#loading-span7').show();
},
success : function(data, textStatus, xhr) {
var details = JSON.parse(data);
console.log(details);
//console.log(details.aaData[0].homepage_url);
$('input[name=company-name]').val(details.aaData[0].name);
//$('input[name=package-id]').html(details.aaData[0].package_id);
/**oTable.$('.edit-company').click(function() {
var data1 = oTable.fnRowSelected(this);
$('input[name=company-name]').val(details.aaData[0].name);
});**/
var option1 = $('<option />', {
text : details.aaData[0].package_id
}), option2 = $('<option />', {
text : details.aaData[1].package_id
});
$('select#package-id').append(option1, option2);
//$('select#package-id option').text(details.aaData[0].package_id);
//$('#package-id').val(details.aaData[0].package_id);
$('#edit_company').modal('show');
$('#loading-span7').fadeOut();
},
error : function(xhr, textStatus, errorThrown) {
//alert('error');
}
});
});
我能做些什么来实现我的目标?