感谢您的回复,我能够通过使用 knockout.js 点击处理程序进行一些更改来解决这个问题。我还必须更改 jeditable 以使用按钮来触发可编辑事件。通常我使用 jeditable 使所有表格都可编辑。 Jeditable 做到了,所以当你调用它时,它会处理被点击项目的点击事件。
这是我的最终 HTML
<table class="table table-striped table-bordered table-hover tablesorter" id="deliveryItemsTable" data-bind="visible: showTable">
<thead>
<tr class="title1">
<td colspan="100%">Delivery Items</td>
</tr>
<tr class="title2">
<th class="header">Created On</th>
<th class="header">Company</th>
<th class="header">Item Number</th>
<th class="header">Phone Number</th>
<th>Price</th>
<th class="header">Location</th>
<th class="header">Notes</th>
<th class="header">Delivery Driver</th>
<th class="header">Status</th>
<th class="header nowrap"></th>
</tr>
</thead>
<tbody data-bind="foreach: deliveryItems">
<tr data-bind="attr: {id: 'row_' + id }">
<td data-bind="text: created_on"></td>
<td data-bind="text: company_name"></td>
<td data-bind="text: product_number_company"></td>
<td data-bind="text: client_phone_number"></td>
<td data-bind="text: formatCurrency(price_to_display)"></td>
<td>
<span data-bind="text: location, attr: {id: 'edit-delivery_items-' + id + '_location' }"></span>
<button data-bind="click: $root.editLocation" class="btn btn-default btn-xs" title="Edit"><i class="glyphicon glyphicon-edit"></i></button>
</td>
<td>
<span data-bind="text: notes, attr: {id: 'edit-delivery_items-' + id + '_notes' }"></span>
<button data-bind="click: $root.editNotes" class="btn btn-default btn-xs" title="Edit"><i class="glyphicon glyphicon-edit"></i></button>
</td>
<td>
<span data-bind="text: delivered_by_text, attr: {id: 'edit_select-delivery_items-' + id + '_delivered_by' }"></span>
<button data-bind="click: $root.editDeliveryDriver" class="btn btn-default btn-xs" title="Edit"><i class="glyphicon glyphicon-edit"></i></button>
</td>
<td data-bind="html: status_labels"></td>
<td class="center"><span class="btn-group">
<button data-bind="click: $root.markAsDelivered, visible: $root.canMarkComplete" class="btn btn-default" title="Mark as delivered"><i class="glyphicon glyphicon-check"></i></button>
<a data-bind="attr: {href: 'index.php?action=editDeliveryItem&id=' + id }, visible: $root.canEdit" class="btn btn-default" title="Edit"><i class="glyphicon glyphicon-edit"></i></a>
</span></td>
</tr>
</tbody>
</table>
<div class="alert alert-info" data-bind="visible: showLoadingNotice">
Loading...
</div>
<div id="deliveryItemsTableUpdateNotice"></div>
这是我想出的用于处理表格和可编辑项目的 JS:
var deliveryItemsModel = function () {
var self = this;
self.deliveryItems = ko.observableArray();
self.showTable = ko.observable(false);
self.showLoadingNotice = ko.observable(true);
self.canMarkComplete = ko.observable(false);
self.canEdit = ko.observable(false);
self.getDeliveryItems = function () {
$.getJSON( SITE_URL + '/ajax.php?action=returnDeliveryItemsTableJSON', function(retrievedData) {
self.updateData(retrievedData);
//console.log('back out');
});
};
self.getDeliveryItemsForToday = function () {
var todaysDate = new Date();
$.getJSON( SITE_URL + '/ajax.php?action=returnDeliveryItemsTableJSON&status=not-delivered&scheduled_for_delivery_on=' + (todaysDate.getMonth() + 1) + '/' + todaysDate.getDate() + '/' + todaysDate.getFullYear(), function(retrievedData) {
self.updateData(retrievedData);
//console.log('back out');
});
};
self.updateData = function (data) {
self.deliveryItems(data);
self.showLoadingNotice(false);
self.showTable(true);
//console.log(data);
// We moved this to functions.js since we use it multiple times
setupDeliveryItemsTable();
};
self.replaceData = function (data) {
self.showLoadingNotice(true);
self.showTable(false);
self.updateData(data);
};
/* Click Functions */
self.markAsDelivered = function (deliveryItem) {
bootbox.confirm('Are you sure you want to mark this as delivered?', function(result) {
if ( result == true ) {
self.showLoadingNotice(false);
self.showTable(true);
jQuery.post(SITE_URL + '/ajax.php?action=markDeliveryItemDelivered&id=' + deliveryItem.id, function(data) {
// Trigger the search function to refresh the table
$('#searchDeliveryItemsForm').submit();
});
}
});
};
self.editLocation = function (deliveryItem) {
handleEditOnClick('delivery_items', deliveryItem.id, 'location');
};
self.editNotes = function (deliveryItem) {
handleEditOnClick('delivery_items', deliveryItem.id, 'notes');
};
self.editDeliveryDriver = function (deliveryItem) {
handleEditDeliveryDriverOnClick('delivery_items', deliveryItem.id, 'delivered_by');
};
};
/* Functions we use for the model above */
function handleEditOnClick(table, dbID, name) {
var options = {
event : 'edit',
id : 'cssID',
cancel : '<button class="btn btn-default btn-sm editable-cancel" type="button"><i class="glyphicon glyphicon-remove"></i></button>',
submit : '<button class="btn btn-primary btn-sm editable-submit" type="submit"><i class="glyphicon glyphicon-ok"></i></button>',
indicator : indicatorImage,
tooltip : 'Click to edit...',
style : 'display: inline;',
width : 'none',
type : 'textarea'
};
$('#edit-' + table + '-' + dbID + '_' + name).editable(
SITE_URL + '/ajax.php?action=updateitem&table=' + table + '&item=' + name + '&id=' + dbID,
options
).trigger('edit');
};
function handleEditDeliveryDriverOnClick(table, dbID, name) {
var options = {
event : 'edit',
id : 'cssID',
cancel : '<button class="btn btn-default btn-sm editable-cancel" type="button"><i class="glyphicon glyphicon-remove"></i></button>',
submit : '<button class="btn btn-primary btn-sm editable-submit" type="submit"><i class="glyphicon glyphicon-ok"></i></button>',
indicator : indicatorImage,
tooltip : 'Click to edit...',
style : 'display: inline;',
width : 'none',
type : 'select',
loadurl : SITE_URL + '/ajax.php?action=getDeliveryDriversJSON' + '&id=' + dbID
};
$('#edit_select-' + table + '-' + dbID + '_' + name).editable(
SITE_URL + '/ajax.php?action=updateDeliveryDriverByID&id=' + dbID,
options
).trigger('edit');
};
var viewModel = new deliveryItemsModel();
ko.applyBindings( viewModel );
viewModel.canMarkComplete(true);
viewModel.canEdit(true);
viewModel.getDeliveryItems();
我不确定这是否对其他人有帮助,但我想我会分享最终对我有用的东西。
如果有人看到任何进一步优化的方法,请告诉我。