我正在尝试使用 Javascript 函数从 odoo 中的模块加载数据
控制器.main:
@http.route(['/datatables/model/get_partners'], type='json', auth="user", website=True)
def get_partners(self, offset=0, page_size=500, count_only=False):
data_obj = request.env['res.partner'].sudo()
if count_only:
return [{'count': data_obj.search([('id', '>', 0)], count=True)}]
else:
data_ids = data_obj.search([('id', '>', 0)], offset=offset, limit=page_size)
if not data_ids:
return []
data_tb = []
for data in data_ids:
data_tb.append([
data.name,
data.title if data.title else '',
data.lang if data.lang else '',
1 if data.supplier else 0,
1 if data.employee else 0,
])
logger.debug('Partners current offset: %s', offset)
return data_tb
Javascript函数:
odoo.define('website_datatables.search', function (require) {
"use strict";
var ajax = require('web.ajax');
var core = require('web.core');
var website = require('website.website');
var QWeb = core.qweb;
$('#partners_data tfoot th').each( function () {
if ($(this).index() > 3) {
$(this).html( ' ');
} else {
$(this).html( '<input type="text" placeholder="?.?." />');
}
});
var table = $('#partners_data').DataTable();
table.clear();
var max_return = 500;
ajax.jsonRpc('/datatables/model/get_partners', 'call',
{ 'count_only' : 1 } )
.then(function(data) {
_.each(data, function(record) {
console.log('record: '+record['count']);
var nof_rows = record['count'];
while (true) {
ajax.jsonRpc('/datatables/model/get_partners', 'call',
{ 'offset' : offset, 'page_size' : page_size } )
.then( function(data) {
if (data) {
table.rows.add(data).draw()
}
});
if (nof_rows == 0) {
break;
}
offset += page_size;
if (nof_rows > page_size) {
nof_rows -= page_size;
} else {
page_size = nof_rows;
nof_rows = 0;
}
console.log('nof_rows: '+nof_rows);
}
});
});
});
XML模板表:
<table id="partners_data" class="display cell-border">
<thead>
<tr>
<th>Name</th>
<th>Title</th>
<th>Language</th>
<th>Supplier</th>
<th>Employee</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Title</th>
<th>Language</th>
<th>Supplier</th>
<th>Employee</th>
</tr>
</tfoot>
<tbody>
</tbody>
</table>
但我没有从中得到任何数据。页脚更改,但表格保持空白。