I have a site where I'm rendering blocks from a JSON file where the user can then sort blocks if they want it in another way that I then save to the database. My problem is how to keep the same structure as it's not a flat JSON structure.
Example from JSON file:
[{
"part": 0,
"blocks": [
{
"type": "template",
"id": "SOME_ID_0"
},
{
"type": "template",
"id": "SOME_ID_1"
},
]
},
{
"part": 1,
"blocks": [
{
"type": "template",
"id": "SOME_ID_2"
},
{
"type": "template",
"id": "SOME_ID_3"
},
]
}]
From the JSON file, each object within "blocks" are rendered and can be rearranged so block in part 0 could have 3 blocks, and part 1 could have only one block etc. The reason for why each block is within "part" and not just flat is because I render only some parts in some special cases.
HTML example:
<div class="blockContent" data-blockcontent="ADD 'part' JSON HERE SOMEHOW?">
<div class="blockContent" data-blockcontent="{"type":"template","id":"SOME_ID_0"}">Some info 1</div>
<div class="blockContent" data-blockcontent="{"type":"template","id":"SOME_ID_1"}">Some info 2</div>
</div>
<div class="blockContent" data-blockcontent="ADD 'part' JSON HERE SOMEHOW?">
<div class="blockContent" data-blockcontent="{"type":"template","id":"SOME_ID_2"}">Some info 3</div>
<div class="blockContent" data-blockcontent="{"type":"template","id":"SOME_ID_3"}">Some info 4</div>
</div>
For the jquery/ajax code, I use push:
// Save block structure for ÅR
var blockContent=[];
$("#myForm").find('.blockContent').each(function(i,item){
blockContent.push($(item).data('blockcontent'));
});
$.ajax({
url:"/ajax/saveBlockStructure",
method:"POST",
async:true,
data: {
id: "{{ $id }}",
_token: "{{ csrf_token() }}",
blockStructure: JSON.stringify(blockContent)
}
}); }
For the PHP/Laravel code, I just have a loop that iterates the json file and I don't think that is necessary to show here.
I don't know how but I guess I should somehow add the start of the part/blocks in the parent div and end it and then start on the next part?
partwrapper element and pass thepartvalue to an attribute. Then loop all thosepartelements with internal loop to build it'sblocksarray