0

I am trying to make a news object and in that section in which there are separate sub-section so it will look like this. user can add and remove subsection. image

and when I press the save button it should send the data as following json structure seperating each subsection by ||.

{"news": {
    "section1": ["ABCDE||FGHI||JKLM"],
    "section2": ["NOPQ"],
    "section3": ["RSTU"]
  }
}

and when use save the data it should get saved and when the user open that page again it should be as last saved. This is what I have tried so far. I have tried to make a div and then wrap test area in it in ng-repeat but it seems like it should be it a table.

// For adding the subsection
$scope.section1 = [];
    $scope.addsection1=function(){
    $scope.section1.push({});
}

// For removing the subsection
$scope.removesection1 = function(id){
    var indexToRemove;
    for(i = 0; i < $scope.section1.length; i++){
        if($scope.section1[i].id === id){
            indexToRemove = i;
        }
        $scope.section1.splice(indexToRemove, 1);
    }
}
<div class="section-div flex-column">
    <div class="flex-row">
        <h4 style="flex-grow: 2;">New Updates</h4> 
        <button class="add-btn" ng-click="addsection1()">+Add field</button>
    </div>
    <textarea ng-repeat="section in section1"
              style="margin: 7px;border: 1px solid #00000047;border-radius: 4px;"
              name="" id="">
    </textarea>     
</div>

Please help me I m a beginner in the angularjs. Thanks in advance.

2
  • ON a side note why angularjs? Are you constrained to this tech stack for some reason (like a legacy branch you have to work with)? Commented Feb 7, 2020 at 17:56
  • Yes I have to stick with angularjs Commented Feb 7, 2020 at 18:05

1 Answer 1

1

send the data as following json structure seperating each subsection by ||

One approach is to use array.join:

var arr = ["ABCDE", "FGHI", "JKLM"];

var obj = { news: { section1: [arr.join("||")] } };

console.log(obj);

Conversely, to receive, use string.split:

var obj = { news: { section1: ["ABCDE||FGHI||JKLM"] } };

var arr = obj.news.section1[0].split("||");

console.log(arr);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.