Datatables - Can't filter columns using sub items

224 Views Asked by At

I want to filter rows using "type" column value. it's coming from sub object named article. I can't direcly access that property. I need to use array key like row.articles.type

I'm able to filter using status which is depends on is_suspend column, I can easly access is_suspend saying that row.is_suspended. But it comes to sub object values, it doesn't filter..

one item look like;

{
id: 7,
user_id: 11,
article_id: 216,
content: "Rerum velit soluta quos provident corporis eum dolores. Dolorem ex deleniti quisquam. Voluptas eum sint itaque quibusdam vel. Non sit alias cumque doloribus aut.",
created_at: "2018-09-14 21:22:53",
updated_at: "2018-09-14 21:22:53",
is_suspend: 0,
user: {
       id: 11,
       name: "Mayra Rau",
       username: "edgar.schmeler",
       email: "[email protected]",
       created_at: "2018-09-11 00:15:47",
       updated_at: "2018-09-11 00:15:47",
       suspended_at: null,
       banned_at: null,
       points: "3772"
},
article: {
         id: 216,
         locale: "fr",
         content: "Qui earum repudiandae aliquam et ab aut maxime. Debitis alias voluptatum voluptate ut ut. Repellendus asperiores ut rem aut et voluptates eum. Sed voluptatem repellat maiores. Dolores commodi laboriosam voluptatem deserunt reprehenderit.",
         articleable_id: 72,
         articleable_type: "App\Models\Cpu",
         type: "Cpu",
         articleable: {
                      id: 72,
                      title: "AMD A4-9120",
                      slug: "amd-a4-9120"
                      }
         }
},

Field section

{
    field: "type",
    title: "Type",
    width: 70,

    template: function (row) {
    var status = {
        Cpu: {'title': 'CPU', 'class': 'm-badge--info'},
        Gpu: {'title': 'GPU', 'class': ' m-badge--default'},
        Mb: {'title': 'MB', 'class': ' m-badge--warning'},
    };
    return '<span class="m-badge '+status[row.article.type].class+' m-badge--wide">'+status[row.article.type].title+'</span>';
}

},

Filter section

$('#m_form_type').on('change', function () {
    datatable.search($(this).val(), 'type');
}).val(typeof query.Type !== 'undefined' ? query.Type : '');
0

There are 0 best solutions below