5

I am doing a Laravel project with editable inline with select option however I want I manage to query the brands and I want to display the array after I push it in the source. Please help

var brand = [];
data_brand.forEach(function(element) {   
    var branddetails = {value: element.id, text: element.brand_name}; 
    brand.push(branddetails);
});
$(function(){
    $('#brand').editable({
        value: 2,    
        source: [

            //I want to output like this {value: 1, text: 'Active'},
            brand.values() // this code does not work
        ]
    });
});
3
  • Can you not use the array directly? , source: brand Commented Mar 25, 2020 at 14:30
  • whats the error you are getting? Without iterating you cannot get the values of Array/List. Commented Mar 25, 2020 at 14:30
  • can you share what you are getting in data_brand? thanks Commented Mar 25, 2020 at 15:25

3 Answers 3

2

This should work:

source: brand.map(item => item)

or simply:

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

3 Comments

That makes no sense. forEach does not return anything.
did you mean brand?
@SivaKondapiVenkata, yes, edited. although collections should be named plural.
0

brand is an array and you since source also expecting the array, You can try like this

source: [...brand]

Comments

0

In order to display the array elements, use loop.

Example-

let branddetails = [{value: 1, text: "something" }];

branddetails.forEach(brand => console.log(brand));

-- Edit --

Instead of creating array and then getting the pushed element, you can directly add the element itself in the source array.

let branddetails;
data_brand.forEach(function (element) {
  branddetails = { value: element.id, text: element.brand_name };
});
$(function () {
  $('#brand').editable({
    value: 2,
    source: [
      branddetails 
    ]
  });
});

3 Comments

OP doesn't want to "display" the elements, they want to use them within .editable({
Ok, so now your edit will only contain the last value from data_brand
@freedomn-m I tried but it does not shows the values.

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.