我有一个资源集合来获取我所有的对话。因为前端已经编码(由另一个人),所以我想将所有这些作为对象返回,以数据库的Dialogue_id作为键,以对话对象作为值。
但是当我想转换从资源集合中获取的数组(使用 (object) $array )时,它仍然返回一个没有我设置的任何键的数组。
在我的控制器函数中我调用:
return new DialogueResourceCollection($dialogues);
Run Code Online (Sandbox Code Playgroud)
我的收藏资源如下所示:
class DialogueResourceCollection extends ResourceCollection
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
$array = [];
for ($i = 0; $i < sizeof($this); $i++) {
$j = $this[$i]->dialogue_id;
$array[$j] = $this[$i];
}
return $array;
}
}
Run Code Online (Sandbox Code Playgroud)
我得到什么:
[
{
"dialogue_id": 1,
"text": "example text"
},
...
Run Code Online (Sandbox Code Playgroud)
我想得到什么:
{
"34" : {
"dialogue_id": 34,
"text": "example …Run Code Online (Sandbox Code Playgroud) 我正在使用 jsPDF 和 autotable 从 DOM 树中的表生成 PDF。我的项目基于 Wordpress,我通过 CDN 注册并排队 () 这两个库。
但是,当执行我的 pdf 生成代码(基本上是要测试的虚拟代码)时,它失败并出现以下错误:
Uncaught TypeError: t.getDocument().getFillColor is not a function
f https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.23/jspdf.plugin.autotable.min.js:10
c https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.23/jspdf.plugin.autotable.min.js:10
drawTable https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.23/jspdf.plugin.autotable.min.js:10
drawTable https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.23/jspdf.plugin.autotable.min.js:10
autoTable https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.23/jspdf.plugin.autotable.min.js:10
Run Code Online (Sandbox Code Playgroud)
由于我找不到任何类似的问题并且无法自己解决它,所以我想分享我的问题。
const doc = new jsPDF({
orientation: "landscape",
unit: "mm",
format: [300, 260]
});
doc.text(25, 25, "Header");
doc.addPage();
doc.autoTable({ html: '#my-table' });
doc.save(pdfName);
Run Code Online (Sandbox Code Playgroud)
<table id="my-table" class="table table-bordered table-custom">
<thead id="test-thead">
<tr>
<td>Col 1</td>
<td>Col 2</td>
</tr>
</thead>
<tbody id="test-tbody">
<tr>
<td>Test 1</td>
<td>Test 2</td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)